Music on the website page

Asked

Viewed 128 times

0

I would like to know a way to put music running in the background when the site page is loaded automatically without the user having to use a player to start the music, I searched in the documentation of the W3school 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 an automatic way when I load the page the music starts playing.

2 answers

0

To make the audio starts automatically just use the attribute autoplay. If you don’t want to look like a player just remove the attribute controls:

<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 attribute loop to get the music back to the beginning when it’s over.

Working example:

<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 label <audio>

  • So Isac, when I put this code it appears a player, and also the music does not even play by clicking on the player play

  • @Vinniciusgomes View my edit. You have the audio file listed in src ? horse.ogg or horse.mp3 ?

  • Has yes <source src=".. /Assets/media/quiz.ogg" type="audio/ogg"> <source src=".. /Assets/media/quiz.mp3" type="audio/mpeg">

  • @Vinniciusgomes See how the example in the answer works. Confirm that you really have the files, and that they are in the correct folder

  • I realized that it was some error in my file because what you put there worked. Thank you Isac, good night.

  • @No problem, good night too.

Show 1 more comment

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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.