Search code examples
iosiphoneaudiompmusicplayercontroller

MPMusicPlayerController won't interact with Remote Controls


even though this question ist asked many times and I didn't find any satisfying answer to this, I still wanna give it another try:

I'm trying to play MPMediaItems with the MPMusicPlayerController. So far so good. This is needed to play files from Itunes Match or Apple Music. Any other files are played via AVPlayer.

Handling remote controls for songs played with the AVPlayer seems to be no problem using:

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];

and

- (void)remoteControlReceivedWithEvent:(UIEvent *)receivedEvent

Also it is possible to retrieve AVPlayerItemDidPlayToEndTimeNotification in background reacting on this to start playing next song.

Trying all of this playing a song with MPMusicPlayerController doesn't seem to work as it should. I'm trying to play a song like this:

 self.audioPlayer = [MPMusicPlayerController applicationMusicPlayer];
 self.audioPlayer.repeatMode = MPMusicRepeatModeNone;
 [self.audioPlayer setQueueWithItemCollection:[MPMediaItemCollection collectionWithItems:@[_playerItem]]];

using the given observers:

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(playbackStateDidChange:) name:MPMusicPlayerControllerPlaybackStateDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(playbackItemDidChange:) name:MPMusicPlayerControllerNowPlayingItemDidChangeNotification object:nil];
[self.audioPlayer beginGeneratingPlaybackNotifications];

If the app is in foreground the observers are called as they should. Going into background the observers are not called anymore. In this case it is not possible for me to detect the end of a song to skip to the next song.

Also - remote controls wont work. I used:

[MPRemoteCommandCenter sharedCommandCenter].playCommand.enabled = YES;
[[MPRemoteCommandCenter sharedCommandCenter].playCommand addTarget:self action:@selector(remotePlay)];

[MPRemoteCommandCenter sharedCommandCenter].pauseCommand.enabled = YES;
[[MPRemoteCommandCenter sharedCommandCenter].pauseCommand addTarget:self action:@selector(remoteStop)];

[MPRemoteCommandCenter sharedCommandCenter].previousTrackCommand.enabled = YES;
[[MPRemoteCommandCenter sharedCommandCenter].previousTrackCommand addTarget:self action:@selector(loadPreviousSong)];

[MPRemoteCommandCenter sharedCommandCenter].nextTrackCommand.enabled = YES;
[[MPRemoteCommandCenter sharedCommandCenter].nextTrackCommand addTarget:self action:@selector(loadNextSong)];

But nether in foreground nor in background these given selectors are called.

Does anyone have a solution to my problem? As said I need to use MPMusicPlayerController as I want to playback Apple Music and iTunes Match tracks.

Related Questions:


Solution

  • Because the Music app is handling the actual playback of the items, your own app doesn't receive the notifications. Your best bet is to file an enhancement request through bugreport.apple.com.

    The Apple Music / iTunes team is listening to feedback on how to improve the Apple Music API from what I could tell at WWDC.