React Native package to get music files from local and sd for iOS and Android
This package allow you to get music files from Android & iOS with following properties:
$ yarn add react-native-get-music-files
or
$ yarn add https://github.com/cinder92/react-native-get-music-files.git
info.plist
following permission
<key>NSAppleMusicUsageDescription</key>
<string>This permission is not needed by the app, but it is required by an underlying API. If you see this dialog, contact us.</string>
android/app/src/main/AndroidManifest.xml
and ensure to add this permission
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_MEDIA_AUDIO"/> <-- Add this for Android 13 and newer versions
As this package needs permissions from the device, please ensure that you asked for permissions before run any of this package functions.
SortSongFields {
TITLE, DURATION, ARTIST, GENRE, ALBUM
}
SortSongOrder {
ASC, DESC
}
import { getAll, getAlbums, searchSongs, SortSongFields, SortSongOrder } from "react-native-get-music-files";
const songsOrError = await getAll({
limit: 20,
offset: 0,
coverQuality: 50,
minSongDuration: 1000,
sortBy: SortSongFields.TITLE,
sortOrder: SortSongOrder.DESC,
});
// error
if (typeof songsOrError === 'string') {
// do something with the error
return;
}
const albumsOrError = await getAlbums({
limit: 10,
offset: 0,
coverQuality: 50,
artist: 'Rihanna',
sortBy: SortSongFields.ALBUM,
sortOrder: SortSongOrder.DESC,
});
// error
if (typeof albumsOrError === 'string') {
// do something with the error
return;
}
const resultsOrError = await searchSongs({
limit: 10,
offset: 0,
coverQuality: 50,
searchBy: '...',
sortBy: SortSongFields.DURATION,
sortOrder: SortSongOrder.DESC,
});
// error
if (typeof resultsOrError === 'string') {
// do something with the error
return;
}
MusicFiles returns an array of objects where you can loop, something like this.
[
{
title : "La danza del fuego",
author : "Mago de Oz",
album : "Finisterra",
genre : "Folk",
duration : 209120,
cover : "data:image/jpeg;base64, ....",
url : "/sdcard/0/la-danza-del-fuego.mp3"
}
]
Album
Type: Object
property | type | description |
---|---|---|
album | string | album name |
artist | string | author |
cover | string | base64 of the artwork |
numberOfSongs | number | number of songs in this album |
Song
property | type | description |
---|---|---|
title | string | title |
artist | string | artist |
album | string | album name |
duration | string | duration in ms |
genre | string | genre |
cover | string | base64 of the artwork |
url | string | path of the song |
async getAlbums(options) → {Promise<Album[] | string>}
options
Type: Object
property | type | description |
---|---|---|
artist | string | required |
limit | number | optional |
offset | number | required if limit set |
coverQuality | number | optional |
sortBy | string | optional |
sortOrder | string | optional |
returns
Type: Albums Error: string
async getAll(options) → {Promise<Song[] | string>}
options
Type: Object
property | type | description |
---|---|---|
limit | number | optional |
offset | number | required if limit set |
coverQuality | string | optional |
minSongDuration | number | optional |
sortBy | string | optional |
sortOrder | string | optional |
returns
Type: Song Error: string
async searchSongs(options) → { Promise<Song[] | string> }
options
Type: Object
property | type | description |
---|---|---|
searchBy | string | required |
limit | number | optional |
offset | number | required if limit set |
coverQuality | number | optional |
sortBy | string | optional |
sortOrder | string | optional |
returns Type: Song Error: string
https://github.com/zoontek/react-native-permissions
PR are welcome!