Compartir a través de


Note

Please see Azure Cognitive Services for Speech documentation for the latest supported speech solutions.

SpeechRecognitionEngine Constructor (RecognizerInfo)

Initializes a new instance of the SpeechRecognitionEngine using the information in a RecognizerInfo object to specify the recognizer to use.

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

Syntax

'Declaration
Public Sub New ( _
    recognizerInfo As RecognizerInfo _
)
'Usage
Dim recognizerInfo As RecognizerInfo

Dim instance As New SpeechRecognitionEngine(recognizerInfo)
public SpeechRecognitionEngine(
    RecognizerInfo recognizerInfo
)

Parameters

Remarks

You can create an instance of this class for any of the installed recognizers. A recognizer is an installed Runtime Language for speech recognition. A Runtime Language includes the language model, acoustic model, and other data necessary to provision a speech engine to perform speech recognition in a particular language. The Microsoft Speech Platform Runtime 11 and Microsoft Speech Platform SDK 11 do not include Runtime Languages for speech recognition. You must download a Runtime Language for each language in which you want to perform speech recognition.

To get information about which speech recognizers are installed, use the InstalledRecognizers() method. See SpeechRecognitionEngine(CultureInfo) for information about downloading Runtime Languages to enable speech recognition for specific languages.

Before the speech recognizer can begin recognition, you must load at least one speech recognition grammar and configure the input for the recognizer.

To load a grammar, call the LoadGrammar or LoadGrammarAsync method.

To configure the audio input, use one of the following methods:

Examples

The following example uses the InstalledRecognizers() method to query for installed recognizers that support English. The results of the query populate a RecognizerInfo object, which is used to initialize the SpeechRecognitionEngineobject.

using System;
using Microsoft.Speech.Recognition;

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

      // Select a speech recognizer that supports English.
      RecognizerInfo info = null;
      foreach (RecognizerInfo ri in SpeechRecognitionEngine.InstalledRecognizers())
      {
        if (ri.Culture.TwoLetterISOLanguageName.Equals("en"))
        {
          info = ri;
          break;
        }
      }
      if (info == null) return;

      // Create the selected recognizer.
      using (SpeechRecognitionEngine recognizer =
        new SpeechRecognitionEngine(info))
      {

        // Create a grammar, construct a Grammar object, and load it to the recognizer.
        Choices animals = new Choices(new string[] { "cow", "pig", "goat" });
        GrammarBuilder farm = new GrammarBuilder("On this farm he had a");
        farm.Append(animals);

        Grammar farmAnimals = new Grammar(farm);
        farmAnimals.Name = "Farm";

        recognizer.LoadGrammar(farmAnimals);

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

See Also

Reference

SpeechRecognitionEngine Class

SpeechRecognitionEngine Members

SpeechRecognitionEngine Overload

Microsoft.Speech.Recognition Namespace