Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

154
Vistas
how do I stop playing audio using the same button

My code plays an audio when the button is clicked but It does not pause when I click it again.

I want to use the same button to play and pause the audio using js.

const rollSound = new Audio

("sounds/short-guided-meditation.ogg");
document.getElementById('play').addEventListener("click", e => rollSound.play());

any help would be highly appreciated.

Edit: I still need an answer. Please share your knowledge.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

As is described at MDN HTMLMediaElement documentation, you can check whether is Media element .paused and decide if you would like to play() or stop(). Something like this:

document.getElementById('play').addEventListener("click", e => rollSound.paused ? rollSound.play() : rollSound.pause());

Snippet example:

const rollSound = document.getElementById('rollSound');
const btn = document.getElementById('play');

btn.addEventListener("click", e => rollSound.paused ? rollSound.play() : rollSound.pause());
<audio id=rollSound loop>
  <source src="https://actions.google.com/sounds/v1/alarms/digital_watch_alarm_long.ogg" type="audio/ogg">
</audio>
<button id=play>play/stop</button>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda