Reputation: 409
I have a released app that just started having trouble setting the nowPlayingItem on MPMusicPlayer for Apple Music tracks. This code works as expected for iTunes tracks on the device, but if the mediaitem set is an Apple Music track, a random track that is in the user's Apple Music library - but not in the current playerqueue - ends up playing. I suspect something has changed in iOS10.3.x or Apple Music is burbing just now. Anyone have ideas of how I could fix this?
Basic code is:
MPMusicPlayerController *musicPlayer;
[musicPlayer stop];
NSLog(@"*** trying to set nowplayingitem to %@", [UIAppDelegate.activeTrack.mediaitem valueForProperty:MPMediaItemPropertyTitle]);
musicPlayer.nowPlayingItem = UIAppDelegate.activeTrack.mediaitem ;
NSLog(@"nowplaying item is:%@", musicPlayer.nowPlayingItem.title);
[musicPlayer play];
if (![musicPlayer.nowPlayingItem.title isEqualToString:UIAppDelegate.activeTrack.name]){
NSLog(@">>>>>>> now playing does not equal current track so return and scratch head");
return;
}
2017-04-14 09:43:14.535079-0400 MyFitnessDJ[720:241825] *** trying to set nowplayingitem to Blame 2017-04-14 09:43:14.761134-0400 MyFitnessDJ[720:241825] nowplaying item is:(null) 2017-04-14 09:43:14.819226-0400 MyFitnessDJ[720:241825] >>>>>>> now playing does not equal current track so return and scratch head
Does anyone have any idea why the nowplaying item would not set to the correct Apple Music track with iOS10.3.x or could there be a burb in Apple Music?
Upvotes: 3
Views: 350
Reputation: 409
In case anyone comes across this post due to a similar problem in iOS 11, I'm back to having the same trouble. Keeping the musicPlayer playing doesn't work, not does wayneh's above solution.
As of the iOS11 general release, trying to set nowPlayingItem on a playing (or not) MPMusicPlayerController yields an unexpected result. Sometimes, it hangs the system for up to 5 seconds then possibly plays the track requested, or sometimes it hangs up to 5 seconds and plays a differing track (one that is on the device, but not necessarily in the queue) or sometimes the previously playing track continues with no apparent effect from the call. Oh, and sometimes it plays the requested track immediately as expected ;-)
If anyone has any luck making this work consistantly, please post.
Upvotes: 1
Reputation: 409
Seems that stopping the music player too soon before trying to set the nowPlayingItem
caused the music player to play a random track. Still don't understand why, but if the music player is not stopped, the nowPlayingItem
can be set.
Upvotes: 2