SpeechRecognitionEngine.RecognizeAsync メソッド

定義

非同期音声認識操作を開始します。

オーバーロード

RecognizeAsync()

単一の非同期音声認識操作を実行します。

RecognizeAsync(RecognizeMode)

1 つ以上の非同期音声認識操作を実行します。

注釈

これらのメソッドは、1 つまたは複数の非同期認識操作を実行します。 認識エンジンは、読み込まれた有効な音声認識文法に対して各操作を実行します。

このメソッドの呼び出し中に、認識エンジンは次のイベントを発生させることができます。

非同期認識操作の結果を取得するには、イベント ハンドラーを認識エンジンの SpeechRecognized イベントにアタッチします。 認識エンジンは、同期または非同期の認識操作が正常に完了するたびに、このイベントを発生させます。 認識が成功しなかった場合、イベントのResultRecognizeCompletedEventArgsハンドラーRecognizeCompletedでアクセスできる オブジェクトの プロパティは になりますnull

非同期認識操作は、次の理由で失敗する可能性があります。

  • または InitialSilenceTimeout プロパティのタイムアウト間隔の有効期限が切れる前に、音声がBabbleTimeout検出されません。

  • 認識エンジンは音声を検出しますが、読み込まれて有効になっている Grammar オブジェクトのいずれにも一致が見つかりません。

  • SpeechRecognitionEngine 、認識を実行する前に、少なくとも 1 つの Grammar オブジェクトを読み込む必要があります。 音声認識文法を読み込むには、 メソッドまたは LoadGrammarAsync メソッドを使用しますLoadGrammar

  • 認識エンジンが認識に関して音声または無音のタイミングを処理する方法を変更するには、、EndSilenceTimeoutInitialSilenceTimeout、、および プロパティをBabbleTimeoutEndSilenceTimeoutAmbiguous使用します。

  • 同期認識を実行するには、いずれかの方法を Recognize 使用します。

RecognizeAsync()

Source:
SpeechRecognitionEngine.cs
Source:
SpeechRecognitionEngine.cs

単一の非同期音声認識操作を実行します。

public:
 void RecognizeAsync();
public void RecognizeAsync ();
member this.RecognizeAsync : unit -> unit
Public Sub RecognizeAsync ()

次の例は、基本的な非同期音声認識を示すコンソール アプリケーションの一部を示しています。 この例では、 を DictationGrammar作成し、それをインプロセス音声認識エンジンに読み込み、1 つの非同期認識操作を実行します。 操作中に認識エンジンによって発生するイベントを示すために、イベント ハンドラーが含まれています。

using System;  
using System.Globalization;  
using System.Speech.Recognition;  
using System.Threading;  

namespace AsynchronousRecognition  
{  
  class Program  
  {  
    // Indicate whether asynchronous recognition is complete.  
    static bool completed;  

    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);  

        // Attach event handlers.  
        recognizer.SpeechDetected +=  
          new EventHandler<SpeechDetectedEventArgs>(  
            SpeechDetectedHandler);  
        recognizer.SpeechHypothesized +=  
          new EventHandler<SpeechHypothesizedEventArgs>(  
            SpeechHypothesizedHandler);  
        recognizer.SpeechRecognitionRejected +=  
          new EventHandler<SpeechRecognitionRejectedEventArgs>(  
            SpeechRecognitionRejectedHandler);  
        recognizer.SpeechRecognized +=  
          new EventHandler<SpeechRecognizedEventArgs>(  
            SpeechRecognizedHandler);  
        recognizer.RecognizeCompleted +=  
          new EventHandler<RecognizeCompletedEventArgs>(  
            RecognizeCompletedHandler);  

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

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

