# 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.

- Kind: Component (Media)
- Docs: https://ui.minidev.pro/docs/audio-recorder
- Source (registry JSON with the full file): https://ui.minidev.pro/r/audio-recorder.json
- License: MIT. One file, yours to edit after install.

## When to use

Use to capture a voice note or memo in the browser (MediaRecorder + Web Audio, no dependencies). Prefer [`AudioPlayer`](https://ui.minidev.pro/docs/audio-player) 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`.

## Installation

### shadcn CLI (copies the source into your project)

```bash
npx shadcn@latest add @minidev/audio-recorder
```

Writes `components/ui/audio-recorder.tsx` and installs its npm dependencies. Older CLI versions: `npx shadcn@latest add https://ui.minidev.pro/r/audio-recorder.json`.

### npm package

```bash
npm i minidev-ui-kit lucide-react motion
```

Add the tokens to your global stylesheet once:

```css
@import "tailwindcss";
@import "minidev-ui-kit/styles.css";
@source "../node_modules/minidev-ui-kit";
```

Next.js also needs `transpilePackages: ["minidev-ui-kit"]` in next.config.

## Import

After the shadcn install:

```tsx
import { AudioRecorder } from "@/components/ui/audio-recorder"
```

From the npm package:

```tsx
import { AudioRecorder } from "minidev-ui-kit/ui/audio-recorder"
```

## Usage

```tsx
import { AudioRecorder } from "@/components/ui/audio-recorder"

export function Example() {
  return (
    <AudioRecorder />
  )
}
```

## Props

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| `maxDuration` | `number` | `120` | no | Longest recording in seconds. Recording stops by itself when it is reached. |
| `onRecorded` | `(blob: Blob, meta: { duration: number; url: string; mimeType: string }) => void` | - | no | Called with the finished recording and its object URL. |
| `title` | `string` | `"Voice note"` | no | Heading of the card. |
| `description` | `string` | `"Attach to invoice INV-2041 for North...` | no | Shown under the title. |
| `fileName` | `string` | `"voice-note"` | no | Base name of the downloaded file. The extension follows the recording format. |
| `bars` | `number` | `48` | no | Number of waveform bars. |
| `className` | `string` | - | no |  |

## 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.

```tsx
function UploadDemo() {
  const [note, setNote] = React.useState<{ duration: number; size: number; type: string } | null>(null)
  return (
    <div className="flex w-full max-w-md flex-col gap-2">
      <AudioRecorder
        maxDuration={30}
        title="Quick reply"
        description="Up to 30 seconds, sent to Theo Okafor"
        fileName="reply-theo-okafor"
        onRecorded={(blob, meta) => setNote({ duration: meta.duration, size: blob.size, type: meta.mimeType })}
      />
      <p className="text-xs text-fg-muted" aria-live="polite">
        {note ? `${note.duration.toFixed(1)} s, ${Math.max(1, Math.round(note.size / 1024))} KB of ${note.type.split(";")[0]} ready to upload` : "Nothing recorded yet."}
      </p>
    </div>
  )
}
```

### Compact

Fewer bars and a narrower width fit the recorder into a sidebar or a comment composer.

```tsx
<AudioRecorder bars={24} title="Pronunciation" description="Say the name once" fileName="northwind-pronunciation" className="max-w-xs" />
```

## Keyboard interactions

| Key | Action |
| --- | --- |
| `Enter` or `Space` | On the round button, starts or stops recording. After recording, plays or pauses. |
| `ArrowRight` or `ArrowUp` | On the finished waveform, seeks forward 1 second, or 5 seconds with Shift. |
| `ArrowLeft` or `ArrowDown` | On the finished waveform, seeks back 1 second, or 5 seconds with Shift. |
| `Home` or `End` | On the finished waveform, jumps to the start or the end. |
| `Space` or `Enter` | 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 focusable `role="slider"` with `aria-valuetext` like "00:04 of 00:12" once the recording is done.
- An `sr-only` polite region announces Recording, Paused and Recorded. The hint line is `aria-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.

## FAQ

### 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.

## Rules for generating code

- Use semantic tokens only (bg, surface, sunken, border, fg, fg-muted, fg-subtle, ink, accent, success, warning, danger). Never raw hex or palette steps like blue-500.
- Shadows only from the set: shadow-xs, shadow-sm, shadow-raised, shadow-md, shadow-lg, shadow-overlay.
- Numbers use tabular-nums. Dark mode is the `dark` class on <html>; components never branch on theme.

## Dependencies

- npm: `lucide-react`, `motion`

## Related

- [Audio Player](https://ui.minidev.pro/docs/audio-player/md): Minimal React audio player row with a play and pause toggle, title, subtitle and an accessible progress bar. Controlled, so it works with any audio source.
- [Speech Input](https://ui.minidev.pro/docs/speech-input/md): React speech to text button on the Web Speech API: live interim text, a mic ring that follows your voice, Escape to stop, no API key and no dependencies.
- [Avatar](https://ui.minidev.pro/docs/avatar/md): Shadcn compatible avatar on Base UI with image and initials fallback, three sizes, a status badge and an overlapping avatar group with a plus count.
- [Carousel](https://ui.minidev.pro/docs/carousel/md): Scroll snap React carousel with previous and next buttons, arrow key support and a labelled region. Shows one, two or three slides by breakpoint, no library.
- [File List](https://ui.minidev.pro/docs/file-list/md): A stacked list of file rows showing each name and metadata such as size or date, with an optional download button per item. Good for React attachment lists.
- [File Row](https://ui.minidev.pro/docs/file-row/md): A single file row with an icon, a truncated name, an optional meta line and a ghost download button with an accessible label. Styled with Tailwind.

---

All 557 components: https://ui.minidev.pro/llms.txt (full text: https://ui.minidev.pro/llms-full.txt).
