【问题标题】:AVAudioEngine not posting notification in SpriteKitAVAudioEngine 不在 SpriteKit 中发布通知
【发布时间】:2016-11-22 09:50:49
【问题描述】:

我在 SpriteKit 游戏中遇到了 AVAudioEngine 的问题 - 插入/拔出耳机时引擎停止并且当下一个声音播放时应用程序崩溃。这是一个已知的错误(或功能?) - 建议修复它 - 使用通知中心,AVAudioEngine 应该在更改其状态时发布通知。我已经制作了这段代码:

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 关于如何修复它的任何建议?

【问题讨论】:

    标签: ios notifications avaudioengine


    【解决方案1】:

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

    let notificationName = Notification.Name("AVAudioEngineConfigurationChangeNotification")
    

    let notificationName = Notification.Name.AVAudioEngineConfigurationChange
    

    然后在插入/拔出耳机时调用您的选择器。

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

    try? self.audioEngine.start() 
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-13
      • 1970-01-01
      • 2021-12-07
      • 2020-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多