useSpeech

Provides unopinionated, high-level access to the Web Speech API for speech-to-text recognition.

Warning

Browser support is strictly limited to Google Chrome. Brave and Edge are explicitly filtered out due to inconsistent Speech API implementations.

Important

The start() method must be triggered by a user gesture (e.g., a click event) to comply with browser security policies regarding microphone access.

Note

The hook includes an auto-restart mechanism for the no-speech error to keep the session alive unless stop() is called manually.

Installation

bash

Description

The useSpeech wraps the SpeechRecognition interface, allowing developers to convert audio from a microphone into real-time text strings. It handles cross-browser prefixes (e.g., webkitSpeechRecognition), manages internal state transitions (listening, stopped, error), and cleans up listeners automatically.

To avoid non-deterministic behavior, crashes, or infinite restart loops, this hook performs a runtime browser check and will not activate outside of Chrome.

Parameters

NameTypeDescription
options.continuousbooleanIf true, the recognition continues even if the user pauses. Default: false.
options.interimResultsbooleanIf true, partial results are returned while the user is still speaking. Default: false.
options.langstringThe BCP 47 language tag for the recognition (e.g., 'en-US'). Default: 'en-US'.

Return values

NameTypeDescription
errorError | nullContains the error object if recognition fails.
start() => voidFunction to initialize and start the speech recognition session.
statusSpeechStatusCurrent status: 'idle', 'listening', 'stopped', 'unsupported', or 'error'.
stop() => voidStops the recognition process.
transcriptstringThe accumulated text recognized from the speech input.

Demo

Speech AI

Status: idle

Source code

tsx