How to prevent “The play() request was interrupted by a call to pause()” error with the audio element with JavaScript?

Spread the love

Sometimes, we want to prevent "The play() request was interrupted by a call to pause()" error with the audio element with JavaScript

In this article, we’ll look at how to prevent "The play() request was interrupted by a call to pause()" error with the audio element with JavaScript.

How to prevent "The play() request was interrupted by a call to pause()" error with the audio element with JavaScript?

To prevent "The play() request was interrupted by a call to pause()" error with the audio element with JavaScript, we play after a delay after pausing the audio.

For instance, we write

playerMP3.volume = 0;
playerMP3.pause();

setTimeout(() => {
  playerMP3.play();
}, 150);

to call pause to pause the audio.

Then we call play in the setTimeout callback after a 150ms delay to prevent the error.

Conclusion

To prevent "The play() request was interrupted by a call to pause()" error with the audio element with JavaScript, we play after a delay after pausing the audio.

Leave a Reply

Your email address will not be published.