JamesBrill / react-speech-recognition

💬Speech recognition for your React app
https://webspeechrecognition.com/
MIT License
657 stars 119 forks source link

Starting to listen right after stopping does not work #66

Closed TejasOS closed 3 years ago

TejasOS commented 3 years ago

Hello,

I'm trying to use this with a TTS library and I'm trying to stop the mic from listening, then use my TTS library to say something, and then start listening again so that my speech recognition does not pick up any of the TTS speech. However, when I do this, like this: SpeechRecognition.stopListening(); console.log('Anything'); SpeechRecognition.startListening({continuous: true});

My SpeechRecognition doesn't start listening again. Any idea why this is happening? Thanks so much!

JamesBrill commented 3 years ago

Hi @Untitleddotpy thanks for raising this issue!

This is a valid problem and thanks for calling it out. The internal mechanism for turning off the microphone is asynchronous but stopListening is synchronous.

I have addressed this in version 3.3.0. You will need to update your code to be asynchronous and await the stopListening function. Something like:

const speak = async (text) => {
  await SpeechRecognition.stopListening();
  callTextToSpeechLibrary(text); 
  SpeechRecognition.startListening({ continuous: true });
}

Now the TTS library (and startListening) will only be called once the microphone has been turned off.

Let me know how you get on with v3.3.0!

TejasOS commented 3 years ago

Hi James,

Thanks so much! It seems to work now!