我正在使用SKAudioNode()在我的游戏中播放背景音乐。我有一个播放/暂停功能,一切正常工作,直到我插入我的耳机。根本没有声音,当我调用暂停/播放函数时,我会得到这个错误
AVAudioPlayerNode.mm:333: Start: required条件为false:_engine->IsRunning() com.apple.coreaudo.avf音频‘,原因是:’必需条件为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
}
}
}
}发布于 2016-02-28 16:27:41
我无法修复它,但是通过使用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()
}
}
}发布于 2017-06-12 12:10:38
SKScene有一个名为audioEngine的属性,在某些情况下会停止。例如,如果您使用的是RPPreviewController ReplayKit,在hi-中播放一部录制好的游戏电影,然后停止播放,所以当您将其排除在外时,audioEngine就不再运行了。
我遇到了这个问题,并通过简单的检查并再次启动audioEngine来解决它。试试这个:
if !self.audioEngine.isRunning {
do {
try self.audioEngine.start()
} catch {
//handle error
}
}https://stackoverflow.com/questions/35662139
复制相似问题