Speech Recognition Engine does not shut down - inv

2019-07-17 05:23发布

问题:

Decided to wire up a simple speech writing application as a test bed for learning Speech Recognition + F#. To allow the speech recognition to be started or stopped I wired up the following methods:

 let Activate () = 
     sp.RecognizeAsync(RecognizeMode.Multiple)

 let Deactivate () = 
     sp.RecognizeAsyncCancel()
     sp.RecognizeAsyncStop()

I can start the engine fine with the default grammar dictionary. The problem comes whens I call the deactivate method. I often get the following exception:

 An unhandled exception of type 'System.InvalidOperationException' occurred in System.Speech.dll

 Additional information: Cannot perform this operation while the recognizer is doing recognition.

回答1:

See here.

There appear to be two issues with your code:

  1. You should either call RecognizeAsyncCancel or RecognizeAsyncStop, but not both. They do the same thing except that RecognizeAysncCancel truncates the input while RecognizeAsyncStop doesn't. I'm guessing that calling both of them in a row is causing the error you're seeing.

  2. Both RecognizeAsyncCancel and RecognizeAsyncStop have callbacks for when they are complete. You shouldn't do anything else with the engine until the final operation is complete. See the link for an example of how to do this.