SpeechToText js : merge stop event handlers

This commit is contained in:
Matt Flax 2025-05-11 20:06:56 +10:00
parent edb51373b2
commit 323dadfe29

View file

@ -94,22 +94,27 @@
recognition.start(); recognition.start();
}); });
recognition.onresult = function(event) { // Combined event handler function for speech recognition events
const transcript = event.results[0][0].transcript; function handleSpeechEvent(eventType, event) {
// Set LED back to gray for all events
led.style.backgroundColor = 'gray'; led.style.backgroundColor = 'gray';
if (eventType === 'result') {
const transcript = event.results[0][0].transcript;
// Try to populate the chat input directly // Try to populate the chat input directly
const success = populateChatInput(transcript); const success = populateChatInput(transcript);
if (!success) if (!success)
console.error('populateChatInput failed'); console.error('populateChatInput failed');
}; }
else if (eventType === 'error') {
recognition.onerror = function(event) {
console.error('Speech recognition error', event.error); console.error('Speech recognition error', event.error);
led.style.backgroundColor = 'gray'; }
}; // 'end' event requires no special handling beyond resetting the LED
}
recognition.onend = function() { // Set up event handlers using the combined function
led.style.backgroundColor = 'gray'; recognition.onresult = function(event) { handleSpeechEvent('result', event); };
}; recognition.onerror = function(event) { handleSpeechEvent('error', event); };
recognition.onend = function() { handleSpeechEvent('end'); };
})(); })();