Audio Player
Play a recording or a spoken reply in a compact row, with a scrub bar drawn from its loudness.
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
- 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
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.
Installation
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.
API reference
AudioPlayer
Accepts native div props beyond those listed.