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

编程入门 行业动态 更新时间:2024-10-24 22:18:26
本文介绍了插入/拔出耳机时,SKAudioNode()崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在游戏中使用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()崩溃

本文发布于:2023-11-05 04:07:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1559853.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:耳机   SKAudioNode

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!