webkitSpeechRecognition always requesting microphone access every time I run the function that calls it

377 Views Asked by At

I'm making a virtual assistant with html and js, and it works, but every time you press the "Ask something" button, it will request microphone access, even after I gave it access before. Is there any way around that? I think it might be that I'm running it directly from my folder, so would it help if I got it on a server or something? Here is my code:

<!DOCTYPE html>
<html>

<body>
  <button onclick="sListening()">Ask something</button>
  <br>
  <br>
  <div id="a">
    <small>z</small>zZ...
  </div>
  <script>
    var SpeechRecognition = SpeechRecognition || webkitSpeechRecognition;
    var recognition = new SpeechRecognition();


    // This runs when the speech recognition service starts
    recognition.onstart = function() {
      document.getElementById("a").innerHTML = "<b>!</b>"
    };

    recognition.onspeechend = function() {
      // when user is done speaking
      recognition.stop();
    }

    // This runs when the speech recognition service returns result
    recognition.onresult = function(event) {
      var transcript = event.results[0][0].transcript;
      process(transcript, "a");

      var confidence = event.results[0][0].confidence;
    };


    function ranarr(arr) {
      return arr[Math.floor(Math.random() * arr.length)];
    }

    function process(text, div) {
      document.getElementById(div).innerText = text;
      text = text.toLowerCase();
      var r = "I don't understand.";
      var hiarr = ['Sup?', 'Howdy.', 'Hello!', 'Well hello to you to!']
      text = text.split(/([!,?,.,\s])/);
      if (text.includes("hi") || text.includes("hello")) {
        r = ranarr(hiarr)
      }
      let utterance = new SpeechSynthesisUtterance(r);
      speechSynthesis.speak(utterance);

    }
    // start recognition
    function sListening() {
      recognition.start();
    }
  </script>
</body>

</html>

0

There are 0 best solutions below