Background Audio with cocoalibspotify

2019-03-16 00:28发布

问题:

I've properly enabled background audio for my app (in the plist). Playing the next track after the current is complete using SPPlaybackManager in the background (when the phone is locked/off) doesn't work.

When the current track ends, and the audio stops, the app won't begin playing the next track until the phone is unlocked and my app becomes active again.

How do I fix this? Here is a snippet of code I'm using to begin the playing of the next track. I observe that the current track becomes nil, and then begin playing the next track. The log shows me that the next current track is being set in the playback manager object, but it alas is silent.

- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {



    if([keyPath isEqualToString:@"spotifyPlaybackManager.currentTrack"]){

        NSLog(@"%@ %@",keyPath,self.spotifyPlaybackManager.currentTrack);

        if(self.spotifyPlaybackManager.currentTrack==nil && self.mode == PlayerModeSpotify){

            NSLog(@"PLAY NEXT");
            [self.spotifyPlaybackManager playTrack:self.nextSPTrack callback:^(NSError *error){
                if(error) TKLog(@"Spotify Playback Error %@",error);
            }];
        }
        [[NSNotificationCenter defaultCenter] postNotificationName:PlayerNowPlayingItemDidChange object:self];
        return;
    }



    [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}

Console:

spotifyPlaybackManager.currentTrack (null)
PLAY NEXT
spotifyPlaybackManager.currentTrack <SPTrack: 0x60f8390>: Karaoke

回答1:

The solution is very simple but it took me a year to realize it. My old solution was to start a background task just before the previous track ended, and keep it running until the next track was playing. That was very error prone. Instead:

Keep track of your playing state (playing or paused). Whenever you transition into Playing, start a background task. Never stop it, unless you transition into Paused. Keep the state as Playing even between tracks. As long as you have the audio background mode in your info.plist and audio is playing, your background task will have an infinite timeout.

Some pseudo code:

@interface PlayController
@property BOOL playing;

- (void)playPlaylist:(SPPlaylist*)playlist startingAtRow:(int)row;
@end

@implementation PlayController
- (void)setPlaying:(BOOL)playing
{
    if(playing == _playing) return;
    _playing = playing;

    UIApplication *app = [UIApplication sharedApplication];
    if(playing)
        self.playbackBackgroundTask = [app beginBackgroundTaskWithExpirationHandler:^ {
            NSLog(@"Still playing music but background task expired! :(");
                    [app endBackgroundTask:self.playbackBackgroundTask];
                self.playbackBackgroundTask = UIBackgroundTaskInvalid;
            }];
    else if(!playing && self.playbackBackgroundTask != UIBackgroundTaskInvalid)
        [app endBackgroundTask:self.playbackBackgroundTask];
}
...
@end

Edit: Oh, and I finally blogged about it.



回答2:

CocoaLibSpotify does a lot of work to start playing a track and will likely spawn new internal threads in the process. I doubt this is allowed in the audio style of backgrounding so you'll likely need to start a temporary background task to change tracks.