Tim*_*mme 21 audio lag avaudioplayer ios swift
在Swift中编写的SpriteKit iOS游戏中,播放非常短的声音(~0.5s)会产生打嗝(如滞后).在其他问题中,我读到了我应该prepareToPlay()发出的声音.
我甚至使用变量(soundReady)来检查声音是否在播放前准备好了.每当完成播放(audioPlayerDidFinishPlaying())时我也会重新准备声音.以下是代码的相关部分:
class GameScene: SKScene, AVAudioPlayerDelegate {
var splashSound = NSURL()
var audioPlayer = AVAudioPlayer()
var soundReady = false
override func didMoveToView(view: SKView) {
let path = NSBundle.mainBundle().pathForResource("plopSound", ofType: "m4a")
splashSound = NSURL(fileURLWithPath: path)
audioPlayer = AVAudioPlayer(contentsOfURL: splashSound, error: nil)
audioPlayer.delegate = self
soundReady = audioPlayer.prepareToPlay()
}
func playSound(){
if(soundReady){
audioPlayer.play()
soundReady = false
}
}
func audioPlayerDidFinishPlaying(player: AVAudioPlayer!, successfully flag: Bool){
//Prepare to play after Sound finished playing
soundReady = audioPlayer.prepareToPlay()
}
}
Run Code Online (Sandbox Code Playgroud)
我不知道我在哪里出错了.我觉得我已经尝试了所有东西(包括但不限于:只准备一次,在播放后立即准备,不使用变量,而只是prepareToPlay()).
附加信息:
bri*_*ric 15
我遇到了同样的问题并在backgroundQueue中播放了声音.
这是一个很好的例子:https://stackoverflow.com/a/25070476/586204.
let qualityOfServiceClass = QOS_CLASS_BACKGROUND
let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass, 0)
dispatch_async(backgroundQueue, {
audioPlayer.play()
})
Run Code Online (Sandbox Code Playgroud)
小智 7
只需从@brilliantairic添加Swift 3版本的解决方案.
DispatchQueue.global().async {
audioPlayer.play()
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4751 次 |
| 最近记录: |