Search code examples
androidaudioreact-native

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?


Solution

  • 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.