jquery – HTML5 audio autoplay not working in chrome-ThrowExceptions

Exception or error:

I have html5 audio like this in my page

<audio src="assets/media/bgaudio.mp3" id="audio" autoplay type="audio/mpeg"></audio>

Here is jQuery code.

$(document).ready(function() {
    $("#audio").get(0).play();
});

But getting an error when refresh the page.

Uncaught (in promise) DOMException: play() failed because the user didn’t interact with the document first. https://goo.gl /xX8pDD

Any idea to fix the issue?

How to solve:

Just do that

<audio controls autoplay>
  <source src="assets/media/bgaudio.mp3" type="audio/mpeg">  
</audio>

Leave a Reply

Your email address will not be published. Required fields are marked *