Siri 将"pause"但不会"play" AVPlayer



我正在制作一个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
}
}
}
}

谢谢。

我注意到您在命令中心实现了playpause,但没有实现playpause。你可能需要这三个。

最新更新