Speech recognition in Vue js

Viewed 274

I'm working with speech recognition and Vue.js

The problem is, I can't load any data, functions to the speechRecognition's method (In code it's submitSpeech).

My first try was to call a function in submitSpeech like this: this.testFunction(). Without succeed.

Second try was to call a function like let function = this.testFunction() and then call it in recognition.onresult { function } . The function was working, but if I wouldn't call it in recognition.onresult { function } , the function would be working anyway, cause of let. I want to call testFunction() when I need it.

Third try was calling function this.testFuntion() through the button where, but still the say problem as in situation with let, so still not working properly:

<b-button id="btnGiveCommand"
    @click="submitSpeech( this.testFunction())"
  >Tap to speak</b-button>

Let's say in testFunction() look like this:

testFunction{
  this.answered = !this.answered
}

Here is the code of submitSpeech():

submitSpeech(function) { //let function = this.testFunction()

  let SpeechRecognition = SpeechRecognition || webkitSpeechRecognition; 
  let SpeechGrammarList = SpeechGrammarList || webkitSpeechGrammarList;

  let grammar = '#JSGF V1.0;' // Typ Gramatiky, ktorú používame

  let recognition = new SpeechRecognition();
  let speechRecognitionList = new SpeechGrammarList();
  speechRecognitionList.addFromString(grammar, 1);
  recognition.grammars = speechRecognitionList;
  recognition.lang = 'en-US';
  recognition.interimResults = false; 

  recognition.onresult = function(event) {
    let last = event.results.length - 1;
    let command = event.results[last][0].transcript;

    console.log(command)
    if(command.toLowerCase() === 'best'){ 
      //this.testFunction()
      function
    } else {
        console.log("command =/ best")
      document.querySelector('#test2').className = "123421412"
    }
  };
  recognition.onspeechend = function() {
    recognition.stop();
  };

  recognition.onerror = function(event) {
    console.log('Error occurred in recognition: ' + event.error)
  }
  recognition.start();

},

Does anybody have any idea how to call the function testFunction() in submitSpeech(){ recognition.onresult(){ Here should the function } }?

Thank's for any help.

0 Answers
Related