I have an audio setup like this
<html>
<head></head>
<body>
<script>
var audio1 = new Audio("music1.mp3");
document.addEventListener('keydown', function(evt) {
console.log('hm');
audio1.play();
});
audio1.addEventListener("ended", function() {
console.log('end');
audio1.currentTime = 0;
})
</script>
</body>
</html>
I click a key to start the audio and console.log('hm'); to make sure it started but whenever the audio ends the second eventListener does not get called and I do not get the "end" message or a restart. I prefer to keep the audio in javascript and not a tag because I have already created the game and changing the audio ways changes about 45% of the game.
Am I doing things wrong? If so please tell me the proper way to do it!