AVAudioEngine 不在 SpriteKit 中发布通知

AVAudioEngine not posting notification in SpriteKit

我在 SpriteKit 游戏中遇到 AVAudioEngine 问题 - 当 plugging/unplugging 耳机时引擎停止,当播放下一个声音时应用程序崩溃。这是一个已知的错误(或功能?) - 有修复它的建议 - 使用通知中心,AVAudioEngine 应该 post 在它改变它的状态时通知。我做了这个代码:

let notificationName = Notification.Name("AVAudioEngineConfigurationChange")      

NotificationCenter.default.addObserver(self, selector: #selector(self.restartEngine(notification:)), name: notificationName, object: nil)

当我这样做时:

NotificationCenter.default.post(name: notificationName, object: nil)

我的选择器被调用了。然而,当我 plug/unplug 耳机时 - 没有任何反应。 Swift3,xcode8,iOS9.3 关于如何解决它的任何建议?

通知的原始 Cocoa 名称是 AVAudioEngineConfigurationChangeNotification,但 Swift 常量称为 AVAudioEngineConfigurationChange。因此,您可以使用以下任一方式处理通知:

let notificationName = Notification.Name("AVAudioEngineConfigurationChangeNotification")

let notificationName = Notification.Name.AVAudioEngineConfigurationChange

当 plugging-in/unplugging 耳机时,您的选择器应该被调用。

这似乎只是 SpriteKit 音频失真的部分解决方案。我在通知处理程序中尝试了以下内容:

try? self.audioEngine.start() 

我能够阻止崩溃,但是我的 SKAudioNode 在 plug/unplug 事件之后不会发出任何声音,直到进程重新启动。