1
I’m trying to make a sound run as soon as the page loads.
I open the browser (Firefox or Chrome) Page access, but the sound doesn’t play. I can only hear the sound after I click anywhere on the page.
The audio is 15 seconds long. After I click anywhere on the page, I can hear, with a few seconds in advance. It’s like it’s already running but the sound only came out when I click on the page.
function execSom() {
var playAudio = document.getElementById("playAudio");
playAudio.play();
}
<body onload="execSom()">
<audio style="visibility:hidden" id='playAudio'>
<source src="https://unicorn-meta-zoo.github.io/episodes/moderation.mp3" type="audio/mpeg">
</audio>
Just to make a question, is your execSom() function inside the <script></script> tag? If so, try to put the "autoplay" attribute in the <audio> TAG. And also try to put the attribute "loop" for the audio to automatically repeat when it stops playing.
– Gato de Schrödinger
Try changing the tag
<script>
from within the<body>
to the<head>
, or otherwise if you are already in<head>
.– Breno