Check out my code snippet:
export class voiceRecognition {
constructor() { }
public startVoiceRecognition() {
const recognition = new webkitSpeechRecognition();
recognition.continuous = false;
recognition.interimresults = false;
recognition.lang = "en-US";
recognition.start();
recognition.onresult = function (e) {
const response = e.results[0][0].transcript;
this.customResponse(response);
recognition.stop();
}
recognition.onerror = function (e) {
recognition.stop();
}
}
public customResponse(response) {
//Code to handle customer response goes here//
}
}
The issue I'm facing is I'm having trouble calling the customResponse(response)
function from recognition.onresult
.
Update: I switched from
recognition.onresult=function(event){}
to recognition.addEventListener("result",(event)=>{})
and this successfully called the function inside it. However, I'm encountering a problem where sometimes it works and sometimes it doesn't when I run it again. It's not even executing recognition.addEventListener("error",(event)=>{})
. Any idea why the same code behaves differently at times?