在react native中下载音频文件



我正在尝试使用rn-fetch-blob下载音频文件"mp3"。

所以我有不止一个案例:(

当我试图添加一个路径到RNFetchBlob配置像这样的

const pathFile = RNFetchBlob.fs.dirs.MainBundleDir // Or .DocumentDir // Others is crashed my App 'Android';

回调"然后"获得成功,因此它是日志

The file is saved to /data/user/0/com.music/files/RNFetchBlobTmp_1ueoigyb8hoscuuizzsue.mp3

但当我试图在我的真实设备中浏览此文件时,我找不到它们,我不知道为什么!

所以在安卓系统中也有同样的问题吗?

startDownload = () => {
const {url} = this.state;
const pathFile = RNFetchBlob.fs.dirs.DocumentDir;
let date = new Date();
RNFetchBlob.config({
fileCache: true,
path:
pathFile +
'/me_' +
Math.floor(date.getTime() + date.getSeconds() / 2),
})
.fetch('GET', url)
.then(res => {
console.log('The file is save to ', res.path()); // It's log here but i can't see the file :
})
.catch(err => console.log('err', err));
};

我首先通过请求权限来解决这个问题,然后调用下载函数

但实际上我不知道他们为什么在请求许可之前告诉我先下载的文件:\

所以

requestToPermissions = async () => {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
{
title: 'Music',
message:
'App needs access to your Files... ',
buttonNeutral: 'Ask Me Later',
buttonNegative: 'Cancel',
buttonPositive: 'OK',
},
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
console.log('startDownload...');
this.startDownload();
}
} catch (err) {
console.log(err);
}
};

startDownload = () => {
const {tunes, token, currentTrackIndex} = this.state;
let {url, name} = tunes[currentTrackIndex];
RNFetchBlob.config({
fileCache: true,
appendExt: 'mp3',
addAndroidDownloads: {
useDownloadManager: true,
notification: true,
title: name,
path: RNFetchBlob.fs.dirs.DownloadDir + `${name}`, // Android platform
description: 'Downloading the file',
},
})
.fetch('GET', url)
.then(res => {
console.log('res', res);
console.log('The file is save to ', res.path());
});
};

相关内容

  • 没有找到相关文章

最新更新