Voice Input
A microphone control with a live level meter and timer, plus a hook that records with getUserMedia and MediaRecorder.
Overview
Voice Input is a microphone control. It moves through idle, waiting for microphone access, listening with a level meter and timer, transcribing, and done or failed. Stop sends the recording on; cancel throws it away.
The component shows the state you pass. Recording and transcription are your app's work. For the first, useVoiceRecorder records from the microphone and hands you the audio as a Blob. Send it to your transcription service and put the text in your composer.
The previews on this page don't use your microphone unless you press "Try with your microphone".
Anatomy
- MicrophoneStarts recording, and shows a spinner while waiting or transcribing
- Level meterBars that scroll with the loudness while listening
- TimerTime recorded, as m:ss
- Cancel and StopDiscard the recording, or finish it
- StatusA line in words for each state
Installing voice-input brings Button along as a registry dependency.
Behavior
States
Pass message to replace the status line, such as the reason access was refused.
Level
Pass level, from 0 to 1, from wherever you measure it. Or pass a stream and the component measures it with Web Audio. The meter samples the level at a steady pace, so it scrolls at the same speed however often the level changes.
When you leave elapsed out, the timer counts from the moment recording starts.
Sizes
default stands on its own in a panel. compact fits a composer's toolbar: a small microphone that grows into a pill while listening.
useVoiceRecorder
The hook does nothing until you call start, so the browser asks for the microphone only when the person presses it. stop finishes the recording and calls onRecorded with the Blob; cancel discards it. The microphone is released when recording ends or the component unmounts.
Accessibility
Each button is named: "Start voice input", "Stop recording", and "Cancel recording". The status is a polite live region, so each change of state is announced. The timer has the timer role. The meter is decorative. Focus moves to the control that replaces the one pressed, and Escape cancels while focus is inside. With reduced motion, the spinner and the halo do not animate.
Installation
Usage
import { useVoiceRecorder, VoiceInput } from "@/components/ai/voice-input"
const transcribe = useTranscription()
const recorder = useVoiceRecorder({
onRecorded: (blob) => transcribe.mutate(blob, { onSuccess: (text) => appendToComposer(text) }),
})
<VoiceInput
size="compact"
state={transcribe.isPending ? "transcribing" : recorder.state}
level={recorder.level}
elapsed={recorder.elapsed}
message={recorder.error?.message}
onStart={recorder.start}
onStop={recorder.stop}
onCancel={recorder.cancel}
/>Examples
In a composer
The compact size beside a send button. It is simulated until you choose "Try with your microphone"; then it records for real and plays the recording back, since transcription is your app's step.
API reference
VoiceInput
Accepts native div props beyond those listed.
useVoiceRecorder
It returns state ("idle", "requesting", "recording", or "error"), level, elapsed, blob, error, stream, and start, stop, and cancel.
useStreamLevel
useStreamLevel(stream) returns the stream's loudness from 0 to 1, read with Web Audio, or 0 without a stream.