Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to download an mp3 file in react native

// use this package rn-fetch-blob
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());
      });
  };
Comment

PREVIOUS NEXT
Code Example
Javascript :: jquery find attribute from siblings 
Javascript :: nestjs prisma on query 
Javascript :: antd: editable table example 
Javascript :: google maps load kml file javascript 
Javascript :: Update failed: ChunkLoadError: Loading hot update chunk app failed. 
Javascript :: angular json and cli json file 
Javascript :: angular 11 support versions nodejs 
Javascript :: usestate previous state 
Javascript :: Cypress failed to make a connection to the Chrome DevTools Protocol after retrying for 50 seconds. 
Javascript :: ejemplo async await javascript 
Javascript :: javascript reduce function array 
Javascript :: uiimage from assets 
Javascript :: react table className 
Javascript :: try catch 
Javascript :: Backbone Router 
Javascript :: best way to setup nextjs project 
Javascript :: how to strip html tags in javascript 
Javascript :: option selected aotu value 
Javascript :: environment variable to debug knex 
Javascript :: redirect all routes to main component vue 
Javascript :: jquery elements which id doesnt contain string 
Javascript :: loop over a nerber in react 
Javascript :: node js install aws-sdk 
Javascript :: javascript getter arrow function 
Javascript :: could not decode base64 cloudinary 
Javascript :: react-router in saga 
Javascript :: asynchronous function using function constructor 
Javascript :: Program to find GCD or HCF of two numbers javascript 
Javascript :: uuid react native expo 
Javascript :: javascript function with parameters 
ADD CONTENT
Topic
Content
Source link
Name
9+3 =