zmxv / react-native-sound

React Native module for playing sound clips
MIT License
2.79k stars 748 forks source link

Resume after pause #609

Open IsmaelMiber opened 5 years ago

IsmaelMiber commented 5 years ago

I have issue with resume sound after pausing it.

I want to make play pause resume sound.

:beetle: The code:


//SoundManager util that i made.

import Sound from "react-native-sound";

const sounds = {
  "crow_sound": {
    sound: "people-talking-2.mp3",
    path: "https://www.pacdv.com/sounds/people_sound_effects"
  },
  "sally_sound": {
    sound: require("./../../assets/sounds/sallysound.mp3")
  },
};
class SoundManager {

  playUserReview(name) {
    try {
      var sound = sounds[name].sound;
      var path = sounds[name].path;
      if (path) {
        this.userReview = new Sound(sound, path, error => {
          if (error) {
            return;
          }

          this.userReview.play(success => {
            if (!success) {
              this.userReview.reset();
            }
          });
        });
      } else {
        this.userReview = new Sound(sound, error => {
          if (error) {
            return;
          }

          this.userReview.play(success => {
            if (!success) {
              this.userReview.reset();
            }
          });
        });
      }
    } catch (error) {
      console.log(error);
    }
  }

  pauseUsersReviews() {
    try {
          this.userReview.pause(success => {
            if (!success) {
              this.userReview.reset();
            }
          });
    } catch (error) {
      console.log(error);
    }
  }

  resumeUserReview() {
    this.userReview.play((success) => {
      success ? console.warn("success") : console.warn("NotSuccess")
    });
  }
}

module.exports = new SoundManager();

-----------------------

//and this is the function that i used onpress play/pause button.

userNameForPlayingRelatedSound = (username) => {
        if(this.state.soundStatus == "notPlayed") {
            SoundManager.playUserReview(username);
            this.setState({soundStatus: "played"});
            console.warn("played");
        }
        else if(this.state.soundStatus == "played") {
            SoundManager.pauseUsersReviews();
            this.setState({soundStatus: "paused"})
            console.warn("paused");
        }
        else if(this.state.soundStatus == "paused") {
            SoundManager.resumeUserReview();
            this.setState({soundStatus: "played"})
            console.warn("resume");
        }

    }

:bulb: Possible solution

Is your issue with...

Are you using...

Which versions are you using?

Does the problem occur on...

If your problem is happening on a device, which device?

paulmelnikow commented 5 years ago

Hi! Could you fill out the issue template, please?

IsmaelMiber commented 5 years ago

Hi! Could you fill out the issue template, please?

Done, sorry for that.

paulmelnikow commented 5 years ago

Can you explain what is happening? If you remove the try blocks are any errors thrown?

IsmaelMiber commented 5 years ago

Can you explain what is happening? If you remove the try blocks are any errors thrown?

when i click to play sound for first time it played will. then i click to pause it paused but when i click again to resume sound, the sound didn't resume it just no sound played.

there is no any error thrown.

vaimeo commented 4 years ago

What is the solution?

asiriPiyajanaka commented 4 years ago

I used a workaround for this issue. When pausing get the current time and set it to state like

// pause sound whoosh.getCurrentTime((seconds) => this.setcuruntPlayTime(seconds) ); whoosh.pause();

setcuruntPlayTime(time) { this.setState({ curuntPlayTime:time }, () => { }) }

then seek the audio object when resuming, like..

async resume (){ if(!_.isEmpty(whoosh)){ // if whoosh empty, app get crashed whoosh.setCurrentTime(this.state.curuntPlayTime); whoosh.play(); } }

kartavyaparekh96 commented 4 years ago

Still not working @asiriPiyajanaka I used

whoosh.setCurrentTime(this.state.curuntPlayTime); 
whoosh.play((success)=> {
          console.log("Hello", success);
        }); 

then after success callback of play came and still, audio is not completed.

fukemy commented 2 years ago

any solution here? thanks

KaviduAloka commented 2 years ago

What worked for me is,

SoundPlayer.playUrl(url);
SoundPlayer.seek(time);