插入 in/out 耳机时 SKAudioNode() 崩溃
SKAudioNode() crashes when plugging in/out headphones
我正在使用 SKAudioNode()
在我的游戏中播放背景音乐。我有一个 play/pause 函数,在我插入耳机之前一切正常。根本没有声音,当我调用 pause/play 函数时出现此错误
AVAudioPlayerNode.mm:333: Start: required condition is false: _engine->IsRunning()
com.apple.coreaudio.avfaudio', reason: 'required condition is 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
}
}
}
}
我无法修复它,但通过使用 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()
}
}
}
SKScene 有一个名为 audioEngine 的 属性 在某些情况下会停止。例如,如果您使用 ReplayKit,在 RPPreviewController 中播放录制的游戏视频会 hi-jack 音频并停止播放,因此当您关闭时它,audioEngine 不再是 运行。
我遇到了这个问题,通过简单检查并再次启动 audioEngine 解决了这个问题。试试这个:
if !self.audioEngine.isRunning {
do {
try self.audioEngine.start()
} catch {
//handle error
}
}
我正在使用 SKAudioNode()
在我的游戏中播放背景音乐。我有一个 play/pause 函数,在我插入耳机之前一切正常。根本没有声音,当我调用 pause/play 函数时出现此错误
AVAudioPlayerNode.mm:333: Start: required condition is false: _engine->IsRunning() com.apple.coreaudio.avfaudio', reason: 'required condition is 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
}
}
}
}
我无法修复它,但通过使用 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()
}
}
}
SKScene 有一个名为 audioEngine 的 属性 在某些情况下会停止。例如,如果您使用 ReplayKit,在 RPPreviewController 中播放录制的游戏视频会 hi-jack 音频并停止播放,因此当您关闭时它,audioEngine 不再是 运行。
我遇到了这个问题,通过简单检查并再次启动 audioEngine 解决了这个问题。试试这个:
if !self.audioEngine.isRunning {
do {
try self.audioEngine.start()
} catch {
//handle error
}
}