Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Playing audio after the page loads in html

Tags:

html

I inserted the audio in html. But the audio gets started before the entire page loads. I want the audio to be played after the entire page gets loaded.

<audio src="bg.mp3" autoplay="autoplay" loop="loop"></audio>

Can anyone help me in this.

like image 970
Priya Sunanthan Avatar asked Jan 16 '13 11:01

Priya Sunanthan


People also ask

How do I get HTML to automatically play audio?

The HTML <audio> autoplay attribute is used to specify that the audio should automatically start playing as soon as it is loaded. It is a Boolean attribute.

How do I autoplay audio in HTML Chrome?

Just add an invisible iframe with an . mp3 as its source and allow="autoplay" before the audio element. As a result, the browser is tricked into starting any subsequent audio file.

How do I autoplay audio in HTML without controls?

The audio tag allows you to embed audio content in your HTML pages. By default the browser does not show any controls for this element. Which means the audio will play only if set to autoplay (more on this later) and the user can't see how to stop it, or control the volume or move through the track.

How do I preload audio in HTML?

We use preload=”auto” attribute to preload the audio file. The HTML Audio Preload Attribute is used to specify how the author thinks the audio should be loaded when the page loads. The audio preload attribute allows the author to indicate to the browser how the user experience of a website should be implemented.


2 Answers

You're going to need JavaScript for that. Remove the autoplay attribute:

<audio id="my_audio" src="bg.mp3" loop="loop"></audio>

and add a script like this:

window.onload = function() {
    document.getElementById("my_audio").play();
}

Or if you use jQuery:

$(document).ready(function() {
    $("#my_audio").get(0).play();
});
like image 191
Aioros Avatar answered Oct 28 '22 17:10

Aioros


Just Copy and Paste this Code in Body section of your HTML Code.

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

and make sure that your audio file should be in same folder.

like image 45
Rajesh Avatar answered Oct 28 '22 15:10

Rajesh