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

222
Visualizações
react-native sound not working after a while

I'm using 50 different sound files. it works normally but on my 14th or 15th try it no longer plays the audio files. (I am using android device)

  const animalSound = new Sound( selectedAnimals.soundUrl ||"snake.mp3", null, error => {
    if (error) console.log("Can't play sound. ", error);
  })

const handlePlaySound = () => {
    animalSound.setVolume(1);
    animalSound.play(() => {
        animalSound.release();
    });
  };

const handleStopSound = id => {
    animalSound.stop()
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I used expo-av for sound, which can also be used in a bare react-native project. (https://github.com/expo/expo/tree/main/packages/expo-av)

I made this hook which allows you to play the sound and aslo clears up the resources for you so you don't have to worry about it.

/* 
    This hooks abstracts away all the logic of 
    loading up and unloading songs. All the hook 
    takes in is the require path of the audio
*/
import React,{useState,useEffect} from 'react'
import { Audio } from 'expo-av';

const useSound = (path) => {
  /* 
    Sound state
  */
  const [sound, setSound] = useState();

  /* 
    Logic to unload sound when screen changes
  */
  useEffect(() => {
    return sound
        ? () => {
            sound.unloadAsync();
        }
        : undefined;
  }, [sound]);
 
  /*
    Memoize the function so that it does not get 
    recomputed every time that the screen load
  */
  const playSound = React.useCallback(async ()=>{
      const { sound } = await Audio.Sound.createAsync(path);
      setSound(sound);
      await sound.playAsync();
  },[sound])

   
  /* 
     Stop sound 
  */
  const stopSound = React.useCallback(async ()=>{
      await sound.stopAsync();
  },[sound])

  return [playSound,stopSound]
}

All you have to do to use the sound is this

/* 
    The hooks returns a function to be called when to play 
    a sound, and it abstracts away having to deal with unloading'
    the sound
 */
const [playSound,stopSound] = useSound(require("snake.mp3"));
about 4 years ago · Juan Pablo Isaza Relatório
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