Charlie Charlie - 6 months ago 64
Javascript Question

Have audio tag play after a delay?

I'm playing an MP3 using the

<audio>
tag in HTML5.

<audio controls="controls" autoplay="autoplay">
<source src="song.mp3" type="audio/mp3" />
</audio>


This works fine, but I am wondering how I can put a delay in before the song starts to autoplay? I know there's not a
delay
attribute, but I'm thinking it could be accomplished via javascript?

Answer

Try this, really simple but you should move the JS outside the markup...

<audio controls="controls" onloadeddata="var audioPlayer = this; setTimeout(function() { audioPlayer.play(); }, 8000)">
    <source src="Kalimba.mp3" type="audio/mp3" />
</audio>
Comments