views:

268

answers:

3

I've been playing with HTML5 audio recently, and though I can get it to play the sound it only ever will play once. No matter what I try (setting the properties, event handlers, etc) I can't seem to get it to loop.

Here's the basic code I'm using:

//myAudio is declared at a global scope, so it doesn't get garbage collected.
myAudio = new Audio('someSound.ogg');
myAudio.loop = true;
myAudio.play();

I'm testing using Chrome (6.0.466.0 dev) and Firefox (4 beta 1), both of which seem happy to ignore my requests for looping. Any ideas?

+4  A: 

While loop is specified, it is not implemented in any browser I am aware of Firefox [thanks Anurag for pointing this out]. Here is an alternate way of looping that should work in HTML5 capable browsers:

myAudio = new Audio('someSound.ogg'); 
myAudio.addEventListener('ended', function() {
    this.currentTime = 0;
    this.play();
}, false);
myAudio.play();
kingjeffrey
Hm... interesting. I didn't have much luck with this method previously but I wasn't setting the current time. When I try this, however I get: "Uncaught Error: INDEX_SIZE_ERR: DOM Exception 1" on the line "this.currentTime = 0"
Toji
@TojiHere is the documentation for the [INDEX_SIZR_ERR error](http://www.whatwg.org/specs/web-apps/current-work/multipage/video.html#offsets-into-the-media-resource). It means the beginning of the song is outside the range the browser can seek. This sometimes occurs if the browser thinks it is streaming media, or if the server lacks certain capabilities (I forget offhand what this specific capability is, but I will try to track it down).You can also use `this.startTime` to return to the earliest available time.
kingjeffrey
@Toji The reason `.currentTime` is needed has to do with how the spec handles the end of a song. Browsers are supposed to just pause the song. So reinitiating play does so at the end of the song. The playhead must be reset.
kingjeffrey
@Toji Here is another thought if you are unable to get `.currentTime` working. Inside the callback function, you could simply redeclare `myAudio` with the same file and play it again. Redeclaring it should also reset the playhead to the beginning of the song.
kingjeffrey
setting it to this.startTime gives me the same error, though now I'm starting to wonder if it's a server issue. I'm serving static files from a local django server (which is not django's most robust feature). Pair that with the fact that Anurag's does work and I think it's probably just me. I'll toy with it a bit more and let you know the results.
Toji
Whoops! Forgot about this question! The core issue was that I was using a local Django test server to serve up my media files, and apparently it's not too happy about providing streaming information. Uploading and trying scripts like the one listed here to my site's server allows looping.
Toji
A: 

Just start it once it's ended:

myAudio.addEventListener("ended", function(e){myAudio.play();}, false);
mattbasta
+4  A: 

Your code works for me on Chrome (5.0.375), and Safari (5.0). Doesn't loop on Firefox (3.6).

See example.

var song = new Audio("file");
song.loop = true;
document.body.appendChild(song);​
Anurag
+1 Very cool. I was unaware `loop` was implemented in webkit.
kingjeffrey
+1, you can also detect if `loop` is supported, e.g. by: `typeof new Audio().loop == 'boolean';`
CMS