Asynchronous sound loading


The game I’m working allows you to choose different ambient sounds from a list. When a song is chosen, a new AudioNode is created using the associated sound file and then played.

Unfortunately, this causes the whole game to freeze while the sound file gets loaded.

I was wondering if jME3 supported asynchronous loading when creating new AudioNodes.

i believe you can stream sound, instead of loading it all into memory before playing it

look at

public AudioNode(AssetManager assetManager, String name, boolean stream, boolean streamCache)

really all is explained in the javadoc

1 Like

Haha, I completely skipped a beat there.

I’m trying to use this constructor:

public AudioNode(AudioRenderer audioRenderer,

AssetManager assetManager,

java.lang.String name,

boolean stream,

boolean streamCache)

However, even when using “true” for the stream and streamCache variable causes only the first half-second to loop when using “audioNode.setLooping(true)”. Since I’m using the music as environmental sound, I need it to be continuous.

The javadoc states:

"streamCache - If stream is also true, then this specifies if the stream cache is used. When enabled, the audio stream will be read entirely but not decoded, allowing features such as seeking, looping and determining duration."

But it doesn't appear to be the case. Any workaround this problem?

Mythruna has a lot of long ambient audio that must also loop… so I use a stream and then watch the node to see when it has stopped and then remove it and create and play a new one.

streamCache might be ok for looping smaller sounds (if it worked) but then you probably wouldn’t be worried about streaming them either.


Another small question: Does the status of the node automatically go to “Stopped” once it’s done streaming the sound file? If not, how do you check if the node is inactive?

Yeah. In my case, when getStatus() != Status.Playing then I recreate the node.