信息
您所需的主题如下所示。但此主题未包含在此库中。

为 Windows Phone 8 选择语音识别器

2014/6/18

仅适用于:Windows Phone 8 和 Windows Phone Silverlight 8.1

Windows Phone 8 包括用于各种语言的语音识别器。每个语音识别器识别一种在特定国家/地区使用的语言。创建 SpeechRecognizerSpeechRecognizerUI 对象后,您可以指定要加载的语音识别器的语言。SpeechRecognizerSpeechRecognizerUI 实例可以加载任意已安装在手机上的语音并使用它来识别语音。如果没有指定任何语言,API 将加载与用户在手机上的“设置”/“语音”中选择的语言匹配的识别器。

以下代码示例将创建语音识别器的实例并借助 LINQ 查询设置其语言。LINQ 查询在描述每个已安装识别器的 SpeechRecognizerInformation 对象中进行搜索,以查找其语言属性的值为“fr-FR”(它表示在法国讲的法语)的对象。变量 frenchRecognizers 被隐式类型化为 SpeechRecognizerInformation 对象。SetRecognizer(SpeechRecognizerInformation) 方法的参数为 LINQ 查询返回的识别器指定一个索引。

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

您也可以使用 SpeechRecognizerInformation 对象设置一个与已安装的语音识别器(如,SpeechRecognizerInformationDescription()SpeechRecognizerInformationDisplayName()SpeechRecognizerInformationId())的其他属性值相匹配的语音识别器。

显示: