How to tell the MPNowPlayingInfoCenter wether or not the music is playing or paused?

佐手、 提交于 2019-12-05 01:38:47
Jan Pittner

Make sure that you're setting the MPNowPlayingInfoPropertyPlaybackRate property. 0.0f to indicate paused, 1.0f to indicate playing. You'll also need to set the MPNowPlayingInfoPropertyElapsedPlaybackTime when you change these values.

Here is example code where updateMetadata is a function that applies those changes to the MPNowPlayingInfoCenter.nowPlayingInfo dictionary. This would indicate to the center that the player is paused.

[self updateMetadata:[NSDictionary dictionaryWithObjectsAndKeys:
     [NSNumber numberWithDouble:audioFile.player.currentTime], 
      MPNowPlayingInfoPropertyElapsedPlaybackTime,
     [NSNumber numberWithFloat:0.0f],  
      MPNowPlayingInfoPropertyPlaybackRate,
      nil]];

I had this problem today: I found that the fact that I was recording audio as well as playing it caused the button to show the pause symbol.

When I stopped the AVAudioRecorder from recording the pause button became a play button.

Quite often, the problem is simply the iPhone Simulator. As soon as you are using the play() function of your AVAudioPlayer instance, the remote control bar is supposed to toggle pause/play automatically. If you run into problems where this doesn't happen, try to run your program on a device.

To toggle the button, you do not need to set any playingInfo of the MPNowPlayingInfoCenter, neither do you need to hold an active AVAudioSession.

Here is example code where updateMetadata is a function that applies those changes to the MPNowPlayingInfoCenter.nowPlayingInfo dictionary. This would indicate to the center that the player is paused.In swift

self.updateMetadata(NSDictionary.dictionaryWithValuesForKeys(NSNumber(Double(audioFile.player.currentTime))),MPNowPlayingInfoPropertyElapsedPlaybackTime,NSNumber(numberWithFloat:0.0f),MPNowPlayingInfoPropertyPlaybackRate,nil)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!