2016-12-26 36 views
2

我无法使Spotify iOS SDK与后台播放一起工作,以便在手机被锁定或应用程序不再处于活动状态时跟踪继续播放。Spotify iOS SDK - 无后台播放

我已经UIBackgroundModes在我Info.plist设置为这样:

<key>UIBackgroundModes</key> 
<array> 
    <string>audio</string> 
    <string>fetch</string> 
</array> 

有别的我错过什么,我需要能够在应用程序设置的SDK的时候?

预先感谢任何帮助

回答

9

为了解决这个问题,我不得不延长我的类来实现SPTAudioStreamingPlaybackDelegate和写入功能,激活和关闭AVAudioSession

步骤1

func audioStreaming(_ audioStreaming: SPTAudioStreamingController!, didChangePlaybackStatus isPlaying: Bool) { 
    if isPlaying { 
     self.activateAudioSession() 
    } else { 
     self.deactivateAudioSession() 
    } 
} 

第2步

// MARK: Activate audio session 

func activateAudioSession() { 
    try? AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback) 
    try? AVAudioSession.sharedInstance().setActive(true) 
} 

// MARK: Deactivate audio session 

func deactivateAudioSession() { 
    try? AVAudioSession.sharedInstance().setActive(false) 
} 
0

我想我已经在我以前的应用程序之一做之前。认为您需要在应用程序启动后立即配置音频会话。

下面是一段代码,显示如何做到这一点。但它用Objective C.

- (void) initializeAudioSession 
{ 
    // Registers this class as the delegate of the audio session to listen for audio interruptions 
    [[NSNotificationCenter defaultCenter] addObserver: self 
              selector: @selector(audioRouteChanged:) 
               name: AVAudioSessionRouteChangeNotification 
               object: [AVAudioSession sharedInstance]]; 

    //Set the audio category of this app to playback (allows music to play in background) 
    NSError *setCategoryError = nil; 
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategorySoloAmbient error: &setCategoryError]; 
    if (setCategoryError) { 
     //RESPOND APPROPRIATELY 
     NSLog(@"AVAudioSession error: %@", setCategoryError); 
    } 

    // An instance of the audio player/manager is passed to the listener 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioRouteChanged:) name:AVAudioSessionRouteChangeNotification object:nil]; 

    //Activate the audio session 
    NSError *activationError = nil; 
    [[AVAudioSession sharedInstance] setActive: YES error: &activationError]; 
    if (activationError) { 
     //RESPOND APPROPRIATELY 
     NSLog(@"AVAudioSession error: %@", activationError); 
    } 
} 

#pragma mark - 
#pragma mark Audio session callbacks 

-(void)audioRouteChanged:(NSNotification*)audioChanged; 
{ 
    NSDictionary *userInfo = [audioChanged userInfo]; 
    int routeChangeReason = (int)[userInfo objectForKey:AVAudioSessionRouteChangeReasonKey]; 

    if ([SpotifyPlayer sharedPlayer].isPlaying) { 
     if (routeChangeReason == kAudioSessionRouteChangeReason_OldDeviceUnavailable) 
     { 
      [[SpotifyPlayer sharedPlayer] setIsPlaying:false callback:nil]; 
     } 
    } 
} 

void audioRouteChangeListenerCallback (void *inUserData, AudioSessionPropertyID inPropertyID, UInt32 inPropertyValueSize, const void *inPropertyValue) 
{ 
    if (inPropertyID != kAudioSessionProperty_AudioRouteChange) return; 


    CFDictionaryRef routeChangeDictionary = inPropertyValue; 
    CFNumberRef routeChangeReasonRef = CFDictionaryGetValue (routeChangeDictionary, CFSTR (kAudioSession_AudioRouteChangeKey_Reason)); 

    SInt32 routeChangeReason; 
    CFNumberGetValue (routeChangeReasonRef, kCFNumberSInt32Type, &routeChangeReason); 

    // "Old device unavailable" indicates that a headset was unplugged, or that the 
    // device was removed from a dock connector that supports audio output. 
    if (routeChangeReason == kAudioSessionRouteChangeReason_OldDeviceUnavailable) 
    { 
     [[SpotifyPlayer sharedPlayer] setIsPlaying:false callback:nil]; 
    } 
} 
+0

感谢您的回答基督徒。我只是想知道为什么当Spotify的示例项目中没有任何地方使用背景音频时,所有这些都是必需的? –

+0

@ja你确定它没有做任何事情吗?你指的是哪个例子? – christian

+0

我指的是包含在SDK下载中的示例项目。事实证明,他们确实有激活AVAudioSession的东西,虽然为什么它不是默认行为的一部分,似乎让我感到困惑 –