I made a website where if the user clicks, it plays a sound. To prevent the sound from overlapping, I had to add the code:
n.pause();
n.currentTime = 0;
n.play();
But that causes the error:
The play() request was interrupted by a call to pause()
To come up each time the sound event is triggered right after another trigger. The sounds still plays fine, but I want to prevent this error message constantly popping up. Any ideas?
Thanks in advance!
try it
This piece of code fixed for me!
Modified code of @JohnnyCoder
HTML:
JS:
It looks like a lot of programmers encountered this problem. a solution should be quite simple. media element return
Promise
from actions soshould do the trick
here is a solution from googler blog:
I have the same issue, finally i solve by:
When you see an error with
Uncaught (in promise)
This just means that you need to handle the promise with a.catch()
In this case,.play()
returns a promise. You can decide if you want to log a message, run some code, or do nothing, but as long as you have the.catch()
the error will go away.