react-native-sound
react-native-sound copied to clipboard
Resume after pause
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...
- [ ] iOS
- [x] Android
- [ ] Windows
Are you using...
- [x] React Native CLI (e.g.
react-native run-android
) - [ ] Expo
- [ ] Other: (please specify)
Which versions are you using?
- React Native Sound: last version
- React Native: ^0.59.10
- iOS:
- Android: 9
Does the problem occur on...
- [ ] Simulator
- [x] Device
If your problem is happening on a device, which device?
- Device: Pocophone F1
Hi! Could you fill out the issue template, please?
Hi! Could you fill out the issue template, please?
Done, sorry for that.
Can you explain what is happening? If you remove the try blocks are any errors thrown?
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.
What is the solution?
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(); } }
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.
any solution here? thanks
What worked for me is,
SoundPlayer.playUrl(url);
SoundPlayer.seek(time);