Audio Recorder
React audio recorder on MediaRecorder with start, pause and stop, an elapsed timer, live waveform bars, playback with a scrubber, download and a max length.
Installation
npx shadcn@latest add @minidev/audio-recorder
MiniDev UI is in the shadcn registry directory, so the short form works with no setup. Older CLI versions can use the full URL: npx shadcn@latest add https://ui.minidev.pro/r/audio-recorder.json
Usage
import { AudioRecorder } from "@/components/ui/audio-recorder"
export function Example() {
return (
<AudioRecorder />
)
}
API reference
| Prop | Type | Default |
|---|---|---|
| maxDurationLongest recording in seconds. Recording stops by itself when it is reached. | number | 120 |
| onRecordedCalled with the finished recording and its object URL. | (blob: Blob, meta: { duration: number; url: string; mimeType: string }) => void | - |
| titleHeading of the card. | string | "Voice note" |
| descriptionShown under the title. | string | "Attach to invoice INV-2041 for North... |
| fileNameBase name of the downloaded file. The extension follows the recording format. | string | "voice-note" |
| barsNumber of waveform bars. | number | 48 |
| className | string | - |
When to use
Use to capture a voice note or memo in the browser (MediaRecorder + Web Audio, no dependencies). Prefer AudioPlayer to play existing audio. The finished waveform is a keyboard slider (Arrow keys seek 1s, Shift 5s, Space plays). Without a microphone or permission it stays idle with a plain-language hint. You own upload: take the Blob from onRecorded.
Keyboard interactions
| Key | Action |
|---|---|
| EnterorSpace | On the round button, starts or stops recording. After recording, plays or pauses. |
| ArrowRightorArrowUp | On the finished waveform, seeks forward 1 second, or 5 seconds with Shift. |
| ArrowLeftorArrowDown | On the finished waveform, seeks back 1 second, or 5 seconds with Shift. |
| HomeorEnd | On the finished waveform, jumps to the start or the end. |
| SpaceorEnter | On the finished waveform, plays or pauses the recording. |
Accessibility
- The round button is labelled "Start recording", "Stop recording", "Play" or "Pause" for its current job.
- The waveform is
role="img"until the recording is finished, then becomes a focusablerole="slider"witharia-valuetextlike "00:04 of 00:12" once the recording is done. - An
sr-onlypolite region announces Recording, Paused and Recorded. The hint line isaria-live="off"while recording, so the countdown is not read every second. - Under reduced motion the level bars repaint 12 times a second instead of 30, and the recording dot does not ping.
Frequently asked questions
How do I upload the recording?
Take the Blob from onRecorded(blob, { duration, url, mimeType }) and send it: form.append("audio", blob, "note.webm"). Don't store url. It is revoked when the visitor records again or the component unmounts.
Which audio format does it record?
The first type the browser's MediaRecorder supports from WebM/Opus, WebM, MP4, Ogg/Opus and Ogg. Read the real type from mimeType in onRecorded. The Download button names the file .webm, .m4a or .ogg to match.
Why does it say recording is not supported?
The browser needs navigator.mediaDevices.getUserMedia and MediaRecorder, which only exist on HTTPS pages and localhost. If the visitor blocks the microphone it explains how to allow it, and with no microphone it says so.
Dependencies
npm
Built from
Examples
Short limit with callback
maxDuration stops the recording on its own; onRecorded hands you the Blob and its metadata, which is what an upload needs.
Compact
Fewer bars and a narrower width fit the recorder into a sidebar or a comment composer.