在接到电话或刚好让电话响起后,我们支持后台播放的 AudioKit 应用程序会永远静音,我不确定如何处理。重新启动声音输出的唯一方法是终止并重新启动应用程序。启用和使用 Siri 等其他中断可以顺利进行,并且应用程序的声音在事件期间会被隐藏。
通常,应用程序可以注册自己以接收通知(例如 NSNotification.Name.AVAudioSessionInterruption
)以检测 AVAudioSession
中断,但如何检索 AVSession
通常传递给通知的对象?
NotificationCenter.default.addObserver(self, selector: #selector(AppDelegate.sessionInterrupted(_:)),
name: NSNotification.Name.AVAudioSessionInterruption,
object: MISSING_AK_AVAUDIOSESSION_REF)
此外,如果能够成功实现音频中断通知,AudioKit 会怎样?它不是为“重新启动”或暂停而设计的。任何帮助将不胜感激。
最佳答案
这将取决于您的应用如何处理它。至少,您需要在中断结束时执行 Audiokit.stop() 和 Audiokit.start()。
你会想用这样的东西注册通知:
NotificationCenter.default.addObserver(self,
selector: #selector(handleInterruption),
name: .AVAudioSessionInterruption,
object: nil)
然后用这样的方式处理它:
@objc internal func handleInterruption(_ notification: Notification) {
guard let info = notification.userInfo,
let typeValue = info[AVAudioSessionInterruptionTypeKey] as? UInt,
let type = AVAudioSessionInterruptionType(rawValue: typeValue) else {
return
}
//...handle each type here
}
关于ios - AVAudioSessionInterruption 的 AudioKit 处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50789652/