### Start Metro Server for Example App
Source: https://github.com/nodefinity/react-native-music-library/blob/main/CONTRIBUTING.md
Starts the Metro bundler to serve the example application. This is necessary to run the example app on a device or emulator.
```sh
yarn example start
```
--------------------------------
### Fetch and Display All Tracks
Source: https://github.com/nodefinity/react-native-music-library/blob/main/docs/docs/getting-started.md
Retrieves all available music tracks from the device and iterates through them, logging the title, artist, duration, and file URL for each track. Includes error handling for the asynchronous operation.
```javascript
const loadTracks = async () => {
try {
const result = await getTracksAsync();
result.items.forEach(track => {
console.log(`${track.title} by ${track.artist}`);
console.log(`Duration: ${Math.floor(track.duration / 60)}:${track.duration % 60}`);
console.log(`File: ${track.url}`);
});
} catch (error) {
console.error('Failed to load tracks:', error);
}
};
```
--------------------------------
### Install iOS CocoaPods Dependencies
Source: https://github.com/nodefinity/react-native-music-library/blob/main/example/README.md
Installs necessary CocoaPods dependencies for the iOS build. This is required before running the iOS app, especially after cloning the project or updating native dependencies. Uses Ruby's bundle command.
```shell
# Install CocoaPods itself (first time)
bundle install
# Install project dependencies
bundle exec pod install
```
--------------------------------
### Import Core Functions from React Native Music Library
Source: https://github.com/nodefinity/react-native-music-library/blob/main/docs/docs/getting-started.md
Imports essential functions for interacting with the music library, including fetching tracks, albums, and artists, as well as getting specific track metadata.
```javascript
import {
getTracksAsync,
getAlbumsAsync,
getArtistsAsync,
getTrackMetadataAsync
} from '@nodefinity/react-native-music-library';
```
--------------------------------
### Install React Native Music Library
Source: https://github.com/nodefinity/react-native-music-library/blob/main/README.md
Instructions for installing the library using npm or yarn.
```bash
npm install @nodefinity/react-native-music-library
# or
yarn add @nodefinity/react-native-music-library
```
--------------------------------
### Start Metro Bundler in React Native
Source: https://github.com/nodefinity/react-native-music-library/blob/main/example/README.md
Starts the Metro JavaScript bundler, which is essential for serving your React Native application to the emulator/device. This command can be run using either npm or Yarn.
```shell
# Using npm
npm start
# OR using Yarn
yarn start
```
--------------------------------
### Fetch and Display Albums
Source: https://github.com/nodefinity/react-native-music-library/blob/main/docs/docs/getting-started.md
Fetches music albums, optionally sorted by title in ascending order and limited to the first 50 results. It then logs the title, artist, and track count for each album. Error handling is included.
```javascript
const loadAlbums = async () => {
try {
const result = await getAlbumsAsync({
sortBy: ['title', true], // Sort by title ascending
first: 50
});
result.items.forEach(album => {
console.log(`${album.title} by ${album.artist}`);
console.log(`Tracks: ${album.trackCount}`);
});
} catch (error) {
console.error('Failed to load albums:', error);
}
};
```
--------------------------------
### Fetch and Display Artists
Source: https://github.com/nodefinity/react-native-music-library/blob/main/docs/docs/getting-started.md
Retrieves a list of artists from the music library, sorted by their name. It then logs the artist's title, the number of albums they have, and the total number of tracks. Includes error handling.
```javascript
const loadArtists = async () => {
try {
const result = await getArtistsAsync({
sortBy: 'title' // Sort by name
});
result.items.forEach(artist => {
console.log(`${artist.title}`);
console.log(`Albums: ${artist.albumCount}, Tracks: ${artist.trackCount}`);
});
} catch (error) {
console.error('Failed to load artists:', error);
}
};
```
--------------------------------
### Run iOS App in React Native
Source: https://github.com/nodefinity/react-native-music-library/blob/main/example/README.md
Builds and runs the React Native application on an iOS simulator or device. This command is executed after ensuring all CocoaPods dependencies are installed. Supports both npm and Yarn.
```shell
# Using npm
npm run ios
# OR using Yarn
yarn ios
```
--------------------------------
### iOS Info.plist Configuration for Music Access
Source: https://github.com/nodefinity/react-native-music-library/blob/main/docs/docs/getting-started.md
Adds the necessary key to the `Info.plist` file for iOS applications to request user permission for accessing their music library. This string will be displayed to the user when the permission prompt appears.
```xml
NSAppleMusicUsageDescriptionThis app needs access to your music library to play songs
```
--------------------------------
### Run Example App on iOS
Source: https://github.com/nodefinity/react-native-music-library/blob/main/CONTRIBUTING.md
Builds and runs the example application on an iOS device or simulator. Ensure you have an iOS development environment set up (Xcode).
```sh
yarn example ios
```
--------------------------------
### Android Permissions for Music Library Access
Source: https://github.com/nodefinity/react-native-music-library/blob/main/docs/docs/getting-started.md
Configures necessary permissions in `AndroidManifest.xml` for Android to access audio media. It includes granular permissions for Android 13+ and traditional storage permissions for older versions.
```xml
```
--------------------------------
### Run Example App on Android
Source: https://github.com/nodefinity/react-native-music-library/blob/main/CONTRIBUTING.md
Builds and runs the example application on an Android device or emulator. Ensure you have an Android development environment set up.
```sh
yarn example android
```
--------------------------------
### Run Android App in React Native
Source: https://github.com/nodefinity/react-native-music-library/blob/main/example/README.md
Builds and runs the React Native application on an Android device or emulator. This command should be executed in a separate terminal after Metro is running. Supports both npm and Yarn.
```shell
# Using npm
npm run android
# OR using Yarn
yarn android
```
--------------------------------
### Request Music Library Permission in React Native
Source: https://github.com/nodefinity/react-native-music-library/blob/main/docs/docs/getting-started.md
Demonstrates how to request audio media permissions at runtime in a React Native application using the `react-native-permissions` library. It checks the result and logs whether the permission was granted or denied.
```javascript
import { request, PERMISSIONS, RESULTS } from 'react-native-permissions';
const requestMusicPermission = async () => {
const result = await request(PERMISSIONS.ANDROID.READ_MEDIA_AUDIO);
if (result === RESULTS.GRANTED) {
console.log('Music permission granted');
} else {
console.log('Music permission denied');
}
};
```
--------------------------------
### Install Project Dependencies with Yarn
Source: https://github.com/nodefinity/react-native-music-library/blob/main/CONTRIBUTING.md
Installs all necessary dependencies for the monorepo project using Yarn workspaces. This command should be run in the root directory. Note that npm is not supported for development due to Yarn workspaces.
```sh
yarn
```
--------------------------------
### Music Library Sorting Examples (JavaScript)
Source: https://github.com/nodefinity/react-native-music-library/blob/main/docs/docs/api.md
Demonstrates how to specify sorting criteria for music library items. This includes single keys, keys with direction, and complex arrays for multi-level sorting. These examples are crucial for implementing ordered lists of tracks, albums, or artists.
```javascript
// Single sort key (descending by default)
sortBy: 'artist'
// Single sort key with direction
sortBy: ['artist', true] // ascending
sortBy: ['artist', false] // descending
// Multiple sort criteria
sortBy: [
['artist', true],
['album', true],
'duration'
]
```
--------------------------------
### Basic Usage of Music Library Functions
Source: https://github.com/nodefinity/react-native-music-library/blob/main/README.md
Demonstrates how to import and use core functions like getTracksAsync, getAlbumsAsync, and getArtistsAsync to retrieve music data. Shows an example of sorting albums by title.
```javascript
import {
getTracksAsync,
getAlbumsAsync,
getArtistsAsync
} from '@nodefinity/react-native-music-library';
// Get tracks
const tracks = await getTracksAsync();
// Get albums with sorting
const albums = await getAlbumsAsync({
sortBy: ['title', true], // Sort by title ascending
first: 50
});
// Get artists
const artists = await getArtistsAsync();
```
--------------------------------
### Basic Usage of React Native Music Library APIs
Source: https://github.com/nodefinity/react-native-music-library/blob/main/docs/docs/intro.md
Demonstrates how to import and use core functions like getTracksAsync, getAlbumsAsync, and getArtistsAsync from the library to retrieve music data. It includes examples of basic retrieval and retrieval with sorting options.
```javascript
import { getTracksAsync, getAlbumsAsync, getArtistsAsync } from '@nodefinity/react-native-music-library';
// Get all tracks
const tracks = await getTracksAsync();
// Get albums with sorting
const albums = await getAlbumsAsync({
sortBy: ['title', true], // Sort by title ascending
first: 50
});
// Get artists
const artists = await getArtistsAsync();
```
--------------------------------
### GET /tracks
Source: https://context7.com/nodefinity/react-native-music-library/llms.txt
Retrieves music tracks from the device with pagination, sorting, and filtering options. Returns a paginated result with cursor support for efficient loading of large collections.
```APIDOC
## GET /tracks
### Description
Retrieves music tracks from the device with pagination, sorting, and filtering options. Returns a paginated result with cursor support for efficient loading of large collections.
### Method
GET
### Endpoint
/tracks
### Parameters
#### Query Parameters
- **first** (number) - Optional - The number of tracks to retrieve. Defaults to 20.
- **sortBy** (array) - Optional - An array of sorting criteria. Each criterion can be a string (field name, defaults to descending) or an array `[fieldName, ascendingBoolean]`.
Example: `[['artist', true], ['album', true], 'duration']`
- **directory** (string) - Optional - Filters tracks by a specific directory.
- **after** (string) - Optional - A cursor for pagination, used to fetch the next page of results.
### Request Example
```javascript
// Basic usage
const result = await getTracksAsync();
// Advanced usage
const tracks = await getTracksAsync({
first: 50,
sortBy: ['artist', true],
directory: '/Music/Favorites',
after: 'some-cursor-string'
});
```
### Response
#### Success Response (200)
- **items** (array