在React Native中下载音频文件?

奥利弗·D

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

所以我有不止一种情况:)

当我尝试像这样向RNFetchBlob配置添加路径时

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

回调“ then”获得成功,因此它是Log

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));
  };
奥利弗·D

我首先通过请求许可来解决此问题,然后调用下载功能,

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

所以

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());
      });
  };

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何获取在NsData中下载的音频文件?

来自分类Dev

如何在Android中下载音频文件

来自分类Dev

为什么我不能在React Native中共享音频文件?

来自分类Dev

是否可以在播放react-native-video之前就缓冲音频文件?

来自分类Dev

无法在Android React Native中下载JS包错误

来自分类Dev

ReCaptcha下载音频文件?

来自分类Dev

iOS-如何下载音频文件

来自分类Dev

AUSampler:引用外部(下载的)音频文件

来自分类Dev

下载锚标签在浏览器中打开文件,但不在 React 组件中下载

来自分类Dev

如何在不使用按钮的情况下在React中播放音频文件?

来自分类Dev

在 Xamarin iOS Native 中下载 Zip 文件

来自分类Dev

在ios中按顺序下载音频文件

来自分类Dev

从YouTube-DL下载的音频文件已损坏

来自分类Dev

Google云端存储:下载部分音频文件

来自分类Dev

允许用户仅通过密码下载视频/音频文件

来自分类Dev

如何从React Native Fetch Blob访问下载的文件?

来自分类Dev

验证音频文件

来自分类Dev

提供音频文件

来自分类Dev

连接音频文件

来自分类Dev

识别音频文件

来自分类Dev

连续播放音频文件(Ionic 3 Native Audio)

来自分类Dev

如何通过使用音频文件ID获取Soundcloud音频下载URL

来自分类Dev

将音频文件上传到Firebase Cloud数据库,然后将其从数据库中重新分发到React js +无限循环问题

来自分类Dev

在网站上查找音频文件的比特率,而无需下载文件

来自分类Dev

将音频文件拆分

来自分类Dev

从PHP读取音频文件

来自分类Dev

拿起Android音频文件

来自分类Dev

回形针音频文件上传

来自分类Dev

从Powerpoint提取音频文件