我正在编码语音重组助手以使其在我的网页上工作,但出现此错误:Uncaught TypeError:SpeechRecognition不是app.js:5的构造函数(我正在使用Google Chrome浏览器)。< / p>
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>
答案 0 :(得分:1)
您需要确保保留正确的大写字母-webkitSpeechRecognition
(大写的R
)。
const SpeechRecognition = window.speechRecognition || window.webkitSpeechRecognition;
console.log(new SpeechRecognition());
答案 1 :(得分:0)
找到了一个解决方案,让变量为webkitSpeechRecognition,不带||。操作员。因为Google实际上使用带有前缀WebKit
的此构造函数。
答案 2 :(得分:0)
SpeechRecognition
不存在。
尝试使用webkitSpeechRecognition
。它对我有用。
这是针对Chrome浏览器客户端的
答案 3 :(得分:-2)
如果您在Firefox中运行,则
不支持SpeechRecognition