1

I'm making a game for a school project and want to add music to the login screen. When my page loads, I get the error Uncaught (in promise) DOMException: play() failed because the user didn't interact with the document first.

This is my function to play the audio

function speelmuziek() {
 var audio = new Audio('sounds/waitsound.mp3');
 audio.play()
}

And I use this in the body of my page to load the function.

<body onload="speelmuziek()">

How can I solve this?

Kevin M. Mansour
  • 2,915
  • 6
  • 18
  • 35
GijsVh
  • 11
  • 2
  • 1
    Check this SO answer: https://stackoverflow.com/questions/49930680/how-to-handle-uncaught-in-promise-domexception-play-failed-because-the-use – Matthew M. May 17 '22 at 14:42

1 Answers1

0

Looks like you need to simulate some user iteraction. Like mute audio -> unmute and then play or something like this

Mariik
  • 135
  • 2
  • 11