Sound for Scene Transition, that doesn't stutter

杀马特。学长 韩版系。学妹 提交于 2019-12-13 15:48:47

问题


In SpriteKit (for those unfamiliar with it) there's a way to load and unload scenes, and a transition (visual) between them.

I'm trying to make a sound play between scenes, as they transition... that doesn't stutter.

So far, all the ways I've tried either create no sound, or the sound stutters, even using a sound manager, like this:

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()
    }


}

Anyone had any success making scene transition sounds run smoothly? I realise there's a lot going on during a scene transition, and that's probably not helping the sound engine. But think there must be a way to make a clear sound play during a scene transition.

And yes, I've tried with .caf and .mp3 and .wav files, all with different "compression" and raw states. I think it's how I play the sounds that's the problem, not the file type.


回答1:


As crashoverride777 said, you need to change when you load the sound manager. You could set up a thread in your didMoveToView() function to minimize loading time:

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

Now, you have your sound file loaded for whenever you need it, lag-free.



来源:https://stackoverflow.com/questions/40724292/sound-for-scene-transition-that-doesnt-stutter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!