Louis
Louis

Reputation: 4210

HTML5 Audio Plays Randomly

I am implementing sound effects in HTML5 audio but after a while, it just stops playing any audio. The file type is correct because it starts fine but then seems to give up.

var sound = new Audio(url);
function play() {        
    sound.play();
}

Is there a better way to do this so it consistently plays sound?

Here is a link to my implementation. Easy to reproduce by pressing spacebar a lot until it eventually gives up (also shoot the lights for added sounds). http://craftyjs.com/elevatoraction/

This occurs for me in the latest version of Chrome (8.0)

Edit: I did as Gaurav suggested and only played the same instance of each sound file, but the same sort of problems are present. It will arbitrarily stop playing.

Edit 2: I just noticed that whenever I try to play the sound, the networkState is always 1 which according to this means it hasn't fully loaded. That is odd seeing as it still plays sometimes and even when it plays the networkState is always 1

Upvotes: 11

Views: 1147

Answers (1)

Gaurav
Gaurav

Reputation: 12796

Don't create a new audio object each time you want to play a sound, reuse the same resource.

var sound1 = new Audio(url);
function playSound1() {
    sound1.play();
}

I think this is related to the bug http://code.google.com/p/chromium/issues/detail?id=57070

Upvotes: 6

Related Questions