Spotify iOS SDK - 没有后台播放

时间:2016-12-26 16:36:51

标签: ios spotify

我无法让Spotify iOS SDK使用后台播放功能,以便在手机锁定或应用程序不再有效时继续播放曲目。

我的UIBackgroundModes设置为Info.plist,如此:

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

在应用中设置SDK时,我还缺少其他我需要启用的内容吗?

提前感谢您提供任何帮助

2 个答案:

答案 0 :(得分:11)

要解决此问题,我必须扩展我的课程以实现Property Customer->passwd is empty at line 872 in file classes/ObjectModel.php并写入函数以激活和停用SPTAudioStreamingPlaybackDelegate

第1步

AVAudioSession

第2步

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

答案 1 :(得分: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];
    }
}