4

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:

Community
  • 1
  • 1
driedel
  • 178
  • 9
  • It's terrible. I think the only way to fix the issue is to tell Apple about it. Have you filed a bug report? – evenodd Aug 16 '16 at 20:33

1 Answers1

2

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.

Jordi Bruin
  • 1,588
  • 1
  • 11
  • 20
  • 1
    there seems to be no way around it. Our best bet is to wait until they bring order to this confusing API. Thanks for the answer – driedel Aug 23 '16 at 09:46
  • Hi Jordi & @driedel, did you by any chance got this working? Two years later, I'm still with this very same issue :( – Andres C Nov 05 '18 at 11:29
  • @andres.cianio nope, issues are still the same. You can use the applicationMusicPlayer instead of the systemMusicPlayer to get a bit more control though. What are you working on right now? – Jordi Bruin Nov 05 '18 at 15:21
  • 1
    @JordiBruin yep, that's what I'm doing at the moment, I'd rather have a little control than NO control at all... But one thing with applicationMusicPlayer is that it doesn't show in the lockscreen (eventhough it shows in the control center, why one and not the other beats me). Got any idea on how to manage that? Also, only way to get next & previous notifications is setting a queue, which beats the purpose of what I'm building to sync multiple streaming services in a single playlist – Andres C Nov 06 '18 at 12:35
  • Haven't really worked a lot with applicationMusicPlayer so can't help you with that sorry – Jordi Bruin Nov 06 '18 at 12:36