1

我正在用 html 和 js 制作一个虚拟助手,它可以工作,但是每次你按下“询问”按钮时,它都会请求麦克风访问权限,即使在我之前授予它访问权限之后也是如此。有什么办法吗?我想可能是我直接从我的文件夹中运行它,所以如果我把它放在服务器或其他东西上会有帮助吗?这是我的代码:

<!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>

4

0 回答 0