Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not working on Android | number of channels is -1 #203

Open
rohitgoyal opened this issue Jun 14, 2017 · 33 comments
Open

Not working on Android | number of channels is -1 #203

rohitgoyal opened this issue Jun 14, 2017 · 33 comments

Comments

@rohitgoyal
Copy link

rohitgoyal commented Jun 14, 2017

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

@rohitgoyal rohitgoyal changed the title Not working on Android Not working on Android | number of channels is -1 Jun 15, 2017
@Winglonelion
Copy link

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.

@rohitgoyal
Copy link
Author

@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')
          }
        });
      }
    });
  }

@benvium
Copy link
Collaborator

benvium commented Jun 16, 2017

Can you try this on new version 0.10.2? This should be fixed

@rohitgoyal
Copy link
Author

@benvium It worked just the first time but after that i doesn't work ever. After closing the app and reopening, the sound does not work even for the first time too. All the time I am getting the sound played callback though.

@benvium
Copy link
Collaborator

benvium commented Jun 16, 2017

@rohitgoyal Can you have a go with react-native-sound-demo and see if that works for you?

@rohitgoyal
Copy link
Author

Tried it. No sounds in react-native-sound-demo on android too. :-/

@benvium
Copy link
Collaborator

benvium commented Jun 16, 2017

Which device and OS are you on?

@rohitgoyal
Copy link
Author

Google Nexus 6P
OS: 6.0.0
API 23

@s5963028
Copy link

I have the same problem.
"duration in seconds: 3.63number of channels: -1"
but when i try to play it i receive this error from callback:
"playback failed due to audio decoding errors"

@alexprice1
Copy link

bump

@ShinichiroMike
Copy link

I have the same problem.
"duration in seconds: 3.63number of channels: -1"
but when i try to play it i receive this error from callback:
"playback failed due to audio decoding errors"

The same here. Running on android device.

@basvasilich
Copy link

"duration in seconds: 3.63number of channels: -1"
but when i try to play it i receive this error from callback:
"playback failed due to audio decoding errors"

The same here.
Samsung Galaxy S6
OS: 5.1.0
API 22

@DavitVosk
Copy link

Anybody has solved the problem?

@DavitVosk
Copy link

DavitVosk commented Aug 17, 2017

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');
			}
		});
	});

@pehlert
Copy link

pehlert commented Sep 2, 2017

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

@gvenk
Copy link
Contributor

gvenk commented Sep 2, 2017

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.

@gvenk
Copy link
Contributor

gvenk commented Sep 2, 2017

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

@gvenk
Copy link
Contributor

gvenk commented Sep 4, 2017

@rohitgoyal Is your problem fixed with the latest version? I see that you already were playing from the onError callback

@rohitgoyal
Copy link
Author

@gvenk I got it working while playing from the Main Bundle but it still doesn't work from a dir path.
In case of Main bundle it doesn't work with an Emulator.
Lot of people might be testing it on an Emulator.

@gvenk
Copy link
Contributor

gvenk commented Sep 5, 2017

@rohitgoyal if you want to get it working from a dir path in the app, you have to remove the second parameter Sound.MAIN_BUNDLE. The onError handler must be the second parameter.

Annoying it's not working on the emulator. I don't have any problem with the emulator, so it's difficult to debug :-(

@rohitgoyal
Copy link
Author

@gvenk I did try it after removing the second parameter only. It didn't work on Android but worked on iOS(Check 3rd reply on the thread).

On Emulator it might be specific emulator issue as well. I tried on genymotion emulator with following config
Google Nexus 6P
OS: 6.0.0
API 23

@gvenk
Copy link
Contributor

gvenk commented Sep 5, 2017

@rohitgoyal ah, I overlooked that comment. Was this on a real device, or emulator?

Ah, I don't have access to the genymotion emulator, so I'm not able to debug this, sorry.

@rohitgoyal
Copy link
Author

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

@gvenk
Copy link
Contributor

gvenk commented Sep 5, 2017

Ok, @rohitgoyal so this issue can be closed according to you?

Maybe we have to add a note about which emulators we know are working with this project.

@rohitgoyal
Copy link
Author

@gvenk Yeah I guess. You can add the note for sound not playing on emulator and sound not playing on real android device using dir path.

@trepidity
Copy link
Collaborator

@gvenk does this relate to switching from true / false?

@gvenk
Copy link
Contributor

gvenk commented Sep 5, 2017

@trepidity don't think so

@smithaitufe
Copy link

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

@StewartJingga
Copy link

Make sure you give some time between loading your sound and playing it.

My "whoosh.play()" was run right after my "var whoosh = new Sound()" which gave me the error.

After I split them into two different function and call them separately with some interval, it works its magic.

Well at least that was my issue.

@Faisal-Manzer
Copy link

// audio is a remote url
s = new Sound(audio, null, (e) => {
        if (e) {
            console.log('error', e);
        } 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')
                }
            });
        }
    });

this solved my issue, Sound.MAIN_BUNDLE and play() took place inside the callback function

React = 16.5.2
React-Native = 0.57.1
React-Native-Sound = 0.10.9

@afilp
Copy link

afilp commented Nov 12, 2018

@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!

@gurol
Copy link

gurol commented Nov 19, 2018

Android could not play the sound embedded in the bundle. There is no problem in iOS (even you can initiate the sound once and then use play() function consecutively.

@kyle-belle
Copy link

Im still experiencing this issue on android still. (June 7th, 2021)

Device: OnePlus 7Pro - Android 10

I am trying to play from network
the number of channels is always -1;

let s;
// component
() => {
const onPressPlay = () => {
        
        if(playing){
            s.pause();
            setPlaying(false);
        }else{
            if(!songLoaded){
                setSongLoading(true)
                s = new Sound(audio_src, null, (error) => {
                    if(error){
                        console.log(e);
                        Alert.alert("Audio Error", error);
                        setSongLoading(false);
                        return;
                    }

                    console.log("Success Loading Audio");
                    console.log('duration in seconds: ' + s.getDuration() + 'number of channels: ' + s.getNumberOfChannels());

                    setSongLoading(false);
                    setSongLoaded(true);
                    s.play((success) => {
                        if(success){
                            console.log("Success Playing Audio");
                            setPlaying(true)
                        }else{
                            console.log("Error Playing Audio");
                        }
                    })
                });
                setSongLoaded(true);
            }else{
                s.play((success) => {
                    if(success){
                        console.log("Success Playing Audio");
                        setPlaying(true)
                    }else{
                        console.log("Error Playing Audio");
                    }
                });
            }
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests