I have an app that plays audio on the background using AVPlayer. I use MPNowPlayingInfoCenter to display the song\'s metadata on the Lock Screen and Control Center. Everythi
Rather than using the UIEvent
stream with remoteControlReceivedWithEvent
, I would recommend you use MPRemoteCommandCenter
to control the previous/next/play/pause actions on the lock screen and control center.
import MediaPlayer
// ...
let commandCenter = MPRemoteCommandCenter.sharedCommandCenter()
commandCenter.previousTrackCommand.enabled = true;
commandCenter.previousTrackCommand.addTarget(self, action: "previousTrack")
commandCenter.nextTrackCommand.enabled = true
commandCenter.nextTrackCommand.addTarget(self, action: "nextTrack")
commandCenter.playCommand.enabled = true
commandCenter.playCommand.addTarget(self, action: "playAudio")
commandCenter.pauseCommand.enabled = true
commandCenter.pauseCommand.addTarget(self, action: "pauseAudio")
Where previousTrack
, nextTrack
, playAudio
, and pauseAudio
are all functions in your class where you control the player.
You may need to explicitly disable the skip forward and backward commands as well:
commandCenter.skipBackwardCommand.enabled = false
commandCenter.skipForwardCommand.enabled = false