Skip to content

Basic Usage ​

This guide covers the core idea — the browse tree — and the minimal code to set up the player, define some content, and play it. Installation is in Getting Started.

The browse tree ​

The library models all content as one navigable tree of Tracks. A Track is browsable or playable, depending on which field you set:

  • Browsable — has a path; navigating into it resolves its children.
  • Playable — has a src; the player can stream it.

Set exactly one. Track → Browsable or playable covers a Track that sets both.

Three fields at a glance — each answers a different question about a Track:

FieldAnswersMeaning
pathwhere does it go?the browse-tree address navigating opens
srcwhat does it play?the media identifier the player streams
idwhich item is it?identity across surfaces — falls back to src when unset
Browsable: opens to children Playable: streams audio

One tree powers both your in-app UI and the native browse views on CarPlay and Android Auto.

Set up the player ​

Call setupPlayer() once at startup to initialize the player:

ts
import { setupPlayer } from 'react-native-audio-browser'

await setupPlayer()

Define a browse tree ​

Describe the tree with configureBrowser: tabs along the top, and routes that resolve each path to its children. The simplest source is static data declared inline:

ts
import { configureBrowser } from 'react-native-audio-browser'

configureBrowser({
  tabs: [{ title: 'Browse', path: '/browse' }],
  routes: {
    '/browse': {
      path: '/browse',
      title: 'Browse',
      children: [
        {
          title: 'Jazz',
          // A path makes this browsable — tapping it opens that path.
          path: '/browse/jazz'
        },
        {
          title: 'Smooth Floret FM',
          // A src makes this playable — tapping it streams the track.
          src: 'https://example.com/floret.mp3'
        }
      ]
    },
    '/browse/jazz': {
      path: '/browse/jazz',
      title: 'Jazz',
      children: [
        {
          title: 'The Stalk 88.5',
          src: 'https://example.com/stalk.mp3'
        }
      ]
    }
  }
})

A route value is a BrowserSource: the static page object shown above, an async callback that returns one ('/path': async ({ path, routeParams }) => ({ path, title, children })), or an HTTP request config that fetches it from your API — handy for trees too large to declare upfront.

A page can also group its tracks into titled, styled sections (sections: [{ title, style, children }, …]) — a flat children list like the ones above is shorthand for a single untitled section. See Browser → How a path resolves.

Play a track ​

On CarPlay and Android Auto, tapping a playable Track plays it for you. To drive playback from your own UI, set a queue and call play:

ts
import { setQueue, play } from 'react-native-audio-browser'

setQueue([{ title: 'Smooth Floret FM', src: 'https://example.com/floret.mp3' }])
play()

The queue ​

The player works through a queue of Tracks. The Track at the current position is the active track — it's what plays, and what external next/previous controls (car, headphones, lock screen) move between.

Now Playing is the metadata shown on the lock screen, notification, and car surfaces. By default it mirrors the active track; for live streams you can override it as the current song changes — see Now Playing.

Read state in your UI ​

Reactive hooks keep your in-app UI in sync with playback — they re-render automatically as it changes:

tsx
import { Button } from 'react-native'
import {
  togglePlayback,
  usePlayingState,
  useActiveTrack
} from 'react-native-audio-browser'

function PlayPauseButton() {
  const { playing } = usePlayingState()
  const track = useActiveTrack()

  return (
    <Button
      title={playing ? `Pause ${track?.title ?? ''}` : 'Play'}
      onPress={() => togglePlayback()}
    />
  )
}

See the API reference for the full set of hooks (useProgress, useQueue, useNowPlaying, and more).