Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 202
Charlie Avatar asked Aug 15 '12 16:08

Charlie


People also ask

How to fix Windows 10 audio delay and glitch problems?

Repairing glitches and delays in Windows 10 audio playback is actually quite simple once the problem has been diagnosed. To begin, right-click on the audio icon in the taskbar and select “Playback Devices”: Double-click on your primary audio device to bring up the speaker/headphones properties dialog, and navigate to the “Advanced” tab:

How to play audio after 8 seconds in JavaScript?

This is the main function to play audio after 8 seconds. To check if your function is working or not you can use console log. This line will add a console message when the audio starts playing. Just put your audio path in the src attribute. loop will play the audio file repeatedly.

Why is my audio not working on Windows 10?

A common problem that faces some users of Windows 10 is delays in the audio output. This guide features a number of common issues faced when playing audio in Windows 10 and a possible solution. This problem can occur with different hardware, but many reports of these issues have been reported with Realtek audio chips.

How to add a console message when audio starts playing?

This line will add a console message when the audio starts playing. Just put your audio path in the src attribute. loop will play the audio file repeatedly.


2 Answers

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>
like image 90
Ian Routledge Avatar answered Oct 03 '22 01:10

Ian Routledge


Javascript only method:

var sound = document.createElement('audio')
sound.id = 'audio'
sound.controls = 'controls'
sound.src = 'http://a.tumblr.com/tumblr_leltkjNwWL1qf32t9o1.mp3'
sound.type = 'audio/mp3'
document.body.appendChild(sound)

function playAudio() {
  document.getElementById('audio').play();
}

setTimeout("playAudio()", 3000);
like image 37
Philip Kirkbride Avatar answered Oct 03 '22 02:10

Philip Kirkbride