javascript - 为什么我在 js 上的语音识别不起作用?

标签 javascript html

我一直在关注 this并用各种 youtube 视频和堆栈溢出问题对其进行补充。但是,我不确定为什么我的语音识别不起作用。我被问到很少允许麦克风访问。
我希望用户麦克风拾取用户的语音并将其附加到某个输入值。这是我的代码:

const recognition = new SpeechRecognition();
              recognition.interimResults = true;

              var recognition = new SpeechRecognition();
                recognition.onresult = function(event) {
                  if (event.results.length > 0) {
                    name.value = event.results[0][0].transcript;
                  }
                }

              recognition.addEventListener("end", () => {
                recognition.start();
              });

              var recognition = new SpeechRecognition();
                recognition.onresult = function(event) {
                  if (event.results.length > 0) {
                    location.value = event.results[0][0].transcript;
                  }
                }
              
              recognition.addEventListener("end", () => {
                recognition.start();
              });

              var recognition = new SpeechRecognition();
                  recognition.onresult = function(event) {
                    if (event.results.length > 0) {
                      state.value = event.results[0][0].transcript;
                      document.care.submit();
                    }
                  }
              
              recognition.addEventListener("end", () => {
                window.location.pathname = '/care';
                document.care.submit();
              });

以下是我收到的错误:
未捕获的 ReferenceError:未定义识别
在 HTMLButtonElement.onclick
未捕获的语法错误:标识符“识别”已被声明
无法加载资源:net::ERR_FAILED

最佳答案

我使用 SpeechRecogition 已经有一段时间了,一开始可能会非常棘手。但是,我发现这个要点对我很有帮助。希望这段代码可以帮助您更像我对 SpeechRecogition 的理解。
要点:https://gist.github.com/strongSoda/27f4caf1335e3d03accf708e1fcdcbf0
Youtube教程(对应代码):https://www.youtube.com/watch?v=4eIRrowvLRk

关于javascript - 为什么我在 js 上的语音识别不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68138455/

相关文章:

html - 为什么 border-radius 在使用 Internet Explorer 的特定网站中不起作用?

javascript - 现代网络爬虫是使用点击事件还是直接导航到 anchor 标签上的 href?

html - Bootstrap (3.3.7) 移动导航栏元素(链接)不工作

iphone - 通过 HTML 打开的全屏播放器即使按下完成也不会停止下载

javascript - 将值传递给 Angular 中的属性

javascript - Reactjs 未捕获类型错误 : Cannot read property 'map' of undefined

Javascript循环获取并显示数组中的每个第二个字符串

javascript - 在 keyup() 之后迭代时 jQuery 卡住

javascript - 表示由 HTML5 FileReader 读取的图像

javascript - 如何在 BackBone View 上正确渲染 $el 元素