zmxv / react-native-sound

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

Please how can i stop a sound that is playing in background #679

Open allindeveloper opened 4 years ago

allindeveloper commented 4 years ago

:beetle: Description

I started playing a sound when the app is in background, so i need to get the current sound that is playing in background and stop it.

:beetle: What have you tried?

i tried something like this,

whoosh.stop(() => {
  // Note: If you want to play a sound after stopping and rewinding it,
  // it is important to call play() in a callback.
  whoosh.play();
});

wasn;t really working :beetle: Please post your code:

This is the code i use to initiate the background job at a particular time in the future

 BackgroundTimer.setTimeout(() => {
             i++
      this.play(this.state.selectedSoundUrl);

    }, this.state.backgroundSeconds - 20000);

This is the function i used to play the song...

  play = async (selectedSoundUrl) => {
    console.log("seleeeeected url",selectedSoundUrl)
    if(selectedSoundUrl !== null) {
      if(this.sound){
        this.sound.play(this.playComplete);
        this.setState({playState:'playing'});
    }else{
        // const filepath = this.props.navigation.state.params.filepath;
        // console.log('[Play]', filepath);

        this.sound = new Sound(selectedSoundUrl,
         Sound.MAIN_BUNDLE, (error) => {
            if (error) {
                console.log('failed to load the sound', error);
                //Alert.alert('Notice', 'audio file error. (Error code : 1)');
                this.setState({playState:'paused'});
            }else{
              console.log("started playing")
                // this.setState({playState:'playing', duration:this.sound.getDuration()});
                this.sound.play(this.playComplete);
            }
        });    
    }
 }
}

:bulb: Possible solution

Is your issue with... Android and IOS

Are you using... React Native CLI

Which versions are you using?

Does the problem occur on... on Simulator

fukemy commented 2 years ago

hi bro, can u provide BackgroundTimer code? Please