问题
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:
You should either call
RecognizeAsyncCancel
orRecognizeAsyncStop
, but not both. They do the same thing except thatRecognizeAysncCancel
truncates the input whileRecognizeAsyncStop
doesn't. I'm guessing that calling both of them in a row is causing the error you're seeing.Both
RecognizeAsyncCancel
andRecognizeAsyncStop
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.
来源:https://stackoverflow.com/questions/17937305/speech-recognition-engine-does-not-shut-down-invalid-operation-exception