4

I am using React.js along with the Web Speech API's SpeechRecognition, however, it does not work and I get the error "ReferenceError: SpeechRecognition is not defined." The code I am using is directly from the SpeechRecognition documentation:

const SpeechRecognition = SpeechRecognition || webkitSpeechRecognition;
const recognition = new SpeechRecognition();

The first line causes the error, but without it, the second line will cause the same error. How can I fix this?

Anchey Peng
  • 71
  • 1
  • 3

2 Answers2

9

try window.SpeechRecognition || window.webkitSpeechRecognition;

See Using the Web Speech API for further explanation why you need the window. prefix.

piouson
  • 3,328
  • 3
  • 29
  • 29
-1

Try this:

Instead of

const recognition = new SpeechRecognition();

Write:

const recognition = new speechRecognition();

This helped me!

holydragon
  • 6,158
  • 6
  • 39
  • 62