Export (0) Print
Expand All

SpeechRecognitionEngine.LoadGrammar Method

Synchronously loads a Grammar object.

Namespace:  System.Speech.Recognition
Assembly:  System.Speech (in System.Speech.dll)

public void LoadGrammar(
	Grammar grammar
)

Parameters

grammar
Type: System.Speech.Recognition.Grammar

The grammar object to load.

ExceptionCondition
ArgumentNullException

Grammar is null.

InvalidOperationException

Grammar is not in a valid state.

The recognizer throws an exception if the Grammar object is already loaded, is being asynchronously loaded, or has failed to load into any recognizer. You cannot load the same Grammar object into multiple instances of SpeechRecognitionEngine. Instead, create a new Grammar object for each SpeechRecognitionEngine instance.

If the recognizer is running, applications must use RequestRecognizerUpdate to pause the speech recognition engine before loading, unloading, enabling, or disabling a grammar.

When you load a grammar, it is enabled by default. To disable a loaded grammar, use the Enabled property.

To load a Grammar object asynchronously, use the LoadGrammarAsync method.

The following example shows part of a console application that demonstrates basic speech recognition. The example creates a DictationGrammar and loads it into a speech recognizer.

using System;
using System.Speech.Recognition;

namespace SpeechRecognitionApp
{
  class Program
  {
    static void Main(string[] args)
    {

      // Create an in-process speech recognizer for the en-US locale.
      using (
      SpeechRecognitionEngine recognizer =
        new SpeechRecognitionEngine(
          new System.Globalization.CultureInfo("en-US")))
      {

        // Create and load a dictation grammar.
        recognizer.LoadGrammar(new DictationGrammar());

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

        // Configure input to the speech recognizer.
        recognizer.SetInputToDefaultAudioDevice();

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

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

    // Handle the SpeechRecognized event.
    static void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
    {
      Console.WriteLine("Recognized text: " + e.Result.Text);
    }
  }
}

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft