Johannes
Johannes

Reputation: 177

HTML5 audio stops working after a while (leak)

So im working on a HTML5 / javascript rts game. Obv there are several sounds playing all the time. So what happens for me is after a while the sounds kinda "crashes" and all sound from this browser tab stops working. I can only bring back sound by restarting the browser, restarting the tab (= reloading the game) doesnt fix it. This is what i use to play sounds:

// play sound
soundToPlay.load();
soundToPlay.play();
soundToPlay.volume = volume;

Im on win7 and FF. What also happens when i play the game is that the windows process "audiodg" increases in memory usage, and when the sound stops working its usually up at something like 2,5 gb. When i close the browser, most of the time it will go back down to a couple of MB, where it should be normally. This audiodg thing is a known bug (not with my game, but in general), but i do not havy any issues with audiodg outside of my game. Most users of my game seem to not have this problem, only relatively few, but im definetly not the only one, also its definetly not a FF thing only, ive seen it happen on chrome, too.

Upvotes: 0

Views: 643

Answers (1)

Todd
Todd

Reputation: 5454

so, my guess is that you're creating the new Audio(filename) every time it must be played.

so, even creating these with javascript, you end up with an HTML template anyway.

var a = new Audio('file.mp3');
// <audio preload=​"auto">​</audio>​

In light of that you should call new Audio(filename) only once, and store it in a variable that can be referenced when the sound should play:

var clone = $(a).clone()[0];
clone.play();
clone.volume = volume;
clone.onended = function() {
    $(clone).remove();   
}

Upvotes: 0

Related Questions