Play javascript audio automatically

Asked

Viewed 1,163 times

1

I have this script, that when I click it plays the audio:

<!DOCTYPE html>
<html>

<div id="oi" onclick="reproduzir('audio01.mp3')">ME CLIQUE</div>

<audio id="audio">
    <source src="" type="audio/mpeg">
    Seu navegador não possui suporte ao elemento audio
</audio>

</html>

<script>
    audio = document.getElementById('audio');
    function reproduzir(urlMusica){
        audio.src = urlMusica;
        audio.play();

    }   

</script>

and wanted it to reproduce itself when I use the "onload" I have tried several ways, but not right. someone could give me a light?

  • You don’t need Js for this just set the tag audio the property self-play.

2 answers

0

  • It didn’t work, you tested there?

  • I tried that way and it worked, but without Js &#xA;<audio id="audio" autoplay>&#xA; <source src="audio01.mp3" type="audio/mpeg">&#xA; Seu navegador não possui suporte ao elemento audio&#xA;</audio>&#xA;

-1

  • Thank you for clearing that doubt

Browser other questions tagged

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