createSpeechRecognition
Category Sensors
Reactive controller interface for the recognition service.
Demo
Your browser does not support the Speech Recognition API.
More details on caniuse.Usage
IMPORTANT
You can only call it during component initialization.
import { createSpeechRecognition } from '@sv-use/core';
const speech = createSpeechRecognition({
lang: 'en',
interimResults: false,
onResult(transcript) {
console.log("Transcript received", transcript);
},
onError(error) {
console.log('Oops, something went wrong', error);
}
});
if (speech.isSupported) {
speech.start();
}
Type Definitions
import { type ConfigurableWindow } from '../__internal__/configurable.js';
import type { SpeechRecognition, SpeechRecognitionErrorEvent } from './types.js';
import type { MaybeGetter } from '../__internal__/types.js';
interface CreateSpeechRecognitionOptions extends ConfigurableWindow {
/**
* Whether continuous results are returned for each recognition, or only a single result.
* @default true
*/
continuous?: boolean;
/**
* Whether interim results should be returned or not.
*
* Interim results are results that are not yet final.
* @default true
*/
interimResults?: boolean;
/**
* The language used for the speech recognition.
*
* Note that if you pass in a getter and change the language, the service must be stopped first for the change to take effect.
* @default 'en-US'
*/
lang?: MaybeGetter<string>;
/**
* A number representing the maximum returned alternatives for each result.
* @default 1
*/
maxAlternatives?: number;
/**
* A callback for when an error occurs.
* @param error The error that occurred.
*/
onError?: (error: SpeechRecognitionErrorEvent) => void;
/**
* A callback for when a new result is obtained.
* @param transcript The transcript of the result.
* @param isFinal Whether the result is final or not.
*/
onResult?: (transcript: string, isFinal: boolean) => void;
}
type CreateSpeechRecognitionReturn = {
readonly isSupported: boolean;
readonly isListening: boolean;
readonly isFinal: boolean;
readonly recognition: SpeechRecognition | undefined;
result: string;
error: SpeechRecognitionErrorEvent | null;
start: () => void;
stop: () => void;
};
/**
* Reactive controller interface for the recognition service.
* @param options Additional options to customize the behavior.
* @see https://svelte-librarian.github.io/sv-use/docs/core/create-speech-recognition
*/
export declare function createSpeechRecognition(options?: CreateSpeechRecognitionOptions): CreateSpeechRecognitionReturn;
export {};