0

I am working on a project and need to input data in a form which contain input task name type=text with id=task_name. The user should be able to input data using annyang voice recognition, annyang is starting and can see red dot on page but when passing the commands nothing shows up, did i make an error somewhere. Clicking on a button will make annyang start and after that user could add use voice command to input data but voice command is not working.

Here is my code

<script>
        
    start_vc.addEventListener('click', () => {

    const speech = new SpeechSynthesisUtterance();

    speech.text = 'How can i help you!';
    speech.lang = 'en-UK';
    speech.volume = 1;
    speech.rate = 1;
    speech.pitch = 1;
    
    window.speechSynthesis.speak(speech);

    if (annyang) {
        
        var commands = {

            'input task name': function(variable) {
                let task_name = document.getElementById("task_name");
                task_name.value = variable;
            }
        };
      
        // Add our commands to annyang
        annyang.addCommands(commands);
      
        // Start listening.
        annyang.start();
      }

});
    </script>

Did i make an error somewhere and if i made an error can someone tell me what was it i kind of new to javascript

Lyfe A1
  • 31
  • 7

1 Answers1

0

Your code will make annyang listen for the voice command "input task name" because you define that command here:

enter image description here

Try saying "input task name" and see if it places that sentence into the task_name field.

You will want to replace that text with a command that will catch any sentence the user says.

For example:

var commands = {
    '*variable': function(variable) {
        let task_name = document.getElementById("task_name");
        task_name.value = variable;
    }
};

annyang.addCommands(commands);

I would also recommend adding annyang.debug() to your code while developing.

Tal Ater
  • 1,121
  • 1
  • 10
  • 17
  • Unfortunately saying 'input task name' does not add anything in the task_name field – Lyfe A1 Apr 07 '21 at 19:56
  • in my console i get a error of Failed to execute 'start' on 'SpeechRecognition': recognition has already started and Speech Recognition is repeatedly stopping and starting – Lyfe A1 Apr 07 '21 at 20:11
  • Sounds like annyang was successfully instantiated on the page, but there might be another unrelated issue. Look at this [answer](https://github.com/TalAter/annyang/blob/master/docs/FAQ.md#why-does-speech-recognition-repeatedly-starts-and-stops) in the FAQ. Once you successfully get it to respond to `input task name` you can also change the command as I describe in my answer above. – Tal Ater Apr 08 '21 at 20:19