zmxv / react-native-sound

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

Sound not playing after few audio Successfully played (Android) #796

Open aniketgupta8960 opened 1 year ago

aniketgupta8960 commented 1 year ago

We played some audio on android but after few audio successfully played we get this error code setConfig(0xf25a71a0:google.mp3.decoder, ConfigPriority(0x6f800002)) ERROR: Undefined(0x80001001) getConfig(0xf25a71a0:google.mp3.decoder, ConfigAndroidVendorExtension(0x6f100004)) ERROR: Undefined(0x80001001) After this error no audio is playing until restart the app although app run fine on ios but facing this issue in android after the audio completed we release the audio reference

we can reproduced this bug if we played 10 audio one by one then in some case it produces the bug

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?

chetstone commented 1 year ago

I had a similar problem. It seems Android can only handle a few open sound instances. I found this very old comment. From my experience it seems something like that is still the case. I was trying to open about 16 sound instances so the user could select a notification sound from a menu. This worked fine on iOS but many of them wouldn't play on Android. I also had a number of open sound instances in other parts of the app. I cleaned those up and decided that for this sound selection screen I didn't need to have the sounds preloaded. I could load and play them all in one go like this:

    const s = new Sound(fileName, Sound.MAIN_BUNDLE, (err) => {
      if (err) {
        info(`Error creating sound, ${JSON.stringify(err)}`);
      } else {
        info(
          `Created sound from ${fileName}, Loaded ${JSON.stringify(
            s?.isLoaded()
          )}`
        );
        s.setNumberOfLoops(0)
          .setVolume(Math.pow(volume, 3))
          .play((success) => {
            if (success) {
              info(`Success playing ${fileName}`);
            } else {
              info(`Failed playing ${fileName}`);
            }
            // Sometimes very short sounds (~100ms) don't play
            // if another sound is not playing or has not played in the last second or so.
            // Delaying the release fixes this.
            // Apparently this callback is called before the sound actually starts playing.
            setTimeout(function () {
              info(`Releasing ${s?._filename}`);
              s?.release();
            }, 140);
          });
      }
    });

I had expected there to be a long delay when the user presses the button, but the additional delay from loading seems almost insignificant. The UX is good.

It is important to release your sound instances after using them. Don't use the reset() function. It places the state machine in the End state so the sound instance is no longer useable, but it doesn't release resources. Anyway, when you call release(), the Native code calls reset() before it calls release().

waqaskhanroghani commented 1 year ago

The solution is there remove the anySoundName.release(); from the code anywhere in the code file.

useEffect(() => { anySoundName.setVolume(1); return () => { anySoundName.release(); // remove this line and see the magic }; }, []);

try first thanks me later :)