0

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?

KelvinDV
  • 1
  • 1

2 Answers2

0

You need to put the preload attribute to make it work on chrome. Also keep in mind that a user must interact with the domain for unmuted autoplay to work.

<audio controls autoplay preload>
  <source src="assets/media/bgaudio.mp3" type="audio/mpeg">  
</audio>
Yondev
  • 1
-1

Just do that

<audio controls autoplay>
  <source src="assets/media/bgaudio.mp3" type="audio/mpeg">  
</audio>
Lalit Kumar
  • 256
  • 2
  • 14