JS

VOICE COMMAND USING JAVASCRIPT

COMMANDS LIST

01

var cmd = {   "say hello" : () =>     { alert("Hello World"); },     "say foo" : () =>     { alert("Foobar!"); } };

MICROPHONE PERMISSION navigator.mediaDevices .getUserMedia({ audio: true })  .then(() => {   SPEECH RECOGNITION OBJECT   const SR =      window.SpeechRecognition ||     window.webkitSpeechRecognition;    var recog = new SR();

JAVASCRIPT VOICE COMMAND

02

  recog.onresult = (evt) => {     SPEECH TO TEXT     let said = evt.results[0][0].transcript                      .toLowerCase();     CALL RESPECTIVE FUNCTION     if (cmd[said]) { cmd[said](); }     recog.stop();   };   recog.start(); });