xcode - MPNowPlayingInfoCenter info is not displayed on iOS 8

£可爱£侵袭症+ 提交于 2019-11-27 20:54:46

The problem is that you are not satisfying the requirements to become master of the lock screen and control center, as I explain in my book. You should be seeing the modern (iOS 8) equivalent of this:

The fact that you are not seeing it suggests that you are omitting one or more of the requirements, which I list (quoting directly from my book here):

  • Your app must contain a UIResponder in its responder chain that returns YES from canBecomeFirstResponder, and that responder must actually be first responder.
  • Some UIResponder in the responder chain, at or above the first responder, must implement remoteControlReceivedWithEvent:.
  • Your app must call the UIApplication instance method beginReceivingRemoteControlEvents.
  • Your app’s audio session’s policy must be Playback.
  • Your app must be emitting some sound.

I don't know which of those you are omitting; it could be more than one. You might like to compare your code with a working example, here:

https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/iOS7bookExamples/bk2ch14p653backgroundPlayerAndInterrupter/backgroundPlayer/backgroundPlayer/ViewController.m

Want to expand @matt's answer - setting nowPlayingInfo is useless when you use AVAudioSessionCategoryOptionMixWithOthers option.

Expanding further on @matt's answer, it's required that you call endReceivingRemoteControlEvents and resignFirstResponder when the app comes back into the foreground (applicationDidBecomeActive). Otherwise the OS assumes you are a bad actor (or forgot to turn them off) and turns off your ability to show the sleep controls entirely, even after you call beginReceivingRemoteControlEvents again. I added these calls and now the Sleep Controls always show when they should.

Want to explain @zakhej's answer - what's happens when set AVAudioSessionCategoryOptionMixWithOthers.

  1. MPNowPlayingInfoCenter is a singleton, which means can only one application set it.
  2. AVAudioSession is shared with other apps, every app can set it.

so. when you set category AVAudioSessionCategoryAmbient, AVAudioSessionCategoryPlayAndRecord and mix with others, AVAudioSessionCategoryPlayback and mix with others, setting MPNowPlayingInfoCenter's nowPlayingInfo is useless.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!