Usage
The auto-imported composable useSpeechRecognition provides speech recognition functionality in the browser using the Web Speech API.
<script setup lang="ts">
const appLocale = useLocale()
const {
state,
isAvailable,
isListening,
start,
stop,
toggle,
setLanguage
} = useSpeechRecognition(
{
lang: appLocale.locale.value.locale,
continuous: true,
interimResults: true
},
{
onStart: () => console.log('Recognition started'),
onEnd: () => console.log('Recognition ended'),
onError: (error) => console.error('Error:', error),
onResult: (result) => console.log('Result:', result.text)
}
)
</script>
API
Parameters
useSpeechRecognition(options?: SpeechRecognitionOptions, events?: SpeechRecognitionEvents)
Creates a speech recognition instance with specified options and event handlers.
options:
'en-US'.true, recognition continues until explicitly stopped. Default: true.true.1.events:
useSpeechRecognition returns an object with the following properties:
state
state: DeepReadonly<Ref<SpeechRecognitionState>>
The current speech recognition state.
isAvailable
isAvailable: ComputedRef<boolean>
A computed property indicating speech recognition availability.
isListening
isListening: ComputedRef<boolean>
A computed property indicating whether recognition is active.
start()
start(): Promise<boolean>
Starts speech recognition.
Returns: Promise<boolean> - true if recognition started successfully, otherwise false.
stop()
stop(): Promise<boolean>
Stops speech recognition.
Returns: Promise<boolean> - true if recognition stopped successfully, otherwise false.
toggle()
toggle(): Promise<boolean>
Toggles the recognition state (start/stop).
Returns: Promise<boolean> - true if the operation was successful, otherwise false.
setLanguage()
setLanguage(lang: string): boolean
Sets the recognition language.
'ru-RU', 'en-US').Returns: boolean - true if the language was set successfully, otherwise false.
recognizer
recognizer: SpeechRecognition | undefined
Web Speech API Recognition instance for advanced use.
Example
Recognized text can be added to a Textarea or Input component.