Non-blocking asynchronous asset loading

I’m having trouble loading music files within my app, in a way that doesn’t halt the app for a second or two. My app contains 16 full length music tracks in .ogg format (and more are being proposed).

My target is Android.

My first approach was to pre-cache all the songs when the app initializes. A loop containing something like this:
Assets.getSound("sfx/someSong1.ogg");

Then later calling something like this with useCache enabled to play the song.
var soundChannel:SoundChannel = Assets.getSound("sfx/someSong1.ogg", true).play();

This appeared to work rather well when tested under Neko on my desktop, but the actual Android deployment resulted in about 1/4 of the tracks failing to play. Setting useCache to false (and removing the pre-caching code) fixed this issue, however the app would halt for a couple of second every time I want to play a song.

I’m now trying what I thought might be an asynchronous approach, with:
Assets.loadMusic("sfx/someSong1.ogg", false).onComplete(playSong);

However that too, appears to be blocking.

What’s the best way to load music files in a non-blocking way?

While the Assets.loadSound or loadMusic methods asynchronously fetch the file from disk, it still decodes OGG synchronously.

There is a beta streaming API that we added, but needs additional testing. I wrote about it here:

If it works, we can wire up support internally for openfl.media.Sound

2 Likes

There’s just no easy way around it:   “yes, you do need to load such things asynchronously, but you need to be prepared for the very-real possibility that the asset which you wish to play is not available yet.”   For the first few seconds of the game, you just might not be able to play that sound.   And there simply might not be any way to know in advance, or to “program around it.”   Quite a few people really DO buy cell-phones in a supermarket.

Thanks singmajesty, I’ll give that a go. A streaming option might fit the bill.

Thank you singmajesty, that’s working beautifully on Neko and Android. Much appreciated!

1 Like