I'm making an iOS radio app. I'd like to be able to use Siri to pause and play once it is already playing (Siri doesn't need to start the app, just control audio once it has already been initiated). Right now "pause" works in iOS from my phone (but not from my Watch), but I can't get it to "play" once it is paused, even though I still see the player in my lock screen. The lock screen controls work fine when tapped.
Here is some code that may be relevant:
func setupRemoteCommandCenter() {
let commandCenter = MPRemoteCommandCenter.shared()
// MARK: PLAY/PAUSE
commandCenter.togglePlayPauseCommand.addTarget { event in
self.togglePlayPause()
return .success
}
// MARK: Skip
func skipBackward(_ event: MPRemoteCommandEvent) -> MPRemoteCommandHandlerStatus {
guard let command = event.command as? MPSkipIntervalCommand else {
return .noSuchContent
}
goBackward15()
return .success
}
let skipBackwardCommand = commandCenter.skipBackwardCommand
skipBackwardCommand.isEnabled = !playingLivestream
skipBackwardCommand.addTarget(handler: skipBackward)
skipBackwardCommand.preferredIntervals = [-15]
func skipForward(_ event: MPRemoteCommandEvent) -> MPRemoteCommandHandlerStatus {
guard let command = event.command as? MPSkipIntervalCommand else {
return .noSuchContent
}
goForward30()
return .success
}
let skipForwardCommand = commandCenter.skipForwardCommand
skipForwardCommand.isEnabled = !playingLivestream
skipForwardCommand.addTarget(handler: skipForward)
skipForwardCommand.preferredIntervals = [30]
}
@objc func handleInterruption(notification: Notification) {
guard let userInfo = notification.userInfo,
let typeValue = userInfo[AVAudioSessionInterruptionTypeKey] as? UInt,
let type = AVAudioSession.InterruptionType(rawValue: typeValue) else {
return
}
if type == .began {
print("Interruption began")
// Interruption began, take appropriate actions
}
else if type == .ended {
if let optionsValue = userInfo[AVAudioSessionInterruptionOptionKey] as? UInt {
let options = AVAudioSession.InterruptionOptions(rawValue: optionsValue)
if options.contains(.shouldResume) {
// Interruption Ended - playback should resume
print("Interruption Ended - playback should resume")
play()
} else {
// Interruption Ended - playback should NOT resume
print("Interruption Ended - playback should NOT resume")
playerPaused = true
}
}
}
}
Thank you.