react-nativereact-native-fsreact-native-sound

which react native library should i use to access music files locally stored on android


I want to access and run music files stored locally on android and ios devices. I tried using react-native-fs but couldn't managed to access. So are there any other better libraries available to use it.


Solution

  • Answering my own question. I was finally able to access the music files / (or any files stored on an android device) in react native using react-native-fs library.

    I stumbled upon quite a few times using API constants provided such as MainBundlePath, DocumentDirectoryPath, ExternalDirectoryPath to name a few except trying ExternalStorageDirectoryPath which was really needed.

    So below is the code (Provided by react-native-fs git repo):

    RNFS.readDir(RNFS.ExternalStorageDirectoryPath)
    .then((result) => {
    console.log('GOT RESULT', result);
    return Promise.all([RNFS.stat(result[0].path), result[0].path]);
    })
    .then((statResult) => {
    if (statResult[0].isFile()) {
    return RNFS.readFile(statResult[1], 'utf8');
    }
    return 'no file';
    })
    .then((contents) => {
    console.log(contents);
    })
    .catch((err) => {
    console.log(err.message, err.code);
    });