Music on the site page

0

I'd like to know a way to put music running in the background when the site page loads automatically without the user having to use a player for the song to start, I looked in the W3School documentation and the example quoted there is the code below:

<audio controls>
  <source src="horse.ogg" type="audio/ogg">
  <source src="horse.mp3" type="audio/mpeg">
Your browser does not support the audio element.
</audio>

I would like it to be automatically when I load the page the song starts playing.

    
asked by anonymous 23.10.2017 / 01:54

2 answers

0

To make the audio start automatically, just use the autoplay attribute. If you do not want a player to look like this, just remove the controls attribute:

<audio autoplay> <!--agora com autoplay e sem controls-->
  <source src="horse.ogg" type="audio/ogg">
  <source src="horse.mp3" type="audio/mpeg">
  Your browser does not support the audio tag.
</audio>

If you want you can also use the loop attribute to make the song return to the beginning when it finishes.

Example to work:

<audio autoplay>
  <source src="https://freesound.org/data/previews/367/367496_4654185-lq.mp3"type="audio/mpeg">
  Your browser does not support the audio tag.
</audio>

Documentation for the <audio> tag

    
23.10.2017 / 01:57
0

//script
var x = document.getElementById("musica"); 
x.play(); 
<!-- HTML-->
<audio id="musica">
  <source src="horse.ogg" type="audio/ogg">
  <source src="https://s3.amazonaws.com/Syntaxxx/bigger-picture.mp3"type="audio/mpeg">
Your browser does not support the audio element.
</audio>
    
23.10.2017 / 02:50