react-native-sound

react-native-sound no sound from Android (only Genymotion emulator)


I've added the code to play sounds in my react native app.

I've tried specifying the audio file location in various ways:

    const soundInfo =
      {
        url: require('./sound.wav'),
      }

with:

    new Sound(soundInfo.url, error => callback(error, sound));

and:

    const soundInfo =
      {
        url: 'sound.wav',
      }

with:

    const sound = new Sound(soundInfo.url, Sound.MAIN_BUNDLE, error => callback(error, sound));

Everything works fine in the Gemymotion Android emulator, but not when the same app is installed on a physical phone.

The docs: https://github.com/zmxv/react-native-sound

Say:

Android: Save your sound clip files under the directory android/app/src/main/res/raw. Note that files in this directory must be lowercase and underscored (e.g. my_file_name.mp3) and that subdirectories are not supported by Android.

I've tried putting the audio files everywhere, but no sounds are played.

Is there a way to check where they need to be? Why is it working on the emulator?

Thanks.


Solution

  • As I've done some tests, I put here my findings just in case someone needs more clarity.

    Having the audio files referenced this way:

    const soundInfo =
      {
        url: require('./sound.wav'),
      }
    

    They need to be placed in the same directory as the .js file using them. This will work on an emulator and on the actual device. I didn't use the directory android/app/src/main/res/raw. I didn't use the Sound.MAIN_BUNDLE option either as it wasn't working, so I went for the first option with require().