Compartir a través de


Note

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

SpeechRecognitionEngine Constructor (String)

Initializes a new instance of the SpeechRecognitionEngine class with a string parameter that specifies the name of the recognizer to use.

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

Syntax

'Declaration
Public Sub New ( _
    recognizerId As String _
)
'Usage
Dim recognizerId As String

Dim instance As New SpeechRecognitionEngine(recognizerId)
public SpeechRecognitionEngine(
    string recognizerId
)

Parameters

  • recognizerId
    Type: System.String
    The token name of the speech recognizer to use.

Exceptions

Exception Condition
ArgumentNullException

Thrown if recognizerId is a null reference (Nothing in Visual Basic) or an empty string.

ArgumentException

Thrown if recognizerId does not match any installed speech recognizers.

Remarks

The token name of the recognizer is the value of the Id property of the RecognizerInfo object returned by the RecognizerInfo property of the SpeechRecognitionEngine object. To get a collection of all the installed recognizers, use the static InstalledRecognizers() method.

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 creates an instance of SpeechRecognitionEngine by specifying the precise name of the speech recognition engine to use.

using System;
using Microsoft.Speech.Recognition;

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

      // Create a SpeechRecognitionEngine instance for the installed
      // speech recognition engine named SR_MS_en-US_TELE_10.0.
      using (SpeechRecognitionEngine recognizer =
        new SpeechRecognitionEngine("SR_MS_en-US_TELE_10.0"))
      {

        // Create a grammar.
        // Create lists of alternative choices.
        Choices listTypes = new Choices(new string[] { "albums", "artists" });
        Choices genres = new Choices(new string[] { 
          "blues", "classical", "gospel", "jazz", "rock" });

        // Create a GrammarBuilder object and assemble the grammar components.
        GrammarBuilder mediaMenu = new GrammarBuilder("Display");
        mediaMenu.Append("the list of", 0, 1);
        mediaMenu.Append(listTypes);
        mediaMenu.Append("in the", 0, 1);
        mediaMenu.Append(genres);
        mediaMenu.Append("category", 0, 1);

        // Build a Grammar object from the GrammarBuilder.
        Grammar mediaMenuGrammar = new Grammar(mediaMenu);
        mediaMenuGrammar.Name = "Media Chooser";

        // Load the Grammar object to the SpeechRecognitionEngine.
        recognizer.LoadGrammarAsync(mediaMenuGrammar);

        // 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