在本机反应中下载音频文件?
Download Audio file in react native?
我正在尝试使用 rn-fetch-blob
下载音频文件 "mp3"。
所以我不止一个案例:)
当我尝试像这样向 RNFetchBlob 配置添加路径时
const pathFile = RNFetchBlob.fs.dirs.MainBundleDir // Or .DocumentDir // Others is crashed my App 'Android';
回调 "then" 成功所以它是日志
The file is saved to
/data/user/0/com.music/files/RNFetchBlobTmp_1ueoigyb8hoscuuizzsue.mp3
但是当我尝试在我的真实设备中浏览这个文件时,我找不到它们,我不知道为什么!
所以 Android 中有人遇到了类似的问题?
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());
});
};
我正在尝试使用 rn-fetch-blob
下载音频文件 "mp3"。
所以我不止一个案例:)
当我尝试像这样向 RNFetchBlob 配置添加路径时
const pathFile = RNFetchBlob.fs.dirs.MainBundleDir // Or .DocumentDir // Others is crashed my App 'Android';
回调 "then" 成功所以它是日志
The file is saved to /data/user/0/com.music/files/RNFetchBlobTmp_1ueoigyb8hoscuuizzsue.mp3
但是当我尝试在我的真实设备中浏览这个文件时,我找不到它们,我不知道为什么!
所以 Android 中有人遇到了类似的问题?
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());
});
};