Skip to Content
NarakUI

Audio Player

Play an audio file in a single control bar. The bar comes styled, and you can replace or recompose its controls.

Play an audio file in a single control bar. The bar comes styled, and you can replace or recompose its controls.

Default player

The audio player is one bar with play, skip back and forward 10 seconds, the timeline, playback speed, and volume.

How the pieces fit together

An audio player has three required parts: the player, the skin, and the media. Each part has one job and nests inside the part above it, so you can change one without touching the others.

<AudioPlayer>
  <AudioSkin className="w-full">
    <Audio src={src} />
  </AudioSkin>
</AudioPlayer>
PartComponentWhat it owns
PlayerAudioPlayerPlayback state for everything inside it. It renders no UI.
SkinAudioSkinThe interface: control bar, error state, shortcuts.
MediaAudioThe source. It renders the native audio element and nothing else.
AdditionsUserPreferencesOptional behavior. Render it inside the skin.

Any component inside the player can read playback state with usePlayer, even outside the skin. A transcript that follows the current time is one example:

import { selectTime, usePlayer } from '@/components/ui/audio-player';

const currentTime = usePlayer((state) => selectTime(state)?.currentTime);

Size the bar

Put the width on the skin with className. The skin needs no wrapper element. The controls set the height, so the bar never stretches to fill a taller parent.

<AudioSkin className="w-full">
  <Audio src={src} />
</AudioSkin>

Responsive layout

The bar responds to its own width (@container/audio-skin), not the page. Below 512px wide the skip buttons drop, then the volume slider (440px) and time labels (360px), so the timeline keeps its room.

Internationalization

Pass locale to AudioSkin to translate every label, tooltip, and screen reader announcement. The bar supports right-to-left (RTL) languages: with an RTL locale it sets its own text direction.

<AudioSkin className="w-full" locale={locale}>
  <Audio src={src} />
</AudioSkin>

Remember listener preferences

Render UserPreferences inside the skin to keep the listener’s volume, mute, and speed from one player to the next, across page loads and visits:

import { Audio, AudioSkin, UserPreferences } from '@/components/ui/audio-player';

<AudioSkin className="w-full">
  <Audio src={src} />
  <UserPreferences />
</AudioSkin>;

Keyboard shortcuts

The skin binds the playback shortcuts: play or pause (Space, K), seek (ArrowLeft/ArrowRight, J/L), volume (ArrowUp/ArrowDown), mute (M), jump to a percentage (0–9), and speed (</>). They work while focus is inside the bar, so two players on one page don’t compete for keys.

Player states

The skin and its default bar show these states without any code from you.

Buffering

When playback waits for data for more than half a second, the brand mark covers the play button and draws itself in a loop. The indicator is part of DefaultAudioControls, so a bar you build yourself has to render BufferingIndicator to keep it.

Error

When the source can’t play, the bar shows a short message in place of its controls.

Build your own control bar

Not recommended

The default bar keeps every player in the product consistent. Replace it only when a surface can’t use it, such as a row too narrow for the full bar.

DefaultAudioControls is the bar the skin renders by default. To replace it, compose the styled controls into your own bar and pass it to the skin through controls.

Installation

pnpm dlx shadcn@latest add @narakui/audio-player

Add "@narakui": "https://ui.narakcode.dev/r/{name}.json" to components.json, or install directly from https://ui.narakcode.dev/r/audio-player.json.

Reference

Exports

ExportImportWhat it does
AudioPlayer@/components/ui/audio-playerOwns the playback state. Renders no UI.
AudioSkin@/components/ui/audio-playerThe control bar, the error state, and the keyboard shortcuts.
Audio@/components/ui/audio-playerPlays an audio file through the native audio element.
usePlayer@/components/ui/audio-playerReads playback state and actions from any component inside AudioPlayer.
audioFeatures@/components/ui/audio-playerThe feature set descriptor behind AudioPlayer.
DefaultAudioControls@/components/ui/audio-playerThe default bar. Replace it through controls on AudioSkin.
UserPreferences@/components/ui/audio-playerPersists volume, mute, and playback speed across sessions.

AudioSkin Props

PropTypeDefaultWhat it does
classNamestring—Sets the width of the bar, for example w-full.
controlsReactNode<DefaultAudioControls />Replaces the default bar.
localestringPage languageThe language of labels, announcements, and RTL direction.

Audio Props

Audio takes src, ref, and the standard props of the native <audio> element.