Partilhar via


Note

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

SpeechRecognitionEngine Constructor (CultureInfo)

Initializes a new instance of the SpeechRecognitionEngine class using the default speech recognizer for a specified locale.

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

Syntax

'Declaration
Public Sub New ( _
    culture As CultureInfo _
)
'Usage
Dim culture As CultureInfo

Dim instance As New SpeechRecognitionEngine(culture)
public SpeechRecognitionEngine(
    CultureInfo culture
)

Parameters

Exceptions

Exception Condition
ArgumentException

None of the installed speech recognizers support the specified locale, or culture is the invariant culture.

ArgumentNullException

Culture is a null reference (Nothing in Visual Basic).

Remarks

If the system does not support the locale defined by culture, an exception will be generated on construction.

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:

A recognizer is an installed Runtime Language for speech recognition. The Microsoft Speech Platform Runtime 11 and Microsoft Speech Platform SDK 11 do not include any Runtime Languages for speech recognition. You must download and install a Runtime Language for each language in which you want to recognize speech. 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. See InstalledRecognizers() for more information.

The Runtime Languages are different for each version of the Speech Platform Runtime. You must download the Runtime Language version that matches the version of the Speech Platform Runtime that you have installed. The Runtime Languages for the Speech Platform SDK 11 are redistributable and are different than the languages that ship with Windows Vista or Windows 7. Use the following link to download Runtime Languages for version 11 of the Speech Platform Runtime:

See Language Support for a list of languages for which you can download Runtime Languages.

Examples

The following example initializes a SpeechRecognitionEngine object that selects the default speech recognition engine for US English (en-US).

using System;
using Microsoft.Speech.Recognition;

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

      // Create a SpeechRecognitionEngine instance that selects 
      // the default speech recognition engine for US English.
      using (SpeechRecognitionEngine recognizer =
         new SpeechRecognitionEngine(new System.Globalization.CultureInfo("en-US")))
      {

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