ios - AudioKit 处理 AVAudioSessionInterruption
问题描述
在接到电话或只是让电话响起后,我们启用了后台播放的 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
}
推荐阅读
- typescript - Typescript提取类的公共接口
- javascript - 正则表达式仅允许 1-31 个数字并禁用字母和符号
- javascript - React GET API 调用中的无限循环以及使用 Hooks 和 useState() 的正确方法
- javascript - 在 Javascript 中将 jQuery 原生函数重建为 HTMLElements
- xamarin - Xamarin.Forms:MultiBinding IMultiValueConverter 接收 Null 值
- postgresql - 提高 SELECT 语句从具有 110 万行(包括相关表)PostgreSQL 的表中检索单条记录的性能
- android - Flutter:将 html 嵌入到容器中
- python-3.x - 如何将包从另一个环境安装到一个环境中?
- visual-studio - 编译问题
- android - 华为 Push Kit 通知消息键/值在 Android 中为空