Lock Screen iPod Controls Not Working With Spotify Music Player
Asked Answered
F

2

6

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?

Forethought answered 15/4, 2015 at 15:58 Comment(0)
F
0

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];
Forethought answered 9/6, 2015 at 19:4 Comment(0)
D
2

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.

Dull answered 28/5, 2015 at 21:53 Comment(2)
I'm not sure that this will help me. If I add these methods to my appDelegate, they do not get called. I suspect they don't get called because my audio player is not in the appDelegate. But this gives me food for thought. Perhaps it has something to do with your suggestion. I'll keep trying.Forethought
I also ensure that my audio player can be referenced through the appDelegate as well. That way no matter which view controller or view you are in it can be controlled seamlessly. That was the approach I had taken. Good luck.Dull
F
0

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];
Forethought answered 9/6, 2015 at 19:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.