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

286
Views
How to play and stop audio whenever we click on button

I have a button in which onClick method is attached which will play a vibaration sound.

Button.js

import React from "react";
import "./button.css";

export default function App() {
  let audio = new Audio("/vibrate.mp3");

  const start = () => {
    audio.play();
  };

  return (
    <button className="btn" onClick={start}>
      Click Me
    </button>
  );
}

Vibration sound is playing whenever we click on button. But if I click button again then it is not playing vibration sound because previous sound is not completed yet.
I want to stop the sound (if playing) and play from start if we click on button.

You can check my sandbox .

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

0

I found a solution to stop the audio...

import React from "react";
import "./button.css";

export default function App() {
  let audio = new Audio("/vibrate.mp3");

  const start = () => {
    if(audio.paused === true){
      audio.play();
    }else{
      audio.pause();
      audio.currentTime = 0;
      audio.play();
    }
    
  };

  return (
    <button className="btn" onClick={start}>
      Click Me
    </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!