Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React-Native sound clip stops working after a certain amount of clicks

I am using the react-native-sound library. I have a button that invokes a function to play a short sound file. After playing the sound file 15 times, the sound no longer plays, even though the function loads.

The following is my function code:

  playSound() {
    var s = new Sound('soundfile.mp3', Sound.MAIN_BUNDLE, (e) => {
      if (e) {
        console.log('error', e);
      } else {
        console.log('duration', s.getDuration());
        s.play()
      }
    });

  }

What is happening and how can I solve this problem?

like image 462
kojow7 Avatar asked Dec 20 '25 00:12

kojow7


1 Answers

The reason is because all the resources are being used up. You should release your audio resources when you are done with them.

 playSound() {
    var s = new Sound('soundfile.mp3', Sound.MAIN_BUNDLE, (e) => {
      if (e) {
        console.log('error', e);
      } else {
        console.log('duration', s.getDuration());
        s.play(()=>{
                s.release()
        })
      }
    });

  }

Also, you can reuse a Sound instance rather than recreating a new resource each time.

like image 110
kojow7 Avatar answered Dec 21 '25 14:12

kojow7



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!