Apple Music contradicts MPNowPlayingInfoCenter

I need help with a problem when the music player application is playing in the background.

I can play music in the app and in the background with both services. I can also install MPNowPlayingInfoCenter and display the correct information, but play / pause, the next track and previous tracks only work when the user is authenticated using Spotify:

  • notifications are received correctly by the application when a user authenticates with Spotify

  • but it does not work when the user authenticates with Apple Music. In this case, it seems that Apple Music is the one who receives notifications.

I use AVPlayer to play music when syncing with Apple Music and SPTAudioStreamingController when syncing with Spotify.

Here is the media center setup code:

 - (void)setMediaCenterinfoForPlayer:(id)player { SPTAudioStreamingController *spotifyPlayer; AVPlayer *localPlayer; NSMutableDictionary *trackInfo = [[NSMutableDictionary alloc] initWithDictionary: @{ MPMediaItemPropertyTitle: self.currentTrack.name, MPMediaItemPropertyArtist: ((SPTArtist *)self.currentTrack.artists[0]).name, MPMediaItemPropertyAlbumTitle : self.currentTrack.album.name, MPNowPlayingInfoPropertyPlaybackRate: @(1.0) }]; if ([player isKindOfClass:[SPTAudioStreamingController class]]) { spotifyPlayer = (SPTAudioStreamingController *)player; [trackInfo setObject:[NSNumber numberWithFloat:spotifyPlayer.currentPlaybackPosition] forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime]; [trackInfo setObject:[NSNumber numberWithFloat:spotifyPlayer.currentTrackDuration] forKey:MPMediaItemPropertyPlaybackDuration]; } else { localPlayer = (AVPlayer *)player; NSTimeInterval playbackTime = [self currentPlaybackTimeForPlayer:player]; [trackInfo setObject:@(playbackTime) forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime]; [trackInfo setObject:@(CMTimeGetSeconds(localPlayer.currentItem.asset.duration)) forKey:MPMediaItemPropertyPlaybackDuration]; } [MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = trackInfo; [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; Class playingInfoCenter = NSClassFromString(@"MPNowPlayingInfoCenter"); if (playingInfoCenter) { [self albumURLCoverForCurrentTrackWithBlock:^(UIImage *albumImage) { MPMediaItemArtwork *albumArt = [[MPMediaItemArtwork alloc] initWithImage:albumImage]; [trackInfo setObject:albumArt forKey:MPMediaItemPropertyArtwork]; [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:trackInfo]; }]; } } 

Here is the code for handling events:

 - (void)remoteControlReceivedWithEvent:(UIEvent *)event { if (event.type == UIEventTypeRemoteControl) { MPNowPlayingInfoCenter *center = [MPNowPlayingInfoCenter defaultCenter]; NSMutableDictionary *playingInfo = [NSMutableDictionary dictionaryWithDictionary:center.nowPlayingInfo]; [playingInfo setObject:[NSNumber numberWithFloat:[AudioPlayerManager sharedInstance].spotifyPlayer.currentPlaybackPosition] forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime]; center.nowPlayingInfo = playingInfo; if (event.subtype == UIEventSubtypeRemoteControlPlay) { [[AudioPlayerManager sharedInstance] playTrack]; } else if (event.subtype == UIEventSubtypeRemoteControlPause) { [[AudioPlayerManager sharedInstance] pauseTrack]; } else if (event.subtype == UIEventSubtypeRemoteControlPreviousTrack) { [[AudioPlayerManager sharedInstance] previousTrack]; } else if (event.subtype == UIEventSubtypeRemoteControlNextTrack) { [[AudioPlayerManager sharedInstance] nextTrack]; } [[NSNotificationCenter defaultCenter] postNotificationName:kEventTypeRemoteControlUpdateState object:self]; } } 

Can someone point me to a way to solve this situation?

+7
ios objective-c notifications mpnowplayinginfocenter
source share

No one has answered this question yet.

See similar questions:

0
Spotify does not resume after video from WKWebView
0
Is there a way to get notifications about Apple Music playing in the background?

or similar:

441
This certificate has an invalid release of Apple Push Services.
thirty
Get current song from Spotify iOS app
17
How to set the current playback time and elapsed time on the iOS 7 locked screen?
10
How to say MPNowPlayingInfoCenter, or not, does music play or pause?
8
Taking audio session
5
MPNowPlayingInfoCenter is always in a playback state
5
MPNowPlayingInfoCenter conflicts with Apple Music
2
AVPlayer + MPNowPlayingInfoCenter runs on the simulator, but not on the device
one
MPNowPlayingInfoCenter reject
one
How can I disable my application (using AVPlayer) to mute when the ringtone is disabled?

All Articles