Ok, having some problems with Apple's command center here, playing background audio/on lock screen and cant understand why. Seems pretty simple but I don't even have the episode information displaying reliably in command center, definitely cant play/pause.
First I start the Audio session:
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, with: .mixWithOthers)
print("Playback OK")
try AVAudioSession.sharedInstance().setActive(true)
print("Session is Active")
} catch {
print(error)
}
then I set up my command center buttons to be enabled explicitly:
UIApplication.shared.beginReceivingRemoteControlEvents()
let commandCenter = MPRemoteCommandCenter.shared()
commandCenter.skipForwardCommand.isEnabled = true
commandCenter.skipBackwardCommand.isEnabled = true
commandCenter.nextTrackCommand.isEnabled = true
commandCenter.togglePlayPauseCommand.isEnabled = true
// commandCenter.previousTrackCommand.isEnabled = true
// commandCenter.togglePlayPauseCommand.isEnabled = true
commandCenter.togglePlayPauseCommand.addTarget(self, action:#selector(togglePlayPause))
// commandCenter.nextTrackCommand.addTarget(self, action:#selector(nextTrackForward))
// commandCenter.previousTrackCommand.addTarget(self, action:#selector(nextTrackBack))
commandCenter.skipForwardCommand.addTarget(self, action:#selector(ffPressed))
commandCenter.skipBackwardCommand.addTarget(self, action:#selector(rwPressed))
Here is the lldb plist keys if that matters for audio:
What's wrong here?
Maybe your question is old but someone could need this too, so this is how I did it :
Swift 5
let player = AVPlayer()
func commandCenter () {
let commandCenter = MPRemoteCommandCenter.shared()
commandCenter.playCommand.addTarget { (commandEvent) -> MPRemoteCommandHandlerStatus in self.play(); return .success }
commandCenter.pauseCommand.addTarget { (commandEvent) -> MPRemoteCommandHandlerStatus in self.pause(); return .success }
}
func pause () {
player.pause ()
print("paused")
}
func play () {
player.play()
print("play")
}
You don't need to enable commandCenter.playCommand to true