Player Setup
Configure and initialize the player before any other API calls.
setupPlayer
| Field | Type | Default | Description |
|---|---|---|---|
| contentType | 'music' | 'speech' | — | Content type hint for the system. Affects audio routing and focus behavior. - 'music': Standard music playback (default) - 'speech': Spoken content like podcasts/audiobooks On iOS this sets the AVAudioSession mode (.default vs .spokenAudio). On Android this sets AudioAttributes contentType (MUSIC vs SPEECH). |
| handleAudioBecomingNoisy | boolean | — | Whether the player should automatically pause when audio output is rerouted (e.g. headphones disconnected). Defaults to true. |
| autoUpdateMetadataFromStream v5.1.0+ | boolean | true | When `true` (default), ICY/ID3 metadata updates the queued MediaItem and Now Playing. When `false`, only Event.MetadataReceived is emitted; call updateMetadata yourself. |
| cache | CacheConfig | — | Enable disk caching and preloading. When set, played audio is written through to a disk cache and the next queue item is automatically preloaded. Previously cached content is served from disk on subsequent plays. On Android this uses Media3's SimpleCache + CacheDataSource. On iOS this uses an AVAssetResourceLoaderDelegate-based streaming cache. |
| progressSync | ProgressSyncConfig | — | — |
| android | Partial<AndroidPlayerConfig> | — | — |
CacheConfig
| Field | Type | Default | Description |
|---|---|---|---|
| maxSizeBytes | number | 524288000 (500 MB) | Maximum disk cache size in bytes. When the cache exceeds this size, least-recently-used items are evicted. |
| preloading | PreloadConfig | — | Preloading configuration. Requires cache to be enabled. |
ProgressSyncConfig
| Field | Type | Default | Description |
|---|---|---|---|
| intervalSeconds | number | — | — |
| http | { url: string; headers?: Record<string, string> } | — | — |
The HTTP POST body is JSON with the following shape:
{ "mediaId": "ep-123", "position": 142.5, "duration": 3600.0, "timestamp": 1710000000000 } This is the same payload emitted by the PlaybackProgressUpdated event. A final tick is always sent on pause or stop.
AndroidPlayerConfig
| Field | Type | Default | Description |
|---|---|---|---|
| wakeMode * | 'none' | 'local' | 'network' | 'none' | — |
| skipSilenceEnabled * | boolean | false | — |
| notification | { channelId: string; channelName: string; smallIcon: string; } | — | Android notification configuration. |
| cast | string | — | Chromecast receiver app ID. When set, Cast support is enabled and the `<OutputDeviceButton>` / `<CastButton>` will filter for this receiver. Use `DEFAULT_CAST_RECEIVER_APP_ID` for Google's Default Media Receiver (no registration required). Omit to disable Cast entirely. |
notification is an object with channelId, channelName, and smallIcon (Android resource name, e.g. 'ic_stat_music_note').
Example
TrackPlayer.setupPlayer({
contentType: 'music',
handleAudioBecomingNoisy: true,
cache: { maxSizeBytes: 200 * 1024 * 1024 }, // 200 MB
progressSync: {
intervalSeconds: 30,
http: {
url: 'https://api.example.com/progress',
headers: { 'Authorization': 'Bearer xxx' },
},
},
android: {
wakeMode: 'network',
notification: {
channelId: 'com.myapp.audio',
channelName: 'Audio Playback',
smallIcon: 'ic_notification',
},
},
}); destroy
Tears down the player and releases all resources. After calling this, you must call setupPlayer() again to use the player.
TrackPlayer.destroy(); setCommands
Configure which commands appear on the lock screen and in the notification.
| Field | Type | Default | Description |
|---|---|---|---|
| capabilities * | PlayerCommand[] | — | Which commands are available in the notification/remote controls. |
| handling | RemoteControlHandling | 'native' | Who handles remote control button presses. |
| perCommandHandling | PerCommandHandling | — | Per-command handling overrides (only used when handling is 'hybrid'). |
| forwardInterval | number | — | Interval in seconds for the skip-forward button (default: 15). Only used when capabilities includes SkipForward. |
| backwardInterval | number | — | Interval in seconds for the skip-backward button (default: 15). Only used when capabilities includes SkipBackward. |
handling controls who handles remote commands. 'native' (default) lets the system act automatically with no background handler needed. 'js' routes all events to your background handler. 'hybrid' does both, with per-command overrides via perCommandHandling.
Available PlayerCommand values: PlayerCommand.Seek, PlayerCommand.PlayPause, PlayerCommand.Next, PlayerCommand.Previous, PlayerCommand.Stop, PlayerCommand.SkipForward, PlayerCommand.SkipBackward.
import { PlayerCommand } from '@rntp/player';
TrackPlayer.setCommands({
capabilities: [
PlayerCommand.PlayPause,
PlayerCommand.Next,
PlayerCommand.Previous,
PlayerCommand.Seek,
],
forwardInterval: 30,
backwardInterval: 15,
}); updateProgressSyncHeaders
Update the HTTP headers used for progress sync POST requests at runtime — useful when auth tokens refresh.
TrackPlayer.updateProgressSyncHeaders({ Authorization: 'Bearer new-token' }); Caching
When cache is configured, audio is stored locally after the first download. Subsequent plays of the same URL are served from cache without a network request.
TrackPlayer.setupPlayer({
cache: { maxSizeBytes: 500 * 1024 * 1024 }, // 500 MB
}); TrackPlayer.clearCache(); useProgress hook exposes a cached field (in seconds) indicating how much of the current track is stored locally. Caching works regardless of whether the server provides a Content-Length header — the total size is determined when the download completes.Preloading
Preloading downloads tracks to cache before they’re played, reducing gaps between tracks and enabling instant playback from browse screens.
Auto preloading
Automatically preload the next tracks in the queue:
| Field | Type | Default | Description |
|---|---|---|---|
| window | number | 0 (disabled) | Auto-preload next N tracks in queue. Preloading starts when the current item is live, a local file, or fully cached on disk. It also re-evaluates when the queue changes (items added, removed, replaced, or reordered). |
TrackPlayer.setupPlayer({
cache: {
preloading: {
window: 1, // preload next 1 track
},
},
}); Auto preloading starts when the current track is fully cached, is a live stream, or is a local file. It also re-evaluates when the queue changes (items added, removed, replaced, or reordered). For on-demand streams, preloading waits until the current track finishes downloading to avoid competing for bandwidth.
Manual preloading
Preload any track on demand — useful for browse screens, recommendations, or any UI where the user might play a track:
| Field | Type | Default | Description |
|---|---|---|---|
| duration | number | — | Target duration in seconds. If omitted, preloads the entire file. |
// Preload entire file
TrackPlayer.preload(mediaItem);
// Preload approximately 30 seconds
TrackPlayer.preload(mediaItem, { duration: 30 });
// Cancel an in-progress preload
TrackPlayer.cancelPreload(mediaItem); Manual preloading works independently of the queue. The cache is keyed by URL, so any future play of the same URL will be a cache hit.
cache to be configured in setupPlayer.