JamesBrill / react-speech-recognition

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

Race condition between stopListening() and command handling on Chrome Mobile #187

Open korneliuszw opened 10 months ago

korneliuszw commented 10 months ago

Hello, I am using this library to create voice navigation for my app (v3.10.0 from npm). So far it is great. However, I encountered a race condition when using stopListening() with continuous mode on Chrome Android. I have a command called "stop" which stops voice recognition using SpeechRecognition.stopListening() (abortListening doesn't help). During this stopListening() call, an onend is called from SpeechRecognition API (because of chrome limitations) and resets listening. In result, despite calling stopListening(), the recognition is still working.

Reproduction (say stop right after start): https://codesandbox.io/s/upbeat-lucy-sxjlx6?file=/src/App.js

korneliuszw commented 10 months ago

Workaround for this issue

const recognition = SpeechRecognition.getRecognition()
if (recognition) recognition.continuous = false
SpeechRecognition.stopListening()