Download Audio file in react native?

前端 未结 1 1016
一生所求
一生所求 2020-12-22 08:00

I\'m trying to download audio file \"mp3\" using rn-fetch-blob.

So I have more than one cases :)

When I trying to add a path to RNFetchBlob conf

相关标签:
1条回答
  • 2020-12-22 08:44

    I solve this issue by request for permission firstly then call the download function,

    But actually I don't know why they tell me the file downloaded firstly before asking for permission :\

    So

    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());
          });
      };
    
    0 讨论(0)
提交回复
热议问题