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

4

11

I can't seem to make iOS show the correct play / pause button in the remote audio controls. I do receive the remote control events and set all values of the nowPlayingInfo dictionary.

Everything works fine and I even see a cover photo on the lock screen. Except the pause/play button. It always looks like pause even if my AVAudioPlayer is playing. It sends a pause event regardless of playback state.

How can I notify iOS that AVAudioPlayer is paused and that it should now show a play button in the remote control buttons bar?

Aventine answered 9/9, 2013 at 19:59 Comment(2)
If you figured this out, please post your resolution.Sophi
Do you update MPNowPlayingInfoCenter on playback state changes (e.g. pause)?Lamed
U
6

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]];
Unwholesome answered 10/9, 2014 at 12:55 Comment(0)
M
0

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.

Musicianship answered 9/2, 2014 at 5:34 Comment(0)
M
0

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.

Misdeem answered 14/7, 2016 at 10:17 Comment(0)
A
0

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)
Affined answered 24/10, 2016 at 4:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.