SpeechRecognizer
Speech recognition: the microphone to text, live — dictation into a field, voice commands. A
service plugin, a typed wrapper over the host-registered "speech" service. Host-optional:
guard with SpeechRecognizer.supported before showing a dictation button.
A plugin of the app: list it in app.json — "plugins": { "speech": "*" } (one list for every
platform; the LeCodes viewer registers every first-party plugin whatever the list says).
The recognizer is the platform's, and so are its languages, its quality and where the audio
goes: Apple's on iOS (on the device for the languages it can, else Apple's servers), Google's on
Android (the device's recognition service — a phone without Google's app has none, and start()
rejects "unavailable"), the browser's on the web (Chrome sends the audio to Google, Safari to
Apple; Firefox has none). For a transcript that is the same everywhere — a voice message to text —
record with AudioRecorder and recognize on your server.
At a glance
const speech = new SpeechRecognizer()
speech.addEventListener('result', r => { field.value = r.transcript }) // partials, then the final
speech.addEventListener('end', () => micButton.style({ opacity: 1 }))
speech.addEventListener('error', (code, message) => { if (code !== 'noSpeech') toast(message) })
micButton.onPress(async () => {
if (speech.listening) return speech.stop()
micButton.style({ opacity: 0.5 })
await speech.start({ language: 'ru-RU' }) // one utterance: ends after a pause
})API
new SpeechRecognizer() // nothing opens until start()
SpeechRecognizer.supported: boolean // the host registered the service
SpeechRecognizer.supportedLanguages(): Promise<string[]> // BCP-47, sorted; [] where the host cannot say (the web)
speech.start(options?: SpeechOptions): Promise<void>
speech.stop(): void // finish: the final result, then end
speech.abort(): void // drop what was heard: end, no result
speech.listening: boolean
speech.addEventListener('result', (r: SpeechResult) => void)
speech.addEventListener('speechStart' | 'speechEnd' | 'end', () => void)
speech.addEventListener('error', (code: SpeechErrorCode, message: string) => void)
interface SpeechOptions { language?: string, continuous?: boolean /* false */, interimResults?: boolean /* true */, onDevice?: boolean /* false */ }
interface SpeechResult { transcript: string, isFinal: boolean, confidence: number | null }
type SpeechErrorCode = "noSpeech" | "audio" | "network" | "denied" | "aborted" | "unavailable" | "failed"- The permissions — speech recognition (iOS has a dialog of its own) and the microphone — are
asked by the host on the first
start()(the contract's@permission speech microphone); a refusal rejects with"denied". To ask earlier, or to send the user to the settings after a refusal, usepermissions(permissions.md). - One utterance by default: the listening ends after a pause in the speech (the final result,
then
end).continuous: truekeeps listening, a final result per utterance, untilstop(). interimResultsdelivers partials while the user speaks (isFinalfalse), each the whole utterance so far — replace, do not append.confidencecomes with a final result where the recognizer says (Apple, the browser); null otherwise.onDevice: truekeeps the audio on the phone: iOS 13+ and Android 12+ where the device has a model for the language, else"unavailable". The web ignores it."noSpeech"is the error of a listening in which nothing was heard; it is not a failure of the app's and usually needs no message.- A listening is at most about a minute on iOS; a continuous one restarts itself.
See also
- audio-recorder.md — the recording to send to a server-side recognizer
- permissions.md —
permissions.request("speech"),openSettings()