问题
This is a problem that has been bothering us for months now. I created a bug report for apple, but they aren't responding. I also created a sample project that demonstrates the bug. I would really appreciate any ideas that you guys have. So without further adieu, here is the description of the problem and a link to the sample project. Thank you so much!
Sample Project: https://dl.dropboxusercontent.com/u/28210/AppleMusicBug.zip
MPMusicPlayerController is used to play Apple Music songs via the new setQueueWithStoreIDs method. This was introduced when iOS 9.3 was released. The issue is that once you leave the app, these two notifications no longer fire and wake up our app:
MPMusicPlayerControllerPlaybackStateDidChangeNotification MPMusicPlayerControllerNowPlayingItemDidChangeNotification
The reason this is so so critical is that our app does not use exclusively Apple Music to play songs. We need to know when a song ends so that we can fire off code to play another song in our custom queue. Currently, this is impossible. We have attempted to use background tasks, but this does not work, as our methods does not work on background tasks. We need the same type of functionality that AVPlayerItem uses (AVPlayerItemDidPlayToEndTimeNotification) that will wake up our app when a song ends, regardless if the app is in the foreground or background. Without this, we can't use the API.
////////////////////////////////// Steps to reproduce //////////////////////////////////
- Install the app on an iPhone with an Apple Music Premium subscription. Ensure that the permission settings are open for the test app. Ensure that you are able to play a full song in the music app using your Apple Music subscription. (I understand you might not have a premium subscription. I'm not positive, but the issue should still be present if you are playing a song using MPMuiscPlayerController, however you are able to play that song)
- Open the test app.
- A popup should come up confirming that the app sees your apple music premium subscription. It should say "Great. Now play a song."
- Tap to play a song. The song should being playing in a few moments.
- Tap the home button to bring the app to the background.
At this point you will notice that the notifications (MPMusicPlayerControllerNowPlayingItemDidChangeNotification and MPMusicPlayerControllerPlaybackStateDidChangeNotification) will not fire. When the song that you play ends, the application is not notified. Furthermore, you'll notice that the lock screen controls do not work, and remoteControlReceivedWithEvent is not notified of these events as well. Only when you bring the app back to the foreground are the notifications such listed above fired. This needs to work in the background.
Thanks for helping us with the issue. If you would like to discuss the problem more in depth via email, you can reach me at gabe@cymbal.fm.
:)
来源:https://stackoverflow.com/questions/39621072/mpmusicplayercontroller-does-not-send-background-notifications-when-using-apple