react-native-sound: Not working on Android | number of channels is -1

Sounds are working on iOS using the below code but not working on Android. I have notification.wav file in the android/app/src/main/res/raw directory. Help please.

const s = new Sound('notification.wav', Sound.MAIN_BUNDLE, (e) => {
      if (e) {
        console.log('error', e);
        return;
      }
      console.log('sound: duration in seconds: ' + s.getDuration() + 'number of channels: ' + s.getNumberOfChannels());
      s.play(() => s.release());
    });

On log I am getting this but no sound played. And the number of channels is -1

sound: duration in seconds: 0.542number of channels: -1

About this issue

  • Original URL
  • State: open
  • Created 7 years ago
  • Comments: 33 (7 by maintainers)

Most upvoted comments

solved… just put “music.play()” out of constructor error part, like this.

         // Load the sound file
         const music = new Sound('nargiz_vdvoyom.mp3', Sound.MAIN_BUNDLE, (error) => {
		if (error) {
			console.log('failed to load the sound', error);
			return;
		}
		// loaded successfully, play			
		music.play((success) => {
			if (success) {
				console.log('successfully finished playing');
			} else {
				console.log('playback failed due to audio decoding errors');
			}
		});
	});

@Faisal-Manzer But does this mean that we have to initiate the same sound as new, every time we use it? I used to put the sound in a window variable (e.g. window.sound1 = new Sound(...), so that I could just play it later in a different route. It used to work, now not. What is the change needed? Thanks!

new Sound() uses android.media.MediaPlayer.prepareAsync(). So new Sound() returns immediately and it could have been not prepared completely. You could try to add a timeout, but it’s most save to start playing from the onError callback, I think. (and the name onError is a bit confusing)

This must be added to the documentation. I’ll submit a PR next week.

What @DavitVosk means is to put the play() call inside the constructor callback. This has solved playback for me.

@Winglonelion Installed the react-native-sound with your PR. Below is the code to play the sound. On iOS it plays the sound. On Android there are no errors and it prints the log “sound played” but there is no sound.

playTapSound(){
    const s = new Sound('../sounds/tap.wav', (e) => {
      if (e) {
        console.log('error', e);
        return;
      } else{
        console.log('sound loaded successfully: '+s.isLoaded()+" volume is "+s.getVolume())
        s.play((success) => {
          if(success){
            console.log('sound played')
            s.release();
          } else{
            console.log('sound failed to play')
          }
        });
      }
    });
  }

There are a lot of great guys working on this project. Please can someone take a look at this issue.

I am trying to play a user selected music file.

Take a look at the issue here

@gvenk On a real device. No issues. As of now things are working for me using Main Bundle 😃

And the original title of this issue is:

Not working on Android | number of channels is -1

But that is not the real issue, because it will always return -1 on Android because getNumberOfChannels() hasn’t been implemented on Android. See also: #258

change your react-native-sound package dependency by "react-native-sound": "git+https://github.com/zmxv/react-native-sound.git#fix_android_network_sound"

try npm install again.