2015-04-02 33 views
1

使用MSDN的SAPI,您如何取消同步语音识别操作,或者至少立即停止它?Canсel同步语音识别

将输入设置为null会返回一个错误,指出识别器识别时我无法做到这一点,并且使用异步识别不是一种选择。

这里是低于

class MainClass { 

    static void Main() { 
      SpeechRecognitionEngine recognizer = new SpeechRecognitionEngine(); 
      recognizer.LoadGrammar(new DictationGrammar()); 
      recognizer.SetInputToDefaultAudioDevice(); 
      recognizer.Recognize(); 
    } 

    void MethodCalledFromOtherThread() { 
      //Since SpeechRecognitionEngine.Recognize() stops the current thread, 
      //this method is called from a different thread. 
      //I NEED the current thread to stop. 

      //Here I want to Cancel recognizer.Recognize  
     } 
} 
+1

您_might_能够得到原螺纹的保持和它抛出一个异常,而[这个问题](http://stackoverflow.com/questions/44656/is-there-a-good-用于抛出一个特定线程的异常的方法)涵盖了为什么你不应该那样做。你能解释为什么异步选项不适合你,因为它有一个特定的取消方法吗? – 2015-04-02 14:43:19

+0

@JamesThorpe我的应用程序工作和设置的方式,异步操作将在性能方面代价高昂 – JackBarn 2015-04-02 14:51:56

+0

由于它的本性,您无法取消同步呼叫。是否有可以使用的Beginxxx,Endxxx版本的功能? – 2015-04-02 15:11:55

回答

3

一个例子这MSDN article演示如何使用异步SAPI不带螺纹以及与此您可以随时取消操作。

以下是如何尽早终止识别的一个非常简单的例子。

class Program 
{ 
    private static bool _userRequestedAbort = false; 

    // Indicate whether asynchronous recognition is complete. 

    static void Main(string[] args) 
    { 
     // Create an in-process speech recognizer. 
     using (SpeechRecognitionEngine recognizer = new SpeechRecognitionEngine(new CultureInfo("en-US"))) 
     { 
      // Create a grammar for choosing cities for a flight. 
      Choices cities = new Choices(new string[] { "Los Angeles", "New York", "Chicago", "San Francisco", "Miami", "Dallas" }); 

      GrammarBuilder gb = new GrammarBuilder(); 
      gb.Append("I want to fly from"); 
      gb.Append(cities); 
      gb.Append("to"); 
      gb.Append(cities); 

      // Construct a Grammar object and load it to the recognizer. 
      Grammar cityChooser = new Grammar(gb); 
      cityChooser.Name = ("City Chooser"); 
      recognizer.LoadGrammarAsync(cityChooser); 

      bool completed = false; 

      // Attach event handlers. 
      recognizer.RecognizeCompleted += (o, e) => 
      { 
       if (e.Error != null) 
       { 
        Console.WriteLine("Error occurred during recognition: {0}", e.Error); 
       } 
       else if (e.InitialSilenceTimeout) 
       { 
        Console.WriteLine("Detected silence"); 
       } 
       else if (e.BabbleTimeout) 
       { 
        Console.WriteLine("Detected babbling"); 
       } 
       else if (e.InputStreamEnded) 
       { 
        Console.WriteLine("Input stream ended early"); 
       } 
       else if (e.Result != null) 
       { 
        Console.WriteLine("Grammar = {0}; Text = {1}; Confidence = {2}", e.Result.Grammar.Name, e.Result.Text, e.Result.Confidence); 
       } 
       else 
       { 
        Console.WriteLine("No result"); 
       } 

       completed = true; 
      }; 

      // Assign input to the recognizer and start an asynchronous 
      // recognition operation. 
      recognizer.SetInputToDefaultAudioDevice(); 

      Console.WriteLine("Starting asynchronous recognition..."); 
      recognizer.RecognizeAsync(); 

      // Wait for the operation to complete. 
      while (!completed) 
      { 
       if (_userRequestedAbort) 
       { 
        recognizer.RecognizeAsyncCancel(); 
        break; 
       } 

       Thread.Sleep(333); 
      } 

      Console.WriteLine("Done."); 
     } 

     Console.WriteLine(); 
     Console.WriteLine("Press any key to exit..."); 
     Console.ReadKey(); 
    } 
} 
+1

仅支持链接的答案在stackoverflow上不足。 – Sildoreth 2015-04-02 20:28:53

+1

好的,我会马上增加一些价值,而不仅仅是剪切和粘贴 – 2015-04-02 20:34:21

+0

'Thread.Sleep'不会停止语音识别器 – JackBarn 2015-04-02 23:05:39