简体   繁体   中英

Spotify iOS SDK - No background play

I can't get the Spotify iOS SDK working with background play so that track continue to play when the phone is locked or the application is no longer active.

I have UIBackgroundModes in my Info.plist set up as so:

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

Is there something else I'm missing or something I need to enable when setting up the SDK in app?

Thanks in advance for any help

To fix this I had to extend my class to implement the SPTAudioStreamingPlaybackDelegate and write to functions to activate and deactivate the AVAudioSession

Step 1

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

Step 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)
}

I think I have done it before in one of my previous apps. Think you need to configure audio session right after app launch.

Here is a bit of code showing how to do that. But it's written in 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];
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM