Closed alecrugar closed 5 years ago
you have to release the resources after usage , refer this link. @alecrugar
Hi @anjalinirupa,
as you can see in my code, I already release the resources:
soundObj.release();
Hi @alecrugar you have to release like this( inside soundObj.play), its worked for me. thank you
soundObj.play((success: boolean) => { if (success) { soundObj.release(); } });
Hi @anjalinirupa,
you are absolutely right.
Thanks a lot :)
i'm a new one to react native and try many times to fix this. When i add s.release() like sire kojow7's answered it's great but it stoped after less than 50 times. But finally i fixed this, here is my code :
const [countTimesSoundPlay, setCountTimesSoundPlay] = useState(0);
const [soundTrue, setsoundTrue] = useState(null)
useEffect(() => {
var soundTrue = new Sound('sound_good.mp3', Sound.MAIN_BUNDLE, (error) => {
if (error) {
console.log('failed to load the sound', error);
return;
}
// loaded successfully
console.log('duration in seconds: ' + soundTrue.getDuration() + 'number of channels: ' + soundTrue.getNumberOfChannels());
});
setsoundTrue(soundTrue)
}, [countTimesSoundPlay])
const timerRef2 = useRef(null);
const playTrue = async () => {
soundTrue.play((sucess) => {
if (sucess) {
console.log('successfully finished playing');
soundTrue.release()
timerRef2.current = setTimeout(() => {
setCountTimesSoundPlay(countTimesSoundPlay + 1)
}, 1200);
} else {
console.log('playback failed due to audio decoding errors');
}
return () => {
if (timerRef2.current) {
clearTimeout(timerRef2.current);
}
};
});
}
////i tested with 200 times :)
// useEffect (()=> {
// if (soundTrue) {
// playTrue()
// console.log('countTimesSoundPlay', countTimesSoundPlay)
// }
// },[countTimesSoundPlay])
hope it helps!
I'm playing a sound several times in my app. It works great on iOS but on Android it stops playing after the sound is played for about 15-20 times. It works again when the screen is reloaded or the app is closed/opened.
There is a Button. Every time it is clicked this code is executed:
After pressing the button 15-20 times the sound is not played anymore, but there is no error logged. The logs are exactly the same no matter if the sound is played or not.
Any ideas?
Thanks.