다음을 통해 공유


Selecting a speech recognizer for Windows Phone 8

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Windows Phone 8 includes speech recognizers for a variety of languages. Each speech recognizer performs recognition in a single language, as spoken in a specific country/region. After you create a SpeechRecognizer or SpeechRecognizerUI object, you can specify the language of the speech recognizer to load. A SpeechRecognizer or SpeechRecognizerUI instance can load any voice that is installed on the phone and use it to recognize speech. If no language is specified, the API will load a recognizer that matches the language that the user selected in Settings/Speech on the phone.

Selecting a speech recognizer code example

The following code example creates an instance of a speech recognizer and sets its language with the help of a LINQ query. The LINQ query searches through the SpeechRecognizerInformation objects that describe each of the installed recognizers to find one with a language property with a value of "fr-FR", which indicates the French language as spoken in France. The variable frenchRecognizers is implicitly typed as a SpeechRecognizerInformation object. The argument to the SetRecognizer(SpeechRecognizerInformation) method specifies an index for the recognizers returned by the LINQ query.

// Declare the SpeechRecognizerUI object at the class level.
SpeechRecognizerUI recoWithUI;

// Handle the button click event.
private async void SpeakFrench_Click_1(object sender, RoutedEventArgs e)
{
  // Initialize the SpeechRecognizerUI object.
  recoWithUI = new SpeechRecognizerUI();

  // Query for a recognizer that recognizes French as spoken in France.
  IEnumerable<SpeechRecognizerInformation> frenchRecognizers = from recognizerInfo in InstalledSpeechRecognizers.All
                          where recognizerInfo.Language == "fr-FR"
                          select recognizerInfo;
            
  // Set the recognizer to the top entry in the query result.
  recoWithUI.Recognizer.SetRecognizer(frenchRecognizers.ElementAt(0));

  // Create a string array of French numbers.
  string[] nombres = { "un", "deux", "trois", "quatre", "cinq", 
                         "six", "sept", "huit", "neuf", "dix" };

  // Create a list grammar from the string array and add it to the grammar set.
  recoWithUI.Recognizer.Grammars.AddGrammarFromList("frenchNumbers", nombres);

  // Display text to prompt the user's input.
  recoWithUI.Settings.ListenText = "Say a French number";

  // Give an example of ideal speech input.
  recoWithUI.Settings.ExampleText = " 'un', 'deux', 'trois', 'quatre' ";

  // Load the grammar set and start recognition.
  SpeechRecognitionUIResult recoResult = await recoWithUI.RecognizeWithUIAsync();
}

You can also use the SpeechRecognizerInformation object to set a speech recognizer that matches other property values of installed speech recognizers, such as SpeechRecognizerInformationDescription()()(), SpeechRecognizerInformationDisplayName()()(), and SpeechRecognizerInformationId()()().

See Also

Other Resources

Speech recognition for Windows Phone 8

Speech for Windows Phone 8