Play sound after loading page in html

I have inserted audio in html. But the sound starts before the entire page loads. I want the sound to play after loading the entire page.

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

Can anyone help me on this.

+6
source share
4 answers

You will need JavaScript for this. Remove the autoplay attribute:

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

and add the script as follows:

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

Or if you use jQuery:

 $(document).ready(function() { $("#my_audio").get(0).play(); }); 
+9
source

Just copy and paste this code into the body section of your HTML code.

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

and make sure your audio file is in the same folder.

+8
source

I think you should use JS (jQuery) to get the finished document

$ (document) .ready (function ()

Play an audio file using jQuery when a button is clicked

You have an answer here (if I follow you correctly)

+1
source

To re-tone, set the code below

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

and delete the loop at a time. Then it should be like

  <audio src="./audio/preloader.mp3" autoplay="autoplay"></audio> 
-1
source

All Articles