2016-11-22 69 views
0

我SpriteKit游戏中有一个问题,AVAudioEngine - 插入耳机/拔出时,发动机停止并在接下来的声音播放应用程序崩溃。这是一个已知的bug(或功能?) - 某项建议修复它 - 使用通知中心,AVAudioEngine应该张贴通知时,它正在改变它的状态。当我这样做AVAudioEngine不张贴通知,SpriteKit

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) 

我的选择被称为我有此代码。但是,当我插入/拔下耳机 - 没有任何反应。斯威夫特3,Xcode中8,iOS的9.3 关于如何解决它有什么建议?

回答

0

通知的原始可可名称是AVAudioEngineConfigurationChangeNotification,但Swift常数被称为AVAudioEngineConfigurationChange。因此,您可以通过使用处理通知:插头插入/拔出耳机时

let notificationName = Notification.Name("AVAudioEngineConfigurationChangeNotification") 

let notificationName = Notification.Name.AVAudioEngineConfigurationChange 

你的选择应该再被调用。

这似乎仅是用于SpriteKit音频片状的部分解决方案。我尝试了通知处理程序中执行以下操作:

try? self.audioEngine.start() 

我能停止崩溃,但我SKAudioNode不会使插/拔事件之后的任何声音,直到进程重新启动。