I'm building a iOS media player that plays audio files using the AVPlayer. The player is all working fine. I'm now looking at controlling media playback from the lock screen and from headphones. I have implemented a target for MPRemoteCommandCenter.shared()
's playCommand
, pauseCommand
and togglePlayPauseCommand
as such:
let commandCenter = MPRemoteCommandCenter.shared()
commandCenter.pauseCommand.addTarget { (_) -> MPRemoteCommandHandlerStatus in
// Pause media
return .success
}
commandCenter.playCommand.addTarget { (_) -> MPRemoteCommandHandlerStatus in
// Play media
return .success
}
commandCenter.togglePlayPauseCommand.addTarget { (_) -> MPRemoteCommandHandlerStatus in
if player.isPlaying() {
// Pause media
} else {
// Play media
}
return .success
}
After implementing the above, both the lock screen controls and the headphone controls are working fine. My question is, what is the difference between using playCommand/ pauseCommand vs togglePlayPauseCommand?
I noticed that on some devices that I test with, the headphones button will not work when I remove the togglePlayPauseCommand. This gives me a hint to what the difference is but I am not sure. Any help clarifying this would be great, thanks!
UPDATE:
After some more testing, it seems that togglePlayPauseCommand
is used to play/pause media for wired headphones. Whereas the separate playCommand
and pauseCommand
are used to play/pause media for wireless bluetooth headphones (as well as the lock screen). Still not 100% sure why the two separate methods of playing/pausing media are needed though...