I'm coding a voice reconition assitant to make it work in my web page but I got this error: Uncaught TypeError: SpeechRecognition is not a constructor at app.js:5(I'm using Google Chrome browser).
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>
You need to make sure you keep the correct capitaliazation - it's webkitSpeechRecognition
(uppercased R
).
const SpeechRecognition = window.speechRecognition || window.webkitSpeechRecognition;
console.log(new SpeechRecognition());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With