场景转换的声音,不会口吃

Sound for Scene Transition, that doesn't stutter

在 SpriteKit 中(对于那些不熟悉它的人)有一种方法可以加载和卸载场景,以及它们之间的过渡(视觉)。

我正在尝试在场景之间播放声音,当它们转换时...不会卡顿。

到目前为止,我尝试过的所有方法要么没有声音,要么声音断断续续,即使使用声音管理器也是如此:

import AVFoundation
import SpriteKit

open class SoundManager {

    static let soundStart = SKAction.playSoundFileNamed("ActionBeep_AP1.7", waitForCompletion: true)


    static var coinSound = NSURL(fileURLWithPath:Bundle.main.path(forResource: "ActionBeep_AP1.7", ofType: "wav")!)
    static var audioPlayer = AVAudioPlayer()
    open static func playCoinSound(){
        guard let soundToPlay = try? AVAudioPlayer(contentsOf: coinSound as URL) else {
            fatalError("Failed to initialize the audio player with asset: \(coinSound)")
        }
        soundToPlay.prepareToPlay()
        self.audioPlayer = soundToPlay
        self.audioPlayer.play()
    }


}

任何人都成功地使场景转换听起来 运行 流畅?我意识到在场景转换期间会发生很多事情,这可能对声音引擎没有帮助。但是想想肯定有办法在场景转换的时候播放清晰的声音。

是的,我尝试过 .caf 和 .mp3 以及 .wav 文件,所有文件都具有不同的 "compression" 和原始状态。我认为问题出在我播放声音的方式上,而不是文件类型。

正如crashoverride777所说,加载声音管理器时需要更改。您可以在 didMoveToView() 函数中设置一个线程,以最大限度地减少加载时间:

DispatchQueue.global(qos: .userInitiated).async {
  // Load your sound stuff
}

现在,您可以随时加载声音文件,无延迟。