zmxv / react-native-sound

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

Sound eventually stops working on android [SOLVED] #695

Closed HugoKawamata closed 3 years ago

HugoKawamata commented 3 years ago

:beetle: Description

On my android device, sound works correctly (the sound files are in the correct directory and named appropriately as specified in the readme), and loading and playing callbacks work perfectly. This may be different for other devices, but on my device, after exactly 14 sounds have been played, no further sounds are played. However, the after play callback still counts each failed sound play as a "success".

:beetle: What is the observed behavior?

Sound plays correctly 14 times, then fails to play at all after that point. sound.play((success) => console.log(success)) still indicate a successful playback even though the sound is not playing.

:beetle: What is the expected behavior?

Sound should play consistently, especially if the play callback indicates success.

:beetle: Please post your code:

                const charSound = new Sound(
                  soundFile,
                  Sound.MAIN_BUNDLE,
                  (error) => {
                    if (error) {
                      // do something
                    }

                    // play when loaded
                    charSound.play((success) => {
                      if (success) {
                        console.log("played successfully") // this is firing consistently even when sound does not play
                      }
                    });
                  }
                );
                charSound.release();

:bulb: Solution

I have actually solved this already. I'm posting here to help others in the future.

The issue was that in the README, it seems to indicate that sound.release() is meant to be (or at least allowed to be) called from outside the play callback. However, it seems that my sounds were not being properly released, which caused any sounds beyond the 14th to not load (due to too many sounds being loaded in memory?). The solution was to call sound.release() within the play callback, like so:

                const charSound = new Sound(
                  soundFile,
                  Sound.MAIN_BUNDLE,
                  (error) => {
                    if (error) {
                      // do something
                    }

                    // play when loaded
                    charSound.play(() => {
                      charSound.release();
                    });
                  }
                );

Is your issue with...

Are you using...

Which versions are you using?

Does the problem occur on...

If your problem is happening on a device, which device?