Uncaught (in promise) DOMException: The play() request was interrupted by a call to pause()

Hi everyone,

I gave a go to create a piano with HTML,CSS and js and I’ve wasted hours trying to figure out why my code (in /localhost:8000/) wasn’t working and finally using a different browser than chrome…all sorted, piano is playing using the keyboard as I want! :rage:

following my code:

function playSound(e) {
  const audio = document.querySelector(`audio[data-key="${e.keyCode}"]`);
  if (!audio) return;
  audio.pause();
  audio.currentTime = 0.5;
  audio.play();
}
document.addEventListener("keydown", playSound);

on Chrome the error displayed in the console is : “Uncaught (in promise) DOMException: The play() request was interrupted by a call to pause().” After reading here and there I still struggling to understand how to sort it in chrome. Anyone willing to help me out?

thanks!!

It’s very difficult to tell from just this snippet, but I assume playSound has been called, which causes it to play and that just plays, but then another call to playSound happens and it then tries to run pause but falls over because you’re trying to interrupt one audio instance with with another.

1 Like

Thanks for this Dan…I left that project on the side for the moment as I figured out I need to really learn more about promises etc…but I’ll keep in mind your tip for when that time will come! cheers mate

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.