Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught TypeError: SpeechRecognition is not a constructor at app.js:5

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>
like image 915
Mr.Pierrot Avatar asked Nov 16 '22 07:11

Mr.Pierrot


1 Answers

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());
like image 147
Jack Bashford Avatar answered Dec 09 '22 22:12

Jack Bashford