Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

157
Views
¿Cómo dejo de reproducir audio usando el mismo botón?

Mi código reproduce un audio cuando se hace clic en el botón, pero no se detiene cuando lo vuelvo a hacer clic.

Quiero usar el mismo botón para reproducir y pausar el audio usando js.

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

Cualquier ayuda sería muy apreciada.

Editar: todavía necesito una respuesta. Por favor comparta su conocimiento.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Como se describe en la documentación de MDN HTMLMediaElement , puede verificar si el elemento multimedia está en .paused y decidir si desea play() o stop() . Algo como esto:

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

Ejemplo de fragmento:

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!