Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

876
Visualizações
How do I play audio in React?

I'm making a React app and I want to be able to play audio. I'm using nuka-carousel to make a carousel, and on each slide I want to play a different sound.

I just can't seem to get the audio playing (ideally autoplay) at all. I tried using the <audio> tag with the <source> tags inside and that didn't seem to work, and neither did using <audio src="...", autoPlay>, as suggested by another StackOverflow post. All of those resulted in just the controls displaying if I enabled the controls tag but I couldn't even press the play button. I tried react-audio-player and that gave the same result. This is what shows up and this is the code for all 3 of those: enter image description here

This is the code for that

function App() {
  return (
    <div>
      <audio src="..." controls/>

      <audio controls>
        <source src="..."/>
      </audio>

      <ReactAudioPlayer src="..." controls>
        
      </ReactAudioPlayer>
    </div>
  );
}

I also tried using Howler, but with that I found out that I basically need to either have a button to play the sound (I tried making it so that it would autoplay, but for some reason it resulted in the sound playing every time I clicked the page, and I have no idea why). Here's what I have with Howler:

function SoundPlay(src){
    const sound = new Howl({
        src,
        autoplay: true,
        onend: function(){
            console.log("done");
        }
    })
    sound.play();
}
//soundObj is just an object with the sound source and its label
function RenderButtonAndSound(){
        return(
            <button key={index} onClick={() => SoundPlay(soundObj.sound)}>
                {soundObj.label}
            </button>
        )
    })
}

function Slide1(){
    return(
        <div>
            this is the first slide
            {SoundPlay(moon)};
        </div>
    )
} 
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda