2015-04-02 64 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