Play/pause keydown event

In this challenge, I want to add a feature that play/pause the sound by pressing space button two times:

  • The first press will play the sound

  • The second press will pause the sound.

I tried to add this functionality, but I couldn’t. I want help to do this functionality.

You can check the audio.paused property (MDN: paused) and either call play or pause depending on if it is true or not.

document.addEventListener("keydown", (e) => {
  if (e.key === " ") {
      audio.paused ? audio.play() : audio.pause();
  }
})
1 Like