【问题标题】:SKAudioNode() crashes when plugging in/out headphones插入/拔出耳机时 SKAudioNode() 崩溃
【发布时间】:2016-06-10 06:48:37
【问题描述】:

我正在使用SKAudioNode() 在我的游戏中播放背景音乐。我有播放/暂停功能,在我插入耳机之前一切正常。根本没有声音,当我调用暂停/播放功能时出现此错误

AVAudioPlayerNode.mm:333:开始:所需条件为假:_engine->IsRunning() com.apple.coreaudio.avfaudio', reason: '要求条件为假:_engine->IsRunning()

有人知道这是什么意思吗?

代码:

import SpriteKit

class GameScene: SKScene {

let loop = SKAudioNode(fileNamed: "gameloop.mp3")
let play = SKAction.play()
let pause = SKAction.pause()
var isPlaying = Bool()

override func didMoveToView(view: SKView) {  
    loop.runAction(play)
    isPlaying = true
    self.addChild(loop)
}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    _ = touches.first as UITouch!

    for _ in touches {
        if isPlaying {
            loop.runAction(pause)
            isPlaying = false
        } else {
            loop.runAction(play)
            isPlaying = true
        } 
    }
}
}

【问题讨论】:

  • AVAudioEngineConfigurationChangeNotification 对我没有帮助:(我之前发现了这个问题
  • 该死的。你在模拟器上运行吗?我似乎记得最近遇到过类似的问题..
  • 不,我正在我的设备上测试
  • @Whirlwind 这太好了!谢谢你。希望有人知道解决方案:)

标签: swift sprite-kit swift2 avaudioengine skaudionode


【解决方案1】:

我无法修复它,但通过使用AVAudioPlayer(),我找到了一个很好的解决方法。谢谢大家对我的支持!

import SpriteKit
import AVFoundation

class GameScene: SKScene {    
    var audioPlayer = AVAudioPlayer()  

    override func didMoveToView(view: SKView) {        
        initAudioPlayer("gameloop.mp3")        
    }

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {        
        _ = touches.first as UITouch!
        for _ in touches {
            toggleBackgroundMusic()            
        }        
    }

    func initAudioPlayer(filename: String) {        
        let url = NSBundle.mainBundle().URLForResource(filename, withExtension: nil)
        guard let newURL = url else {
            print("Could not find file: \(filename)")
            return
        }
        do {
            audioPlayer = try AVAudioPlayer(contentsOfURL: newURL)
            audioPlayer.numberOfLoops = -1
            audioPlayer.prepareToPlay()
            audioPlayer.play()
        } catch let error as NSError {
            print(error.description)
        }        
    }

    func toggleBackgroundMusic() {        
        if audioPlayer.playing {
            audioPlayer.pause()
        } else {
            audioPlayer.play()
        }        
    }    
}

【讨论】:

    【解决方案2】:

    SKScene 有一个名为 audioEngine 的属性,该属性在某些情况下会停止。例如,如果您使用 ReplayKit,在 RPPreviewController 中播放录制的游戏影片会劫持音频并停止它,因此当您关闭它时, audioEngine 不再运行。

    我遇到了这个问题,通过简单的检查并再次启动 audioEngine 解决了这个问题。试试这个:

    if !self.audioEngine.isRunning {
        do {
            try self.audioEngine.start()
        } catch {
            //handle error
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-12-30
      • 2013-05-01
      • 2011-04-17
      • 1970-01-01
      • 2013-01-07
      • 2014-02-06
      • 1970-01-01
      • 2013-11-11
      • 2014-06-02
      相关资源
      最近更新 更多