react-native-sound icon indicating copy to clipboard operation
react-native-sound copied to clipboard

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

Open allindeveloper opened this issue 5 years ago • 1 comments

: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

  • [ ] iOS
  • [ ] Android
  • [ ] Windows

Are you using... React Native CLI

  • [ ] React Native CLI (e.g. react-native run-android)
  • [ ] Expo
  • [ ] Other: (please specify)

Which versions are you using?

  • React Native Sound:0.11.0
  • React Native:0.62.2
  • iOS: 11
  • Android: 7.1.1
  • Windows:

Does the problem occur on... on Simulator

  • [ ] Simulator
  • [ ] Device

allindeveloper avatar May 16 '20 19:05 allindeveloper

hi bro, can u provide BackgroundTimer code? Please

fukemy avatar May 16 '22 07:05 fukemy