Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lock Screen iPod Controls Not Working With Spotify Music Player

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?

like image 222
JeffB6688 Avatar asked Apr 15 '15 15:04

JeffB6688


1 Answers

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.

like image 89
Caleb Avatar answered Oct 20 '22 01:10

Caleb