Skip to documentation content

Audio Player

Play a recording or a spoken reply in a compact row, with a scrub bar drawn from its loudness.

audio-player-demo

Overview

Voice replies, call recordings, and memos all need the same small player: play, see where you are, jump, and speed it up. Audio Player is that row. With peaks, the scrub bar is the file's waveform, so loud and quiet parts can be found by eye.

Give it a src and it plays the file in its own <audio> element. Leave src out and it is fully controlled: you pass the time, the length, and whether it plays, and it calls you back.

Anatomy

AudioPlayer
  • TogglePlay or pause
  • TitleAn optional line above the scrub bar
  • Scrub barA slider: the waveform from peaks, or a plain bar
  • TimeElapsed and total, in tabular numbers
  • SpeedSteps through the playback rates

AudioPlayer is one component. Installing audio-player brings Button along as a registry dependency.

Behavior

Owned or controlled

ModeSetThe player
OwnedsrcLoads the file, reads its length, plays, pauses, and seeks. onTimeUpdate, onPlayingChange, and onSeek report what happened
ControlledcurrentTime, duration, playingShows what you pass, and asks for changes through onPlayingChange and onSeek

A new src starts from the beginning. If the file cannot load, the scrub bar is replaced by an error and play is disabled.

Peaks

peaks is loudness from 0 to 1 across the file, such as the maximum of each slice of samples, normalized. More than 96 values are reduced to 96 bars, each keeping its slice's loudest value. Bars that have played are drawn in the text color; the rest are muted. Without peaks, the scrub bar is a plain track with a handle.

The player does not decode audio to find peaks. Compute them once, on your server or with AudioContext.decodeAudioData, and store them with the file.

Scrubbing

Press or drag on the scrub bar to move. The time follows the pointer and is committed on release, so a drag seeks once.

Speed

The speed button steps through rates, which defaults to 1ร—, 1.25ร—, 1.5ร—, 2ร—, and 0.75ร—. Control it with playbackRate and onPlaybackRateChange.

Accessibility

The toggle is a button named "Play" or "Pause". The scrub bar has the slider role, a name, and a value read as the time, such as "0:12 of 1:04". The time beside it is read once in the same words.

The error is an alert. Every string comes from labels, and formatAudioTime is exported for your own timestamps.

KeyWhereAction
SpaceToggle or scrub barPlay or pause
Right or UpScrub barForward by step, 5 seconds by default
Left or DownScrub barBack by step
Page Up or Page DownScrub barA tenth of the length forward or back
Home or EndScrub barThe start or the end

Installation

npx honestui@latest add audio-player

Usage

import { AudioPlayer } from "@/components/ai/audio-player"

<AudioPlayer src={memo.url} peaks={memo.peaks} title={memo.name} />

Controlled, with your own audio clock:

<AudioPlayer
  currentTime={time}
  duration={length}
  playing={playing}
  onPlayingChange={setPlaying}
  onSeek={seekTo}
/>

Examples

Plain scrub bar

A controlled player without peaks. A timer stands in for the audio clock.

audio-player-plain

API reference

AudioPlayer

Accepts native div props beyond those listed.

PropTypeDefaultDescription
srcstringNoneA file to play in an owned <audio> element.
titleReactNodeNoneA line above the scrub bar.
peaksnumber[]NoneLoudness from 0 to 1 across the file.
currentTimenumber0Seconds played, when there is no src.
durationnumber0Length in seconds, when there is no src.
playingbooleanfalseWhether it plays, when there is no src.
onPlayingChange(playing: boolean) => voidNoneCalled on play, pause, and end.
onSeek(seconds: number) => voidNoneCalled with the new position.
onTimeUpdate(seconds: number) => voidNoneCalled as the owned element plays.
playbackRatenumber1The speed.
onPlaybackRateChange(rate: number) => voidNoneCalled when the speed button is pressed.
ratesnumber[][1, 1.25, 1.5, 2, 0.75]The speeds, in order.
stepnumber5Seconds moved by the arrow keys.
labelsPartial<AudioPlayerLabels>EnglishButton names, the slider's name and value, the error, and the rate format.

Other exports

ExportShape
formatAudioTime(seconds: number) => string, such as "1:04" or "1:02:09"
AudioPlayerLabelsplay, pause, seek, position, speed, error, rate