Cross Platform audio streaming Module for React native. Provides audio playback, external media controls, background mode and more!
- Lightweight - Complete audio library without managing queue, the code is optimized to use least resources
- Background play - Audio can be playing in the background and media can be controlled externally as well
- Local or network, files or streams - Support for online streaming and offline files
- Multi-platform - Supports Android, iOS and Web
- Supports React Hooks 🎣 - Includes React Hooks for common use-cases so you don’t have to write them
import { TrackPlayer } from "react-track-player";
load = () => {
TrackPlayer.load({
title: "Awesome song",
artist: "Mr. Awesome",
album: "Awesome songs only",
cover: "https://source.unsplash.com/random",
path: "https://dl.dropboxusercontent.com/s/8avcnxmjtdujytz/Sher%20Aaya%20Sher.mp3?dl=0",
}).then(() => {
console.log("audio loaded");
});
};
play = () => {
TrackPlayer.play();
};
pause = () => {
TrackPlayer.pause();
};
Install the module using yarn or npm
npm install react-track-player --save
yarn add react-track-player
Issues while installing might be listed in troubleshooting page.
First of all, you need to set up the player. This usually takes less than a second:
import TrackPlayer from 'react-track-player';
await TrackPlayer.setup({})
// The player is ready to be used
import { addEventListener } from "react-track-player";
subscription = addListener("media", function (event) {
// handle event
console.log("from event listener", event);
if (event == "skip_to_next") {
skipToNext();
} else if (event == "skip_to_previous") {
skipToPrevious();
} else if (event == "completed") {
skipToNext();
} else {
updateStatus();
}
});
subscription.remove();
TrackPlayer.load({
title: "Awesome song",
artist: "Mr. Awesome",
album: "Awesome songs only",
cover: "https://source.unsplash.com/random",
path: "https://dl.dropboxusercontent.com/s/8avcnxmjtdujytz/Sher%20Aaya%20Sher.mp3?dl=0",
})
Load a track into track player. Audio wont be played untill play()
function is called.
Returns: Promise
- The promise resolves if it is sucess
Param | Type | Description |
---|---|---|
title | string | Track title |
artist | string | Name of the artist |
album | string | Name of the album |
cover | string | Audio cover image path |
path | string | Audio url or path for native audio files |
TrackPlayer.getState()
This method can be used to get the state of the player
Returns: Promise
TrackPlayer.getPosition();
Get track player progress position.
Returns: Promise
TrackPlayer.getDuration();
Gets the duration of the current track in seconds.
Returns: Promise
TrackPlayer.destroy()
Destroys the player, cleaning up its resources. After executing this function, you won’t be able to use the player anymore, unless you call setup() again. Get track duration.
Returns: Promise
usePlaybackState gives the state of the player
useProgress accepts an interval to set the rate (in miliseconds) to poll the track player’s progress. The default value is 1000 or every second.
import React from 'react';
import { Text, View } from 'react-native';
import { TrackPlayer, useProgress } from 'react-track-player';
const MyComponent = () => {
const { position, duration } = useProgress()
return (
<View>
<Slider
style={{ width: '100%', height: 40 }}
minimumValue={0}
maximumValue={duration}
value={position}
onSlidingComplete={value => {
TrackPlayer.pause();
TrackPlayer.seekTo(value)
TrackPlayer.play();
}}
/>
<Text>Track progress: {position} seconds out of {duration} total</Text>
</View>
)
}