【问题标题】:Download Audio file in react native?在本机反应中下载音频文件?
【发布时间】:2020-06-13 00:58:23
【问题描述】:

我正在尝试使用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));
  };

【问题讨论】:

    标签: javascript android react-native rn-fetch-blob


    【解决方案1】:

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

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

    所以

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-24
      • 1970-01-01
      • 1970-01-01
      • 2020-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多