6
votes

I added the Spotify player to my app which also plays music using the MPMusicPlayerController. When music is playing from Spotify and the screen is locked, the remote control events are not received for play/pause and FFW/RWD when the user presses these buttons on the locked screen.

If music is playing from the MPMusicPlayerController, I am able to receive the remote control events based on the following code:

-(void) ViewDidLoad {
    ...
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    [self becomeFirstResponder];
    ...
}

and

- (BOOL) canBecomeFirstResponder
{
    return YES;
}

- (void) remoteControlReceivedWithEvent: (UIEvent*) event
{
    // see [event subtype] for details
    if (event.type == UIEventTypeRemoteControl) {
        // We may be receiving an event from the lockscreen
        switch (event.subtype) {
            case UIEventSubtypeRemoteControlTogglePlayPause:
            case UIEventSubtypeRemoteControlPlay:
            case UIEventSubtypeRemoteControlPause:
                // User pressed play or pause from lockscreen
                [self playOrPauseMusic:nil];
                break;

            case UIEventSubtypeRemoteControlNextTrack:
                // User pressed FFW from lockscreen
                [self fastForwardMusic:nil];
                break;

            case UIEventSubtypeRemoteControlPreviousTrack:
                // User pressed rewind from lockscreen
                [self rewindMusic:nil];
                break;

            default:
                break;
        }
    }
}

While the iPod controls are visible when the app enters the background, they do not respond when I press pause. Instead, the iPod controls disappear when I press pause. What addition is needed to enable detection of play/pause and FFW/RWD when streaming audio such as Spotify is playing in the background from lock screen?

2

2 Answers

2
votes

I believe I ran into this in the past. If I remember correctly I added in the

-(void)remoteControlReceivedWithEvent:(UIEvent *) event { ... }

as well as

- (BOOL) canBecomeFirstResponder { return YES; }

to the app delegate (This is also where my audio controller lived). I was having having the issue where the UIViewControllers were not alive during the time I wanted to catch the UIEventTypeRemoteControl notifications.

Give that a try and see if that helps.

0
votes

After further investigation, I have found that if include the following code when my app enters the background and when the remote control events are received, the iPod controls do not disappear.

// Set up info center to display album artwork within ipod controls (needed for spotify)
MPMediaItemArtwork *ipodControlArtwork = [[MPMediaItemArtwork alloc]initWithImage:artworkImage];
[MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = [NSDictionary dictionaryWithObjectsAndKeys:nowPlayingTitle, MPMediaItemPropertyTitle,
                                                         nowPlayingArtist, MPMediaItemPropertyArtist, ipodControlArtwork, MPMediaItemPropertyArtwork,  [NSNumber numberWithDouble:0.0], MPNowPlayingInfoPropertyPlaybackRate, nil];