react-native-sound: Can't load file on device
Hi, I’m working on a simple soundboard app (source code is available at https://github.com/sexykodo/mlcSoundboard) that automatically fetches remote audio files according to a JSON and puts them in MainBundle with RNFetchBlob.
It’s something like this:
fetchRemoteFile = (obj) => {
RNFetchBlob
.config({
path : RNFetchBlob.fs.dirs.MainBundleDir + encodeURI(obj.Path) //target path
})
.fetch('GET', MAIN_URL + encodeURI(obj.Path), {})
.then((res) => {
// the conversion is done in native code
// the following conversions are done in js, it's SYNC
console.log('The file saved to ', res.path())
})
// Status code is not 200
.catch((errorMessage, statusCode) => {
// error handling
console.log(errorMessage)
})
}
In this way I can dynamically sync local files with remote ones (I want the soundboard to work offline). Everything works like a charm on IOS Simulator.
On physical device though, when I load the local file I get an errorcode “ENSOSSTATUSERRORDOMAIN2003334207”, wich is similar to #64 and probably caused by files not being included in Resources.
Is it actually possible to programmatically save an audio file (with RNFetchBlob) and play it?
Thanks in advance
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 3
- Comments: 28
I was getting the same error when trying to play the file right away. This workaround fixes it for me (but it’s hacky). Both timeouts are necessary:
@maurovisintin, thank you for your response. Your solution didn’t work for me but I figured out the problem and got it resolved this way: RNFetchBlob.config({ fileCache: true, })… when initializing the sound const sound = new Sound(file.Path,‘’, error => {… The sound bundle must be an empty string ‘’ in iOS.
Ye, I got this working with RNFetchBlob.fs.dirs.DocumentDir
Something like this
@cooperka Looks like https://github.com/jsierles/react-native-audio/pull/155 solved my issue. Thanks.
1.检查RNSound.xcodeproj是否导入 2.检查libRNSOund.a是否添加 3.检查音频文件是否导入 4.初始化看起来像这样: const mainBundle = Platform.OS === ‘ios’ ? encodeURIComponent(Sound.MAIN_BUNDLE) : Sound.MAIN_BUNDLE; new Sound(‘music.mp3’, mainBundle, (error) => { … } 5.祝您成功!
Use ‘new Promise’ to solve the flow problem
This way resolved issue for both IOS and Android
@jimthedev: Working for me on both device and simulator. There was also another issue that could be related for you, fixed here: https://github.com/jsierles/react-native-audio/pull/155.