I'm building my website and i want a button at the bottom of my page, that will play/pause a song using useSound. It plays when i first click, but then i can't get my song to stop.
Can anyone point me in the right direction? Please see my code below.
import useSound from 'use-sound';
import galaxySfx from '../../public/sounds/galaxy.mp3';
import styles from "./sound.module.scss";
import Aos from "aos";
import "aos/dist/aos.css";
const PlayButton = ({}) => {
const [play, { stop, isPlaying }] = useSound(galaxySfx);
function playSong() {
isPlaying === true;
play();
}
function stopSong() {
isPlaying === false;
stop();
}
return (
<div className={styles.playButton}>
<button
data-aos="zoom-in"
data-aos-offset="100"
onClick={isPlaying ? stopSong() : playSong()}
>
</button>
</div>
);
};
export default PlayButton;
After reading the
use-sound documentation, I didn't see anyisPlayingvalue from the second returned value you're destructuring.So
isPlayingwill be undefined, but you can keep track of the playing state with a useState.The
onClickprop expects a function to be called, so you shouldn't call the any of the functions you pass to it.