Sound for Scene Transition, that doesn't stutt

2019-07-11 06:42发布

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条回答
聊天终结者
2楼-- · 2019-07-11 07:04

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.

查看更多
登录 后发表回答