stop
The Stop function stop an audio file.
Example
Stop sound with onClick Event
If you run the play function and then run the stop function, the audio will stop.
When you run the play function again, it will run from the beginning.
When you stop, isPlaying
becomes false
.
If you play again, isPlaying
becomes true
.
import { useAudio } from "react-use-audio";
// You need to add sound-related files
import testSound from "./sounds/test.mp3";
function App() {
const {
data: { isPlaying },
play,
stop,
} = useAudio(testSound);
return (
<>
<p>isPlaying: {isPlaying ? "true" : "false"}</p>
<button onClick={play}>play</button>
<button onClick={stop}>stop</button>
</>
);
}
export default App;