我react-native-fetch-blob
用来下载文件。该文件位于RNFS.DocumentDirectoryPath + fileExtension;
eq: RNFS.DocumentDirectoryPath /myMusic.mp3;
下载的文件位于
/data/user/0/com.myproject/files/myMusic.mp3
并且根据react-native-sound
它要求将文件放在原始文件夹下。当我按照下面的代码使用react-native-fs
时,它正在播放!!!!
downloadFileAndPlay()
{
console.log('Download');
const downloadDest = RNFS.DocumentDirectoryPath + '/sample.mp3';
const ret = RNFS.downloadFile(
{
fromUrl: 'http://www.julien-gustin.be/files/sample.mp3',
toFile: downloadDest
}
);
ret.promise.then(res => {
console.log(res);
// Load the sound file 'whoosh.mp3' from the app bundle
// See notes below about preloading sounds within initialization code below.
console.log(downloadDest);
var whoosh = new Sound(downloadDest, '', (error) => {
if (error) {
console.log('failed to load the sound', error);
return;
}
// loaded successfully
console.log('duration in seconds: ' + whoosh.getDuration() + 'number of channels: ' + whoosh.getNumberOfChannels());
if(whoosh.isLoaded()){
console.log('LOADED');
}else{
console.log('NOT LOADED');
}
// Play the sound with an onEnd callback
whoosh.play((success) => {
if (success) {
console.log('successfully finished playing');
} else {
console.log('playback failed due to audio decoding errors');
}
});
});
}).catch(err => {
console.log(err);
});
}
在这两个地方我都使用相同的目录路径。但在我们的例子中,它没有播放..当我从另一个组件调用这个文件时,如何检索下载的文件并播放?