我在游戏中使用SKAudioNode()播放背景音乐.我具有播放/暂停功能,一切正常,直到插入耳机.根本没有声音,当我调用暂停/播放功能时出现此错误
I am using a SKAudioNode() to play background music in my game. I have a play/pause function and everything is working fine until I plug in my headphones. There is no sound at all and when I call the pause/play function I get this error
AVAudioPlayerNode.mm:333:开始:所需条件为假:_engine-> IsRunning() com.apple.coreaudio.avfaudio",原因:所需条件为假:_engine-> IsRunning()
AVAudioPlayerNode.mm:333: Start: required condition is false: _engine->IsRunning() com.apple.coreaudio.avfaudio', reason: 'required condition is false: _engine->IsRunning()
有人知道这意味着什么吗?
Does anyone knows what this means?
代码:
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 } } } }推荐答案
我无法修复它,但是通过使用AVAudioPlayer()我发现了一个好的解决方法.谢谢大家对我的支持!
I was not able fix it, but by using AVAudioPlayer() I found a good workaround. Thank you all for supporting me!
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() } } }更多推荐
插入/拔出耳机时,SKAudioNode()崩溃
发布评论