Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Triggering an HTML5 audio track to play whenever it has loaded

I'm trying to play an HTML5 audio track a few seconds after the page has loaded using the .play() JavaScript function.

Sometimes, when the audio loads slowly, and .play() is triggered when the player looks like this: enter image description here

The audio does not play when it is buffered.

Sometimes, when the audio loads quickly and the player looks like this, .play() works fine.

enter image description here

What is the quickest way around this issue? (Using a listener? I holding out for a 'play when loaded' function).

like image 604
KemanoThief Avatar asked Jul 05 '11 10:07

KemanoThief


1 Answers

This should be working:

<script language="JavaScript" type="text/javascript">
    var myAudio = document.getElementById('audio1')
    myAudio.oncanplaythrough = function () {this.play();}
</script>

<audio id="audio1" controls preload="auto" />
<audio id="audio2" controls preload="auto" oncanplaythrough="this.play();" />
like image 182
Lightnin' Myers Avatar answered Oct 11 '22 14:10

Lightnin' Myers