LifecycleCallbacks
Event Handling
Listen to lifecycle events to build reactive interfaces.
Event List
Hook into the JSVoice lifecycle by passing callbacks to the constructor.
| Callback | Trigger |
|---|---|
| onSpeechStart | Microphone is active and listening |
| onSpeechEnd | Microphone has stopped |
| onCommandRecognized | Phrase matched a known command |
| onWakeWordDetected | Wake word valid, awaiting command |
| onError | Error occurred (permissions, no speech) |
Example Implementation
javascript
1const voice = new JSVoice({
2 onSpeechStart: () => {
3 document.getElementById('indicator').classList.add('recording');
4 },
5
6 onCommandRecognized: (phrase, raw) => {
7 console.log("Matched:", phrase);
8 showToast(`Executed: ${phrase}`);
9 },
10
11 onError: (err) => {
12 console.error("Voice Error", err);
13 }
14});