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?
ios objective-c notifications mpnowplayinginfocenter
Marcelo gracietti
source share