        // Wait for the operation to complete.  
        while (!completed)  
        {  
          Thread.Sleep(333);  
        }  
        Console.WriteLine("Done.");  
      }  

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

    // Handle the SpeechDetected event.  
    static void SpeechDetectedHandler(object sender, SpeechDetectedEventArgs e)  
    {  
      Console.WriteLine(" In SpeechDetectedHandler:");  
      Console.WriteLine(" - AudioPosition = {0}", e.AudioPosition);  
    }  

    // Handle the SpeechHypothesized event.  
    static void SpeechHypothesizedHandler(  
      object sender, SpeechHypothesizedEventArgs e)  
    {  
      Console.WriteLine(" In SpeechHypothesizedHandler:");  

      string grammarName = "<not available>";  
      string resultText = "<not available>";  
      if (e.Result != null)  
      {  
        if (e.Result.Grammar != null)  
        {  
          grammarName = e.Result.Grammar.Name;  
        }  
        resultText = e.Result.Text;  
      }  

      Console.WriteLine(" - Grammar Name = {0}; Result Text = {1}",  
        grammarName, resultText);  
    }  

    // Handle the SpeechRecognitionRejected event.  
    static void SpeechRecognitionRejectedHandler(  
      object sender, SpeechRecognitionRejectedEventArgs e)  
    {  
      Console.WriteLine(" In SpeechRecognitionRejectedHandler:");  

      string grammarName = "<not available>";  
      string resultText = "<not available>";  
      if (e.Result != null)  
      {  
        if (e.Result.Grammar != null)  
        {  
          grammarName = e.Result.Grammar.Name;  
        }  
        resultText = e.Result.Text;  
      }  

      Console.WriteLine(" - Grammar Name = {0}; Result Text = {1}",  
        grammarName, resultText);  
    }  

    // Handle the SpeechRecognized event.  
    static void SpeechRecognizedHandler(  
      object sender, SpeechRecognizedEventArgs e)  
    {  
      Console.WriteLine(" In SpeechRecognizedHandler.");  

      string grammarName = "<not available>";  
      string resultText = "<not available>";  
      if (e.Result != null)  
      {  
        if (e.Result.Grammar != null)  
        {  
          grammarName = e.Result.Grammar.Name;  
        }  
        resultText = e.Result.Text;  
      }  

      Console.WriteLine(" - Grammar Name = {0}; Result Text = {1}",  
        grammarName, resultText);  
    }  

    // Handle the RecognizeCompleted event.  
    static void RecognizeCompletedHandler(  
      object sender, RecognizeCompletedEventArgs e)  
    {  
      Console.WriteLine(" In RecognizeCompletedHandler.");  

      if (e.Error != null)  
      {  
        Console.WriteLine(  
          " - Error occurred during recognition: {0}", e.Error);  
        return;  
      }  
      if (e.InitialSilenceTimeout || e.BabbleTimeout)  
      {  
        Console.WriteLine(  
          " - BabbleTimeout = {0}; InitialSilenceTimeout = {1}",  
          e.BabbleTimeout, e.InitialSilenceTimeout);  
        return;  
      }  
      if (e.InputStreamEnded)  
      {  
        Console.WriteLine(  
          " - AudioPosition = {0}; InputStreamEnded = {1}",  
          e.AudioPosition, e.InputStreamEnded);  
      }  
      if (e.Result != null)  
      {  
        Console.WriteLine(  
          " - Grammar = {0}; Text = {1}; Confidence = {2}",  
          e.Result.Grammar.Name, e.Result.Text, e.Result.Confidence);  
        Console.WriteLine(" - AudioPosition = {0}", e.AudioPosition);  
      }  
      else  
      {  
        Console.WriteLine(" - No result.");  
      }  

      completed = true;  
    }  
  }  
}

注釈

このメソッドは、単一の非同期認識操作を実行します。 認識エンジンは、読み込まれた有効な音声認識文法に対して操作を実行します。

このメソッドの呼び出し中に、認識エンジンは次のイベントを発生させることができます。

非同期認識操作の結果を取得するには、イベント ハンドラーを認識エンジンの SpeechRecognized イベントにアタッチします。 認識エンジンは、同期または非同期の認識操作が正常に完了するたびに、このイベントを発生させます。 認識が成功しなかった場合、イベントのResultRecognizeCompletedEventArgsハンドラーRecognizeCompletedでアクセスできる オブジェクトの プロパティは になりますnull

同期認識を実行するには、いずれかの方法を Recognize 使用します。

このメソッドは、メソッドの同期的な例外がスローできる非使用例外をすべて返すタスクに格納します。 返されたタスクに例外が格納されている場合、その例外はタスクの待機時にスローされます。 などの ArgumentException使用例外は、引き続き同期的にスローされます。 格納されている例外については、 によって Recognize()スローされる例外に関するページを参照してください。

こちらもご覧ください

適用対象

RecognizeAsync(RecognizeMode)

Source:
SpeechRecognitionEngine.cs
Source:
SpeechRecognitionEngine.cs

1 つ以上の非同期音声認識操作を実行します。

public:
 void RecognizeAsync(System::Speech::Recognition::RecognizeMode mode);
public void RecognizeAsync (System.Speech.Recognition.RecognizeMode mode);
member this.RecognizeAsync : System.Speech.Recognition.RecognizeMode -> unit
Public Sub RecognizeAsync (mode As RecognizeMode)

パラメーター

mode
RecognizeMode

1 つまたは複数の認識操作を実行するかどうかを示します。

次の例は、基本的な非同期音声認識を示すコンソール アプリケーションの一部を示しています。 この例では、 を DictationGrammar作成し、インプロセス音声認識エンジンに読み込み、複数の非同期認識操作を実行します。 非同期操作は 30 秒後に取り消されます。 操作中に認識エンジンによって発生するイベントを示すために、イベント ハンドラーが含まれています。

using System;  
using System.Globalization;  
using System.Speech.Recognition;  
using System.Threading;  

namespace AsynchronousRecognition  
{  
  class Program  
  {  
    // Indicate whether asynchronous recognition is complete.  
    static bool completed;  

    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);  

        // Attach event handlers.  
        recognizer.SpeechDetected +=  
          new EventHandler<SpeechDetectedEventArgs>(  
            SpeechDetectedHandler);  
        recognizer.SpeechHypothesized +=  
          new EventHandler<SpeechHypothesizedEventArgs>(  
            SpeechHypothesizedHandler);  
        recognizer.SpeechRecognitionRejected +=  
          new EventHandler<SpeechRecognitionRejectedEventArgs>(  
            SpeechRecognitionRejectedHandler);  
        recognizer.SpeechRecognized +=  
          new EventHandler<SpeechRecognizedEventArgs>(  
            SpeechRecognizedHandler);  
        recognizer.RecognizeCompleted +=  
          new EventHandler<RecognizeCompletedEventArgs>(  
            RecognizeCompletedHandler);  

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

        completed = false;  
        Console.WriteLine("Starting asynchronous recognition...");  
        recognizer.RecognizeAsync(RecognizeMode.Multiple);  

        // Wait 30 seconds, and then cancel asynchronous recognition.  
        Thread.Sleep(TimeSpan.FromSeconds(30));  
        recognizer.RecognizeAsyncCancel();  

