Siri 会 "pause" 但不会 "play" AVPlayer
Siri will "pause" but not "play" AVPlayer
我正在制作一个 iOS 广播应用程序。我希望能够在播放时使用 Siri 暂停和播放(Siri 不需要启动应用程序,只需在启动后控制音频)。现在 "pause" 在 iOS 从我的 phone 工作(但不是从我的手表),但一旦暂停,我就无法将它转到 "play",即使我仍然在我的锁定屏幕中看到播放器。点击时锁定屏幕控件工作正常。
下面是一些可能相关的代码:
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
,但没有实施 play
或 pause
。您可能需要所有这三个。
我正在制作一个 iOS 广播应用程序。我希望能够在播放时使用 Siri 暂停和播放(Siri 不需要启动应用程序,只需在启动后控制音频)。现在 "pause" 在 iOS 从我的 phone 工作(但不是从我的手表),但一旦暂停,我就无法将它转到 "play",即使我仍然在我的锁定屏幕中看到播放器。点击时锁定屏幕控件工作正常。
下面是一些可能相关的代码:
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
,但没有实施 play
或 pause
。您可能需要所有这三个。