import { useState } from "react";
function Music(){
const [pauseToggle, setpauseToggle] = useState(false)
const music = new Audio(require(`./Alan.mp3`));
console.log(music);
let isPlaying = false;
const player = () => {
pauseToggle ? setpauseToggle(false) : setpauseToggle(true);
if(isPlaying){
music.pause();
isPlaying = false;
}else{
music.play();
isPlaying = true;
}
}
return(
<div>
<button onClick={player}>{pauseToggle?"=":">"}</button>
</div>
)
}
export default Music;
I'm unable to pause the audio. IInstead it gets played twice when I try to pause.
I tried to get help from this Unable to pause audio in Reactjs but it didn't help.
Any other solution for pausing audio file?