Screen lock iPod controls do not work with Spotify music player

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

If music plays from MPMusicPlayerController, I can receive 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 iPod controls are visible when the app enters the background, they don’t respond when I pause. Instead, the iPod controls disappear when I pause. What add-on is needed to detect play / pause and FFW / RWD when streaming audio, such as Spotify, in the background from the lock screen?

+6
source share
2 answers

After further research, I found that if I turn on the following code when my application enters the background and when remote control events are received, 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]; 
0
source

I believe that I came across this in the past. If I remember correctly, I added in

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

and

 - (BOOL) canBecomeFirstResponder { return YES; } 

for the application delegate (this is also where my audio controller lived). I had a problem when the UIViewControllers were not alive while I wanted to catch the UIEventTypeRemoteControl notifications.

Give a try and see if this helps.

+2
source

Source: https://habr.com/ru/post/985196/


All Articles