Search code examples
iphoneiosspotifycocoalibspotify-2.0libspotify

How to resume spotify player playback after interrupt when app is in background?


I have an app which plays Spotify track. It works perfectly when app moves to background but when it get interrupted by 'call' or 'iOS pop-ups' it stops playing. I have added Required background modes to App plays audio in .plist. How to resume playback when interruption ended while app is in background.


Solution

  • i have used audio interruption callbacks.

    AudioSessionInitialize (NULL, NULL, interruptionListenerCallback,self);
    AudioSessionSetActive(YES);
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    

    And after receiving interruption call play/pause method of playback manager.

    void interruptionListenerCallback (void *inUserData, UInt32 interruptionState)
    {
    // This callback, being outside the implementation block, needs a reference
    //to the AudioPlayer object
    CustomPlayerView *controller = (__bridge CustomPlayerView *) inUserData;
    
    if (interruptionState == kAudioSessionBeginInterruption)
    {
       [controller.playbackManager setIsPlaying:NO];
    }
    else if (interruptionState == kAudioSessionEndInterruption)
    {
       [controller.playbackManager setIsPlaying:YES];
    }
    }