JamesBrill / react-speech-recognition

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

Allow code to run inside "recognition.onresult" #81

Closed 0x213F closed 3 years ago

0x213F commented 3 years ago

Summary

I want to keep track of the raw interim transcript data accessible from recognition.onresult. As such, I wrote the following code:

const recognition = SpeechRecognition.getRecognition();
recognition.onresult = (e) => {
    // run custom code here...
}

interimTranscript does not fit my use case because I am looking to go beyond just a string; I want all the event data has to offer.

Observed behavior

When I try reading transcript it is empty.

Expected behavior

I should be able to run code onresult and still be able to access the transcript through react-speech-recognition.

Hypothesis

Overriding recognition.onresult screws up the internals of react-speech-recognition, resulting in the transcript being empty.

Next steps

Can someone validate or invalidate my hypothesis? Have I included sufficient information in this report, or should I give more details?

If there is any work that needs to be done here (and I may be of assistance), I am available to make a PR.

JamesBrill commented 3 years ago

Hi @0x213F Your report does indeed contain sufficient information, thank you. And your hypothesis is correct.

May I ask what event data you need to access besides the transcript?

JamesBrill commented 3 years ago

In the meantime, you could try this workaround:

  const recognition = SpeechRecognition.getRecognition()
  const onResult = recognition.onresult
  recognition.onresult = (e) => {
    // run custom code here...
    onResult(e)
  }