react-native-sound
react-native-sound copied to clipboard
Sound sometimes playing on Android, sometimes not
:beetle: Description
Playing sounds work sometimes and sometimes not. Only affects Android. Same exact code works flawlessly on iOS.
:beetle: What is the observed behavior?
Playing sounds work sometimes and sometimes not. new Sound
callback returns no errors, and play()
callback returns true. But still, the sounds are only played sometimes. There seems to be no correlation between which file succeeds to play and which doesn't.
:beetle: What is the expected behavior?
That it works as intended, i.e. that I can trust the documentation of this package.
:beetle: Please post your code:
// Below is a simplified example of what I'm doing.
const audioFiles = {
example: require('./audio/example.mp3')
}
class Track {
constructor(id) {
this._id = id
this._sound = null
this.isLoaded = false
}
load = () => new Promise((resolve, reject) => {
this._sound = new Sound(audioFiles[this._id], (error) => {
if (error) {
reject(error)
} else {
this.isLoaded = true
resolve()
}
})
})
play = () => new Promise(async (resolve, reject) => {
if (this.isLoaded === false) {
try {
await this.load()
} catch (error) {
reject(error)
}
}
this._sound.play((success) => {
if (!success) {
reject('Play failed')
} else {
resolve()
}
})
})
}
const track = new Track('example')
try {
track.play()
} catch (error) {
console.log(error)
}
:bulb: Does the problem have a test case?
:bulb: Possible solution
:bulb: Is there a workaround?
:bulb: If the bug is confirmed, would you be willing to create a pull request?
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: 0.11.0
- React Native: 0.61.4
- Android: Have tried a few different, 6.0.1 on physical device. Not sure about the emulators. Latest version I guess (brand new emulators from latest version of Android Studio).
Does the problem occur on...
- [x] Simulator
- [x] Device
If your problem is happening on a device, which device?
- Device: Xperia D6603 running Android 6.0.1
+1 here. I found it seems like the player's state is abnormal. For example, if I call the play()
quite fast before the previous is finished. Then there's a high chance that it has the problem. And after the problem emerges, all following attempts to play the audio fail. I need to restart the app.
BTW, I found the issue can be significantly solved (but not 100%) by calling .release()
at appropriate timing. I guess there's race condition causing an abnormal state of Android Media Player. But I don't know the root cause yet.
I'll deeply appreciate If someone who is familiar with the project can help to check the source codes relates to the release()
.
I'm facing the same issue. Some sounds randomly stop playing. After one stops, consecutive all sounds stop playing.
Hello, Any updates on this issue?
Same here, happening on Android only and on Lenovo tabM10. Feels like a race condition problem since it is hard to reproduce, but happens often enough to be unreliable in production.
First idea to try out - the docs specify that it is wise to replay the sound in the sound.stop()
callback, e.g:
// Stop the sound and rewind to the beginning
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();
});
Will try it out soon, but even so it feels like the library is getting outdated, with the latest release happening a year ago and the maintainers claiming it to be in alpha/beta stage. So I guess it is time to fork and customise or find a new library. 🙈
@AdamGerthel did you manage to figure out a fix for this? 🙏
@rasmuslelumees I ended up using Expo-AV instead (can be used in the "bare" workflow if you have Expo unimodules installed). It hasn't been without issues either but it's been more stable than the other packages.
Here's my implementation: https://github.com/expo/expo/issues/1873#issuecomment-592214795
Thanks @AdamGerthel, will take a look 💯
+1 same issue
+1 same issue
Any one have solution for this? I am facing same issue and our app is live so it impacts to user base.
@anjandaffo try to release() sound when component didmout
@nguyenvanphuc2203 in component did mount we don't have audio object to release it. There is no method to release all. In library i checked it is creating pool of media player. I am releasing the audio from inside the callback of play method and unmount of components.
this._sound.release() @anjandaffo , such as sample in this issue
@nguyenvanphuc2203 in component mount this._sound is null. Did you mean component unmount? In component unmount I am calling the release function
@anjandaffo oh yes, in componentUnmount you need release sound, sorry i forget
@nguyenvanphuc2203 we are doing the same. On component unmount and play completion callback we are calling function of release. After the audio not play in callback of load getting success true and and play method instant giving callback
I had the same problem. The solution that works for me is to call the method reset() directly after the sound finnished playing.
Solution:
export const doPlaySound = (soundFile: string) =>{ const playSound = new Sound(soundFile, Sound.MAIN_BUNDLE, (error: string) => { if(error){ console.log('failed to load the sound', error); return; } playSound.play((success: boolean) => { if(success) { playSound.reset(); return; }else{ console.log('playback failed due to audio decoding errors'); return; } }); }); }
sry i don't know how i can post the code here more readable
Hey guys, I was having the same issue as described in this thread. But I managed to solve the problem.
So, for future readers, this issue seems related to #607. Apparently, audios can display different durations on different devices (and this seems to be caused by the bit rate of the audio, don't ask me what that means, I don't know), and this inconsistency can cause some of the functions of the library to not work properly.
The solution for that is related in this comment, but I'm going to describe it here too.
You're going to need to format your audio and make the bit rate of it constant. To do that you need an audio editor (I've used Audacity, it's free). And for Audacity you need to (Not sure how that would work in another editor):
- Click on File menu and import your audio
- Now click on File menu again and click export
- After that you just need to export your audio with a constant bit rate (simple like that), take a look at the screenshot below.
