Max
Max

Reputation: 2749

SceneKit - Audio causes cxa_throw with a lag the first time I play a sound

I play back a sound like this ( this is inside a SCNNode subclass ):

    let audioSource = SCNAudioSource(named: "coin.wav")
    let audioPlayer = SCNAudioPlayer(source: audioSource)
    self.addAudioPlayer(audioPlayer)

The first time this is called, I get a severe lag and an expection is thrown. I notice the lag, when I disable the All_Expection_Breakpoint.

What can I do against this?

enter image description here

Upvotes: 1

Views: 675

Answers (2)

Sebastien Metrot
Sebastien Metrot

Reputation: 124

The C++ exception comes from AVAudioEngine that is used by the SceneKit audio layer. The AVAudio* framework uses C++ exceptions internally so if you have a breakpoint set in Xcode to break when C++ exceptions are thrown Xcode will break a lot in the AVAudio* code (mostly at init times). You can safely ignore these as they are caught by the framework before they reach your code anyway.

If you don't want the lag you can instantiate your audio source and load it at startup time: let audioSource = SCNAudioSource(named: "coin.wav") audioSource.load()

And then add the player when you need it later: let audioPlayer = SCNAudioPlayer(source: audioSource) self.addAudioPlayer(audioPlayer)

By the way, players are cached and recycled so you don't have to worry too much about memory being used for nothing.

Note also that the SCNAction uses exactly the same API than you do, so if you create an action with a sound that hasn't previously been loaded in memory with .load() you will also get a lag.

Hope this helps,

S.

Upvotes: 1

Confused
Confused

Reputation: 6288

This is really only for atmospheric, ambient and background environment sorts of sounds, to be looped and moved with a character or attached to the position of a waterfall or something like that.

It's not a performant, instant sound player for immediate sound effects requiring low latency.

For that you're better off using an SCNAction to play the audio as an Action when needed, or using something like Fmod that's designed for low latency sound playback.

I'm not sure how I know this.

Upvotes: 0

Related Questions