        // Wait for the operation to complete.  
        while (!completed)  
        {  
          Thread.Sleep(333);  
        }  
        Console.WriteLine("Done.");  
      }  

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

    // Handle the SpeechDetected event.  
    static void SpeechDetectedHandler(object sender, SpeechDetectedEventArgs e)  
    {  
      Console.WriteLine(" In SpeechDetectedHandler:");  
      Console.WriteLine(" - AudioPosition = {0}", e.AudioPosition);  
    }  

    // Handle the SpeechHypothesized event.  
    static void SpeechHypothesizedHandler(  
      object sender, SpeechHypothesizedEventArgs e)  
    {  
      Console.WriteLine(" In SpeechHypothesizedHandler:");  

      string grammarName = "<not available>";  
      string resultText = "<not available>";  
      if (e.Result != null)  
      {  
        if (e.Result.Grammar != null)  
        {  
          grammarName = e.Result.Grammar.Name;  
        }  
        resultText = e.Result.Text;  
      }  

      Console.WriteLine(" - Grammar Name = {0}; Result Text = {1}",  
        grammarName, resultText);  
    }  

    // Handle the SpeechRecognitionRejected event.  
    static void SpeechRecognitionRejectedHandler(  
      object sender, SpeechRecognitionRejectedEventArgs e)  
    {  
      Console.WriteLine(" In SpeechRecognitionRejectedHandler:");  

      string grammarName = "<not available>";  
      string resultText = "<not available>";  
      if (e.Result != null)  
      {  
        if (e.Result.Grammar != null)  
        {  
          grammarName = e.Result.Grammar.Name;  
        }  
        resultText = e.Result.Text;  
      }  

      Console.WriteLine(" - Grammar Name = {0}; Result Text = {1}",  
        grammarName, resultText);  
    }  

    // Handle the SpeechRecognized event.  
    static void SpeechRecognizedHandler(  
      object sender, SpeechRecognizedEventArgs e)  
    {  
      Console.WriteLine(" In SpeechRecognizedHandler.");  

      string grammarName = "<not available>";  
      string resultText = "<not available>";  
      if (e.Result != null)  
      {  
        if (e.Result.Grammar != null)  
        {  
          grammarName = e.Result.Grammar.Name;  
        }  
        resultText = e.Result.Text;  
      }  

      Console.WriteLine(" - Grammar Name = {0}; Result Text = {1}",  
        grammarName, resultText);  
    }  

    // Handle the RecognizeCompleted event.  
    static void RecognizeCompletedHandler(  
      object sender, RecognizeCompletedEventArgs e)  
    {  
      Console.WriteLine(" In RecognizeCompletedHandler.");  

      if (e.Error != null)  
      {  
        Console.WriteLine(  
          " - Error occurred during recognition: {0}", e.Error);  
        return;  
      }  
      if (e.InitialSilenceTimeout || e.BabbleTimeout)  
      {  
        Console.WriteLine(  
          " - BabbleTimeout = {0}; InitialSilenceTimeout = {1}",  
          e.BabbleTimeout, e.InitialSilenceTimeout);  
        return;  
      }  
      if (e.InputStreamEnded)  
      {  
        Console.WriteLine(  
          " - AudioPosition = {0}; InputStreamEnded = {1}",  
          e.AudioPosition, e.InputStreamEnded);  
      }  
      if (e.Result != null)  
      {  
        Console.WriteLine(  
          " - Grammar = {0}; Text = {1}; Confidence = {2}",  
          e.Result.Grammar.Name, e.Result.Text, e.Result.Confidence);  
        Console.WriteLine(" - AudioPosition = {0}", e.AudioPosition);  
      }  
      else  
      {  
        Console.WriteLine(" - No result.");  
      }  

      completed = true;  
    }  
  }  
}  

注釈

が の場合mode、 または RecognizeAsyncStop メソッドが呼び出されるまで、認識エンジンは非同期認識操作の実行をRecognizeAsyncCancel続行Multipleします。

このメソッドの呼び出し中に、認識エンジンは次のイベントを発生させることができます。

非同期認識操作の結果を取得するには、イベント ハンドラーを認識エンジンの SpeechRecognized イベントにアタッチします。 認識エンジンは、同期または非同期の認識操作が正常に完了するたびに、このイベントを発生させます。 認識が成功しなかった場合、イベントのResultRecognizeCompletedEventArgsハンドラーRecognizeCompletedでアクセスできる オブジェクトの プロパティは になりますnull

非同期認識操作は、次の理由で失敗する可能性があります。

  • または InitialSilenceTimeout プロパティのタイムアウト間隔の有効期限が切れる前に、音声がBabbleTimeout検出されません。

  • 認識エンジンは音声を検出しますが、読み込まれて有効になっている Grammar オブジェクトのいずれにも一致が見つかりません。

同期認識を実行するには、いずれかの方法を Recognize 使用します。

こちらもご覧ください

適用対象