Skip to documentation content

Voice Input

A microphone control with a live level meter and timer, plus a hook that records with getUserMedia and MediaRecorder.

voice-input-demo

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

VoiceInput
  • 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

StateShows
idleThe microphone button
requestingA spinner while the browser asks for access
recordingCancel, the level meter, the timer, and Stop
transcribingA spinner until your service replies
doneA check and "Added to your message"
errorThe reason, and the microphone button to try again

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.

KeyAction
Enter or SpaceStart, stop, or cancel
EscapeCancel while recording

Installation

npx honestui@latest add voice-input

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.

voice-input-composer

API reference

VoiceInput

Accepts native div props beyond those listed.

PropTypeDefaultDescription
stateVoiceInputStateRequiredThe state to show.
levelnumberNoneLoudness from 0 to 1 while recording.
streamMediaStream | nullNoneA stream to measure when you don't pass level.
elapsednumberCountedMilliseconds recorded.
messagestringThe state's wordsReplaces the status line.
size"default" | "compact""default"A standalone panel, or a toolbar control.
onStart() => voidNoneCalled by the microphone button.
onStop() => voidNoneCalled by Stop.
onCancel() => voidNoneCalled by Cancel and Escape.
disabledbooleanfalseDisables the microphone button.
labelsPartial<VoiceInputLabels>EnglishButton names and status words.

useVoiceRecorder

OptionTypeDescription
onRecorded(blob: Blob) => voidCalled with the recording when it stops.
mimeTypestringA preferred container, such as "audio/webm", when the browser supports it.
constraintsMediaTrackConstraintsPassed to getUserMedia.

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.