First, the code where I first encountered this runs fine on iOS 7, 8, 8.4, etc. Doesn't work though on any of the iOS 9 betas.
Update: I downloaded the "Add Music" Sample Code and recreated the exact same problem with 100% reproducable steps. I opened a bug for it and uploaded their sample code to demonstrate, 21784478. So if anyone else is seeing MPMusicPlayerController hang when you call "play" it looks like a real issue.
I'm creating a musicPlayerController using [MPMusicPlayerController applicationMusicPlayer], initializing the audio session, giving the player a collection, and calling "play", whereupon the app hangs, and nothing plays, until I background my app. Also, when in this waiting state, any other calls to the music controller or property queries on the current MediaItem will also hang. But, they have been sent, and if you press the home button on the device to background the app your music will play.
appMusicPlayer = [MPMusicPlayerController applicationMusicPlayer];
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty (
kAudioSessionProperty_AudioCategory,
sizeof (sessionCategory),
&sessionCategory
);
UInt32 shouldMix = true; // I need to mix audio with iTunes, which is why I use applicationMusicPlayer above
AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof (shouldMix), &shouldMix);
AudioSessionSetActive(YES); // For older iOS versions, iOS 7 and earlier, depricated now in favor of calling setActive (below)
NSError *activationErr = nil;
[[AVAudioSession sharedInstance] setActive: YES error: &activationErr];
[appMusicPlayer setQueueWithItemCollection: collection]; // Pass in a collection of Music Items
[appMusicPlayer setRepeatMode: MPMusicRepeatModeNone];
[appMusicPlayer beginGeneratingPlaybackNotifications];
[appMusicPlayer play];
When I call the play method of the controller the app the music doesn't start playing--but if I background the app it will suddenly start playing, then when I return my app to the foreground I can skip between tracks and things will work until I press stop and [appMusicPlayer stop] is called. Then after [appMusicPlayer stop] is called subsequent calls to [appMusicPlayer play] won't work (and hangs for a few seconds in the process) until I background my app, then the music starts playing.
Even stranger, if I restart the device with a full shutdown and start my app first, the "play" method call will work immediately with no backgrounding necessary, but it will do so in the wrong mode, as if ipodMusicPlayer were called instead of applicationMusicPlayer, and control will be handed off to the iOS Music app instead of staying with my app. I can see this by backgrounding my app and then going into the iOS Music app and I will see that it is playing my track and it's pause button will be visible (In the first scenario where my app hangs and I have to background it this doesn't happen). Also, in this scenario shutting down my app doesn't stop the music, again, because it seems to not be an applicationMusicPlayer but a systemMusicPlayer.