ios - AVAudioSessionInterruption 的 AudioKit 处理

标签 ios iphone avaudiosession audiokit interruption

在接到电话或刚好让电话响起后,我们支持后台播放的 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/

相关文章:

.net - 使用 .Net 创建 CalDAV 服务

iphone - 关闭 UIImagePicker 后获取视频长度?

swift - 是否可以录制来自 iPhone 的音频?

ios - 如何在 Swift 中以编程方式使用主扬声器

iphone - iOS - 禁用 UIDocumentInteractionController 选项按钮

iphone - 如何删除按钮的背景图像

ios - Alamofire 上传带有进度的大文件并在应用重启时显示进度

ios - AudioUnit 录音每 30 秒出现一次故障

ios - 本地化描述,英文截图

iphone - 如何从底部uitableview返回主viewcontroller