admin管理员组

文章数量:1279090

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?

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?

Share Improve this question asked Oct 3, 2020 at 5:25 Anchey PengAnchey Peng 1131 gold badge1 silver badge6 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 11

try window.SpeechRecognition || window.webkitSpeechRecognition;

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

Try this:

Instead of

const recognition = new SpeechRecognition();

Write:

const recognition = new speechRecognition();

This helped me!

本文标签: javascriptWeb Speech API SpeechRecognition not defined when using ReactjsStack Overflow