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

153
Views
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 answers
Answer question

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 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!