gpt4 book ai didi

javascript - 未捕获的类型错误 : SpeechRecognition is not a constructor at app. js:5

转载 作者:行者123 更新时间:2023-12-03 07:01:19 24 4
gpt4 key购买 nike

我正在编写语音识别助手以使其在我的网页中工作,但出现此错误:Uncaught TypeError: SpeechRecognition is not a constructor at app.js:5(我使用的是 Google Chrome 浏览器)。

const btn = document.querySelector('.talk');
const content = document.querySelector('.content');

const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechrecognition;
const recognition = new SpeechRecognition();

recognition.onstart = function() {
console.log('Voice Is Activated, You Can Speak');
};

recognition.onresult = function(event) {
console.log(event);
};

btn.addEventListener('click', () => {
recognition.start();
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>MY ASSISTANT</title>
</head>
<body>
<h1>Hello World</h1>
<img src="#" alt=""></img>
<button class="talk">Talk</button>
<h1 class="content"></h1>

<script src="app.js"></script>
</body>
</html>

最佳答案

您需要确保保持正确的大写字母 - 它是 webkitSpeechRecognition (大写 R)。

const SpeechRecognition = window.speechRecognition || window.webkitSpeechRecognition;
console.log(new SpeechRecognition());

关于javascript - 未捕获的类型错误 : SpeechRecognition is not a constructor at app. js:5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56570581/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com