xxx xxx - 1 year ago 3799
HTML Question

How to prevent "The play() request was interrupted by a call to pause()" error?

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.currentTime = 0;;

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!

Answer Source

I have encountered this issue recently as well - this could be a race condition between 2 promises. It looks like there is a reference to this issue, or something related here.

Both play() and pause() return promises (see here for the API), so you might be able to call one after the promise is resolved, like so:

n.pause().then(function() {
  n.currentTime = 0;;
}, function() {
 // Handle any pause() failures.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download