Я создаю iOS приложение для радио. Я хотел бы иметь возможность использовать Siri для приостановки и воспроизведения после того, как оно уже воспроизводится (Siri не нужно запускать приложение, просто управляйте звуком, как только он уже запущен). Прямо сейчас «пауза» работает в iOS с моего телефона (но не с моих часов), но я не могу заставить его «играть», когда он приостановлен, хотя я все еще вижу плеер на экране блокировки. При нажатии кнопки управления экраном блокировки работают нормально.
Вот код, который может иметь отношение к делу:
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
}
}
}
}
Спасибо.