2

I keep getting this error that says "DOMException: play() failed because the user didn't interact with the document first." but, I don't know what to do. Any suggestions?

<script>
var audioElement = new Audio();
audioElement.setTrack("assets/music/bensound-acousticbreeze.mp3");
audioElement.audio.play();

</script>
Neatlent
  • 21
  • 1
  • 1
  • 2
  • You have to mute the element to play it automatically without user interaction. https://stackoverflow.com/questions/49930680/how-to-handle-uncaught-in-promise-domexception-play-failed-because-the-use – Liftoff Sep 15 '21 at 16:57

1 Answers1

6

Hi I'm completely new here, but I seem to have found the answer to this.

That error means that in newest version of Chrome sounds are not allowed to play until the user interacts with the webpage.

So all I did (having had the same issue and stumbling upon your question)

I simply attached the sound to a click (as its my intended design anyway), but if you want to play some music as intro you might need to introduce like a "start" button before that menu or otherwise a check that plays the sound when user interacts with the website.

Sorry that I'm not super technical about this, I am learning also and I hope this helps.

https://gamedev.stackexchange.com/questions/163365/html5-games-play-failed-because-the-user-didnt-interact-with-the-document-fi

If it helps here's a video of how I solved my problem:

https://recordit.co/iMmQ0BCbyN

In the video the sound finally plays when I click but would throw an error when I try to play it on load...

Cheers