Sound reload on HTML5

I am trying to better manage sound objects in memory by pooling, etc for HTML5 target via the Sound class.

var snd:Sound = new Sound();
snd.addEventListener( Event.COMPLETE, onLoaded );
snd.load( new URLRequest( “mysound.m4a” );

…load it, play sound and sometime later

snd.close();
snd = new Sound();
snd.load( new URLRequest( “mysound.m4a” ) ); //same URL path as before

But the second time I try to access the same sound object, nothing plays. Looking at the openfl.media.Sound class, I see it checks if the sound exists in the library. If it doesn’t, it passes a load call to SoundJS. If it does already exist, it triggers loadComplete automatically. On Sound.close() it appears to removes it from the library.

But my trace statements inside Sound indicate that __soundID does exist after close() is called (ie, not being removed from the library).

Any help would be awesome! I have hundreds of audio files in my game and I don’t want them ALL to be inevitably loaded in the browser cache if the user plays the whole thing in one sitting.

Thanks!

I think a lot of games don’t call .close(), and loads only one time. Perhaps there’s something more we should do to ensure that .close() behaves properly?

Well, here’s what I would expect from the close() function:

  • Remove sound asset from any management system (openfl, soundjs, etc) so they don’t think it is already loaded and can load again in the future.
  • Unload so the file is not hogging any memory.

I am not sure if soundjs has some kind of pooling or management inherent to it or if it continually loads new sound objects and keeps them around. Even some kind of pooling option if we can’t unload for some reason would be great.

The concern I have is just that on previous projects (granted, I was using openfl1 with no soundjs), I have seen memory leaks over time with each new sound that played, which eventually caused iOS to shut down the browser.

I found this inside Sound.js::registerSound(), so maybe it suggests the problem with reloading is there…

// OJR note this will disallow reloading a sound if loading fails or the source changes
loader.on(“complete”, createjs.proxy(this._handleLoadComplete, this));


This may be deserving of a separate thread, but a few easy updates to Sound.hx that seem useful:

New alternate extension:

Sound.hx::init():441 SoundJS.alternateExtensions = [ “ogg”, “mp3”, “wav” ];

//add m4a alt
SoundJS.alternateExtensions = [ “ogg”, “m4a”, “mp3”, “wav” ];

More error reporting when SoundJS fails:

Sound.hx::SoundJS_onFileError():497 dispatchEvent (new IOErrorEvent(IOErrorEvent.IO_ERROR));

//pass event info through…
var ioe:IOErrorEvent = new IOErrorEvent(IOErrorEvent.IO_ERROR);
ioe.text = “Error loading sound…\nid=” + event.id + “\nsrc=” + event.src + “\ndata=” + event.data;
dispatchEvent (ioe);

I have the same situation, but with libraries. After loading a lib with sounds, then using close on them, unloading the library and reloading it, the sounds previously unloaded won’t play again.

This is due cache remaining uncleared for OpenFL (during reinitialization of sounds, which were unloaded before, OpenFL decides they are still present in the cache). I posted a fix here: Unload asset library