zmxv / react-native-sound

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

Is there any limit for preloading sounds for prod build on iOS? #759

Open alexeyvax opened 2 years ago

alexeyvax commented 2 years ago

:beetle: Description

The limit for preloading sounds on iOS.

:beetle: What have you tried?

I'm trying to preload more than 300 short sounds via new Sound('file-name.mp3', Sound.MAIN_BUNDLE, () => {}); at the same time, but have no luck loading all sounds. It only loads about 150 sounds and that's it. Also, I would like to mention that this happened for prod build on iOS, but on dev build it works fine.

:beetle: Please post your code:

// sounds = Array<{ title: string; value: string }>

  loadSounds = async (sounds) => {
    const promisifiedSounds = sounds.map((sound) => {
      return new Promise((res, rej) => {
        const currentSound = new Sound(sound.value, Sound.MAIN_BUNDLE, (error) => {
          if (error) {
            console.info('failed to load the sound ', error);
            rej(error);
          }

          currentSound.setVolume(1);

          res(currentSound);
        });
      });
    });

    const data = await Promise.all(promisifiedSounds);

    return data;
  }

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?

alexeyvax commented 2 years ago

Also, I can see an error inside the prod build on xcode console

[javascript] 'failed to load the sound ', { code: 'ENSOSSTATUSERRORDOMAIN-42',
  message: 'The operation couldn\\U2019t be completed. (NSOSStatusErrorDomain error -42.)',
  domain: 'NSOSStatusErrorDomain',
  userInfo: {},
  nativeStackIOS: 
   [ '0   MyApp               0x00000001006ddd88 RCTJSErrorFromCodeMessageAndNSError + 100',
     '1   MyApp               0x00000001006ddce8 RCTJSErrorFromNSError + 204',
     '2   MyApp               0x000000010067a028 -[RNSound prepare:withKey:withOptions:withCallback:] + 944',
     '3   CoreFoundation                      0x00000001a99fb724 338F58B1-9B75-38A0-B908-E2574AB1D39A + 1238820',
     '4   CoreFoundation                      0x00000001a98ced04 338F58B1-9B75-38A0-B908-E2574AB1D39A + 7428',
     '5   CoreFoundation                      0x00000001a98cf308 338F58B1-9B75-38A0-B9

Does anybody know, what does this error means?

WenLonG12345 commented 2 years ago

I have the same issue as IOS Prod didnt play any sound, but debug works well. Any solution?

alexeyvax commented 2 years ago

I figured this issue out. Instead of loading all sounds after app initialization, just load one sound when the user pressed a play button.

And that's it.

It takes some milliseconds to load your sound but it works perfectly for dev and prod builds.