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?
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.