次の方法で共有


SpeechRecognitionEngine.LoadGrammarCompleted イベント

定義

SpeechRecognitionEngineGrammar オブジェクトの非同期読み込みを終了するときに発生します。

public:
 event EventHandler<System::Speech::Recognition::LoadGrammarCompletedEventArgs ^> ^ LoadGrammarCompleted;
public event EventHandler<System.Speech.Recognition.LoadGrammarCompletedEventArgs> LoadGrammarCompleted;
member this.LoadGrammarCompleted : EventHandler<System.Speech.Recognition.LoadGrammarCompletedEventArgs> 
Public Custom Event LoadGrammarCompleted As EventHandler(Of LoadGrammarCompletedEventArgs) 
Public Event LoadGrammarCompleted As EventHandler(Of LoadGrammarCompletedEventArgs) 

イベントの種類

次の例では、インプロセス音声認識エンジンを作成し、特定の単語を認識し、無料のディクテーションを受け入れるための 2 種類の文法を作成します。 この例では、 Grammar 完成した各音声認識文法からオブジェクトを構築し、そのオブジェクトを Grammar インスタンスに非同期的に SpeechRecognitionEngine 読み込みます。 認識エンジンとSpeechRecognizedイベントのLoadGrammarCompletedハンドラーは、認識の実行に使用されたオブジェクトのGrammar名前と認識結果のテキストをそれぞれコンソールに書き込みます。

using System;
using System.Speech.Recognition;

namespace SampleRecognition
{
  class Program
  {
    private static SpeechRecognitionEngine recognizer;
    public static void Main(string[] args)
    {

      // Initialize an in-process speech recognition engine and set its input.
      recognizer = new SpeechRecognitionEngine();
      recognizer.SetInputToDefaultAudioDevice();

      // Add a handler for the LoadGrammarCompleted event.
      recognizer.LoadGrammarCompleted +=
        new EventHandler<LoadGrammarCompletedEventArgs>(recognizer_LoadGrammarCompleted);

      // Add a handler for the SpeechRecognized event.
      recognizer.SpeechRecognized +=
        new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);

      // Create the "yesno" grammar.
      Choices yesChoices = new Choices(new string[] { "yes", "yup", "yeah" });
      SemanticResultValue yesValue =
          new SemanticResultValue(yesChoices, (bool)true);
      Choices noChoices = new Choices(new string[] { "no", "nope", "neah" });
      SemanticResultValue noValue =
          new SemanticResultValue(noChoices, (bool)false);
      SemanticResultKey yesNoKey =
          new SemanticResultKey("yesno", new Choices(new GrammarBuilder[] { yesValue, noValue }));
      Grammar yesnoGrammar = new Grammar(yesNoKey);
      yesnoGrammar.Name = "yesNo";

      // Create the "done" grammar.
      Grammar doneGrammar =
        new Grammar(new Choices(new string[] { "done", "exit", "quit", "stop" }));
      doneGrammar.Name = "Done";

      // Create a dictation grammar.
      Grammar dictation = new DictationGrammar();
      dictation.Name = "Dictation";

      // Load grammars to the recognizer.
      recognizer.LoadGrammarAsync(yesnoGrammar);
      recognizer.LoadGrammarAsync(doneGrammar);
      recognizer.LoadGrammarAsync(dictation);

      // Start asynchronous, continuous recognition.
      recognizer.RecognizeAsync(RecognizeMode.Multiple);

      // Keep the console window open.
      Console.ReadLine();
    }

    // Handle the LoadGrammarCompleted event.
    static void recognizer_LoadGrammarCompleted(object sender, LoadGrammarCompletedEventArgs e)
    {
      string grammarName = e.Grammar.Name;
      bool grammarLoaded = e.Grammar.Loaded;

      if (e.Error != null)
      {
        Console.WriteLine("LoadGrammar for {0} failed with a {1}.",
        grammarName, e.Error.GetType().Name);

        // Add exception handling code here.
      }

      Console.WriteLine("Grammar {0} {1} loaded.",
      grammarName, (grammarLoaded) ? "is" : "is not");
    }

    // Handle the SpeechRecognized event.
    static void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
    {
      Console.WriteLine("Grammar({0}): {1}", e.Result.Grammar.Name, e.Result.Text);

      // Add event handler code here.
    }
  }
}

注釈

認識エンジンの LoadGrammarAsync メソッドは、非同期操作を開始します。 は SpeechRecognitionEngine 、操作の完了時にこのイベントを発生させます。 認識エンジンがGrammar読み込んだオブジェクトを取得するには、関連付けられている LoadGrammarCompletedEventArgsGrammar プロパティを使用します。 認識エンジンが読み込んだ現在 Grammar のオブジェクトを取得するには、認識エンジンの Grammars プロパティを使用します。

認識エンジンが実行されている場合、アプリケーションは を使用 RequestRecognizerUpdate して音声認識エンジンを一時停止してから、文法の読み込み、アンロード、有効化、または無効化を行う必要があります。

LoadGrammarCompleted デリゲートを作成する場合は、イベントを処理するメソッドを指定します。 イベント ハンドラーにイベントを関連付けるには、イベントにデリゲートのインスタンスを追加します。 イベント ハンドラーは、デリゲートを削除しない限り、イベントが発生するたびに呼び出されます。 イベント ハンドラー デリゲートの詳細については、「 イベントとデリゲート」を参照してください。

適用対象

こちらもご覧ください