Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

134
Vistas
Toggle play/pause button in Next with mp3 audio

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;

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

After reading the use-sound documentation, I didn't see any isPlaying value from the second returned value you're destructuring.

So isPlaying will be undefined, but you can keep track of the playing state with a useState.

...

import { useState } from "react";

const PlayButton = ({}) => {
  const [isPlaying, setIsPlaying] = useState(false);
  const [play, { stop }] = useSound(galaxySfx);

  function playSong() {
    setIsPlaying(true);
    play();
  }

  function stopSong() {
    setIsPlaying(false);
    stop();
  }

The onClick prop expects a function to be called, so you shouldn't call the any of the functions you pass to it.

  return (
    <div className={styles.playButton}>
      <button
        data-aos="zoom-in"
        data-aos-offset="100"
        onClick={isPlaying ? stopSong : playSong}
      >
        🎺
      </button>
    </div>
  );
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

You could use the same handler for both operations. Check the state of the isPlaying variable and act accordingly:

function togglePlay(){
    if(isPlaying){
        stop();
    } else{
        play();
    }
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

Just remove bracket, in this way functions won't run unless you onClick

onClick={isPlaying ? stop: play}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda