Audio HTML5 element playing only once with Javascript

I was trying to play a sound with javascript by using <audio> html5 tag. The sound played fine except it played only once. The solution was quite difficult to find.

In order to play a sound with javascript in Google Chrome, the audio file needs to be fetched from a server that supports Content-Range HTTP header. Otherwise, the browser won’t be able to determine the length of the sound and resetting audio tag’s currentTime property won’t work: it will always show the length of the sound, once played. The property behaved as if it were read-only. Calling .play() method again on the object won’t generate any sounds.

I got this problem because my simple web server in node.js didn’t support Content-Range header. Moving the audio (mp3, ogg, wav) files to an external web server (Apache or nginx) solved the problem and Chrome was able to play and replay the sounds.

A simple example:

<script>
var a = new Audio();
a.src = 'url_of_the_file_to_play.ogg';
a.play();
</script>
Ten wpis został opublikowany w kategorii Internet. Dodaj zakładkę do bezpośredniego odnośnika.

Dodaj komentarz

Twój adres e-mail nie zostanie opublikowany. Pola, których wypełnienie jest wymagane, są oznaczone symbolem *

*

Możesz użyć następujących tagów oraz atrybutów HTML-a: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>