zmxv / react-native-sound

React Native module for playing sound clips
MIT License
2.79k stars 748 forks source link

Sound doesn't work after playing it more than 15-20 times #565

Closed alecrugar closed 5 years ago

alecrugar commented 5 years ago

I'm playing a sound several times in my app. It works great on iOS but on Android it stops playing after the sound is played for about 15-20 times. It works again when the screen is reloaded or the app is closed/opened.

There is a Button. Every time it is clicked this code is executed:

 const playSound = (): void => {
    let Sound = require("react-native-sound");

    Sound.setCategory("Playback", false);

    let soundObj = new Sound('mysound.mp3',Sound.MAIN_BUNDLE,(error: Error) => {
        if (error) {
          console.log(error);

          return;
        }

        soundObj.play((success: boolean) => {
          if (success) {
            console.log("sound played successfully", success);
          }
        });
      }
    );

    soundObj.release();
    };

After pressing the button 15-20 times the sound is not played anymore, but there is no error logged. The logs are exactly the same no matter if the sound is played or not.

Any ideas?

Thanks.

anjalinirupa commented 5 years ago

you have to release the resources after usage , refer this link. @alecrugar

https://stackoverflow.com/questions/41196649/react-native-sound-clip-stops-working-after-a-certain-amount-of-clicks

alecrugar commented 5 years ago

Hi @anjalinirupa,

as you can see in my code, I already release the resources:

soundObj.release();

anjalinirupa commented 5 years ago

Hi @alecrugar you have to release like this( inside soundObj.play), its worked for me. thank you

soundObj.play((success: boolean) => { if (success) { soundObj.release(); } });

alecrugar commented 5 years ago

Hi @anjalinirupa,

you are absolutely right.

Thanks a lot :)

MacVanHien commented 1 year ago

i'm a new one to react native and try many times to fix this. When i add s.release() like sire kojow7's answered it's great but it stoped after less than 50 times. But finally i fixed this, here is my code :

const [countTimesSoundPlay, setCountTimesSoundPlay] = useState(0);
  const [soundTrue, setsoundTrue] = useState(null)

  useEffect(() => {
    var soundTrue = new Sound('sound_good.mp3', Sound.MAIN_BUNDLE, (error) => {
      if (error) {
        console.log('failed to load the sound', error);
        return;
      }
      // loaded successfully
      console.log('duration in seconds: ' + soundTrue.getDuration() + 'number of channels: ' + soundTrue.getNumberOfChannels());
    });

    setsoundTrue(soundTrue)
  }, [countTimesSoundPlay])

  const timerRef2 = useRef(null);

  const playTrue = async () => {
    soundTrue.play((sucess) => {
      if (sucess) {
        console.log('successfully finished playing');
        soundTrue.release()
        timerRef2.current = setTimeout(() => {
          setCountTimesSoundPlay(countTimesSoundPlay + 1)
        }, 1200);
      } else {
        console.log('playback failed due to audio decoding errors');
      }
      return () => {
        if (timerRef2.current) {
          clearTimeout(timerRef2.current);
        }
      };
    });
  }

////i tested with 200 times :)
  // useEffect (()=> {
  //   if (soundTrue) {
  //      playTrue()
  //     console.log('countTimesSoundPlay', countTimesSoundPlay)
  //   }
  // },[countTimesSoundPlay])

hope it helps!