插入/拔出耳机时SKAudioNode()崩溃

Che*_*pig 12 sprite-kit swift avaudioengine swift2 skaudionode

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

AVAudioPlayerNode.mm:333:开始:必需条件为false:_engine-> IsRunning()com.apple.coreaudio.avfaudio',原因:'必需条件为false:_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
        } 
    }
}
}
Run Code Online (Sandbox Code Playgroud)

Che*_*pig 2

我无法修复它,但通过使用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()
        }        
    }    
}
Run Code Online (Sandbox Code Playgroud)