Why audio autoplay doesn't work in Google Chrome?

Viewed 12291

I'm trying to autoplay an audio file when page has been just opened. My browser is google chrome updated to last version.. here is the code

<body>

    <iframe class="audio" autoplay="autoplay" controls style = "display:none;">
    <source src="./sound/pacmanbegin.mp3" type="audio/mpeg"/>
    </iframe>

</body>

This doesn't work also in other browser as Internet explorer, edge etc.. Anyone can help me?

2 Answers

Recently many browsers can only autoplay with sound off, so you'll need to add muted attribute to the audio tag, that is not make sense, so in my opinion the best way is adding:

 document.getElementById('audio').play();

after your </audio> tag.
take a look at this code:

 <audio controls style="display:none" id="audio">
  <source src="https://file-examples.com/wp-content/uploads/2017/11/file_example_MP3_700KB.mp3" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
<script>
document.getElementById('audio').play();
</script>

    <audio controls autoplay>  
        <source src="https://www.soundjay.com/button/beep-01a.mp3" type="audio/ogg">  
    </audio>  

try to use this and enjoy ! :D

Related