I'm making a game, and I want sounds to play on events occurring. I have currently got an object that looks like below:
var soundObject = {
invaderShoot: function() {
var audio = new Audio("laser.wav");
audio.play();
},
//Many more of the same method
};
And then I'm playing the sounds like this:
soundObject.invaderShoot();
However, when I try to do this, it comes up with the following error:
Unhandled Promise Rejection: NotAllowedError: The request is not allowed by the user agent or the platform in the current context, possibly because the user denied permission.
And it comes up highlighting this line in the method:
audio.play();
What is the problem? I have searched through GitHub threads and Stack Overflow questions, but I cannot find a definition of what the error is, let alone how to fix it.
How should I fix this?
I am only permitted to use .wav files, because that is the only file format that my code hosting service allows.