A class to handle microphone permissions, start and observe speech input. It also adds a simple notification which lets the user disable
Call the start function and the notification will appear which tells the user the app is listening.
It will return an observable which calls the next handler on every user input with the corresponding SpeechRecognitionEvent
.
It will fail if the speech recognition fails or the user disables it with the corresponding events.
It will finish after calling .stop()
.
const speechControl = new SpeechControl();
speechControl
.start()
.subscribe(SpeechRecognitionEvent => {}, SpeechControlErrors | SpeechRecognitionError => {}, Event => {})
You can also just listen for specific keywords/phrases. In this case the subscribe handler just gets called when user said this word.
const speechControl = new SpeechControl();
speechControlContinue = speechControl.on('continue').subscribe(SpeechRecognitionEvent => {}, SpeechControlErrors | SpeechRecognitionError => {}, Event => {});
There are two ways to handle permissions. You can ask the user directly or wait for him to allow the microphone permission.
askForPermission(): Observable<any>
: triggers the microphone permission prompt and completes when the user allows, errors when deniedwhenPermissionGranted(): Observable<any>
: completes when the microphone permission is granted, errors when denied
setNotification({container?: HtmlElement, text?: string, disableText?:string}): void
: lets you customize the notificationisEnabled(): boolean
: checks if SpeechRecognition is supported or user disabled the speech recognitionstop(): void
: stops the recognition and removes the notification
const speechControl = new SpeechControl(options?);
recLanguage
: set the recording language, default is the HTML document language or users browser language