[Windows Phone]SpeechRecognizerUI之應用

  • 834
  • 0
  • 2014-01-22

[Windows Phone]SpeechRecognizerUI之應用

這裡介紹使用自訂的SpeechRecognizerUI辨識中文。

 

需要將「ID_CAP_MICROPHONE」及「ID_CAP_SPEECH_RECOGNITION」打開。

 

1.在Mainpage.xaml建立一個Button及Textblock

 

 

 

2.之後我們建立一個async的事件,並使用try/catch。

   1: private async void SpeechRecognitionUI()
   2: {
   3:     try
   4:     {
   5:     }
   6:     catch (Exception ex)
   7:    {
   8:        MessageBox.Show(ex.Message);
   9:    }
  10: }

 

 

 

3.宣告並初始化SpeechRecognizerUI。

   1: SpeechRecognizerUI RecognizerUI = new SpeechRecognizerUI();

 

 

 

 

4.設定語言為「繁體中文」

 

   1: IEnumerable<SpeechRecognizerInformation> chineseRecognizers =
   2:             from recognizerInfo in InstalledSpeechRecognizers.All
   3:             where recognizerInfo.Language == "zh-TW"
   4:             select recognizerInfo;
   5: RecognizerUI.Recognizer.SetRecognizer(chineseRecognizers.ElementAt(0));

 

 

5.建立讓語音搜尋的字串

   1: string[] Number = { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十"  };

 

 

 

6.將剛剛的字串加入至Grammars裡

   1: RecognizerUI.Recognizer.Grammars.AddGrammarFromList("Number", Number);

 

 

 

7.載入所有的Grammars

   1: await RecognizerUI.Recognizer.PreloadGrammarsAsync();

 

 

 

8.RecognizerUI的設定

   1: RecognizerUI.Settings.ListenText = "請說一個數字";//標題文字
   2: RecognizerUI.Settings.ExampleText = "一、二、三…至十";//範例文字
   3: RecognizerUI.Settings.ReadoutEnabled = false;//是否複誦成功辨識之文字

 

 

 

9.取的語音辨識結果,並輸出該辨識文字。

   1: SpeechRecognitionUIResult recoResult = await RecognizerUI.RecognizeWithUIAsync();
   2:  
   3: if (recoResult.ResultStatus == SpeechRecognitionUIStatus.Succeeded)
   4: {
   5:     textblock.Text = recoResult.RecognitionResult.Text;
   6: }

 

 

 

整個SpeechRecognitionUI程式碼如下

   1: private async void SpeechRecognitionUI()
   2: {
   3:     try
   4:     {
   5:         SpeechRecognizerUI RecognizerUI = new SpeechRecognizerUI();
   6:         IEnumerable<SpeechRecognizerInformation> chineseRecognizers =
   7:                     from recognizerInfo in InstalledSpeechRecognizers.All
   8:                     where recognizerInfo.Language == "zh-TW"
   9:                     select recognizerInfo;
  10:         RecognizerUI.Recognizer.SetRecognizer(chineseRecognizers.ElementAt(0));
  11:         string[] Number = { "一", "二", "三", "四", "五", 
  12:                               "六", "七", "八", "九", "十"  };
  13:         RecognizerUI.Recognizer.Grammars.AddGrammarFromList("Number", Number);
  14:         await RecognizerUI.Recognizer.PreloadGrammarsAsync();
  15:         RecognizerUI.Settings.ListenText = "請說一個數字";//標題文字
  16:         RecognizerUI.Settings.ExampleText = "一、二、三…至十";//範例文字
  17:         RecognizerUI.Settings.ReadoutEnabled = false;//是否複誦成功辨識之文字
  18:         SpeechRecognitionUIResult recoResult = await RecognizerUI.RecognizeWithUIAsync();
  19:         if (recoResult.ResultStatus == SpeechRecognitionUIStatus.Succeeded)
  20:         {
  21:             textblock.Text = recoResult.RecognitionResult.Text;
  22:         }
  23:     }
  24:     catch (Exception ex)
  25:     {
  26:         MessageBox.Show(ex.Message);
  27:     }
  28:     
  29: }

 

 

 

 

 

 

 

Reference

Windows Phone 8 SpeechRecognizerUI

Windows Phone 8 – SpeechRecognizer

 

若有觀念錯誤、內容錯誤,勞請告知。 謝謝。

若要轉載請註明出處,謝謝。