Update MPRemoteCommandCenter play/pause button
Asked Answered
A

2

7

I have an iOS 9 app that plays MIDI-like songs using an AudioGraph. I've set up remote control commands with MPRemoteCommandCenter and info with MPNowPlayingInfoCenter such that the currently playing song shows up in Control Center and responds to control center button presses or headphone button presses. Tapping the pause button in Control Center pauses my song, but the playhead in Control Center keeps moving and the button remains as a pause icon. Tapping it again just keeps calling pause.

Is there a way to update the MPNowPlayingInfoCenter or MPRemoteCommandCenter state such that Control Center knows the song is paused?

Solutions I have tried:

  • Setting enabled on playCommand and pauseCommand

    has no effect

  • Setting MPNowPlayingInfoPropertyPlaybackRate to 0 in MPNowPlayingInfoCenter

    stops the playhead from advancing (and resets the elapsed time if I don't explicitly set it as well), but the button is still a pause button

  • Stopping my audio engine and calling AVAudioSession.setActive(false)

    this does seem to work, but it is slow, causes audio glitches, and other parts of my app rely on the audio engine running even if a song is paused

Ashla answered 20/4, 2016 at 20:50 Comment(0)
B
2

It appears that the only way for MPRemoteCommandCenter to know that the audio is paused is for streaming to stop, so the answer to your question appears to be "no". It is a pity that we do not have direct control over Control Center's transport control states...

Since your app requires constant audio streaming, it would seem that your best option is to forego Control Center audio transport support. Perhaps a middle ground would be to remove support for playing and pausing, while retaining the timeline of currently playing audio (as you said above, you can stop the timeline by setting MPNowPlayingInfoPropertyPlaybackRate to 0).

In my case, upon pressing Control Center's pause button, the Control Center play button is only shown if I stop streaming audio (in my case, this means calling stop on my AVAudioEngine instance, and there is no need in my case to set my AVAudioSession to inactive). Stopping streaming also pauses the Control Center's timeline, so for others taking the "stop streaming" solution, you needn't mess with setting MPNowPlayingInfoPropertyPlaybackRate.

Borowski answered 31/5, 2016 at 18:50 Comment(1)
Yeah, I decided to just not use the Control Center playback controls, unfortunately.Ashla
R
0

You can use https://developer.apple.com/documentation/mediaplayer/mpnowplayingplaybackstate?language=objc

MPNowPlayingInfoCenter.defaultCenter.nowPlayingInfo = MPNowPlayingPlaybackStatePlaying;

or

MPNowPlayingInfoCenter.defaultCenter.nowPlayingInfo = MPNowPlayingPlaybackStatePaused;

avilable from iOS 13.0

Rochellrochella answered 10/8, 2022 at 9:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.