场景转换的声音,不会结巴

时间:2022-02-15 21:13:13

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

在SpriteKit中(对于不熟悉它的人来说)有一种加载和卸载场景的方式,以及它们之间的转换(视觉)。

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.

是的,我尝试过。caf和。mp3和。wav文件,都有不同的“压缩”和原始状态。我认为问题在于我如何播放声音,而不是文件类型。

1 个解决方案

#1


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:

正如crashoverride777所言,在加载声音管理器时需要进行更改。您可以在didMoveToView()函数中设置一个线程来最小化加载时间:

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

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

现在,当你需要它的时候,你的声音文件就会被加载。

#1


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:

正如crashoverride777所言,在加载声音管理器时需要进行更改。您可以在didMoveToView()函数中设置一个线程来最小化加载时间:

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

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

现在,当你需要它的时候,你的声音文件就会被加载。