[Windows Phone]Speech Recognizer介紹
這邊將介紹不使用SpeechRecognizerUI 的Speech Recognizer。簡單說就是沒有使用系統提供的GUI介面,來進行語音辨識的功能。
在功能列須打開
「ID_CAP_MICROPHONE」
「ID_CAP_NETWORKING」
「ID_CAP_SPEECH_RECOGNITION」
1.在 Mainpage.xaml新增TextBlock及Button。
1: <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
2: <StackPanel Orientation="Vertical">
3: <TextBlock x:Name="textblock" Text="" FontSize="30" HorizontalAlignment="Center"/>
4: <Button x:Name="button" Click="button_Click" Content="speaker" />
   5:     </StackPanel>
		
		
   6: </Grid>      
		
2.到cs裡宣告全域變數,讓button事件可以使用。
之後到MainPage裡,初始化Recognizer、設定Grammars及註冊AudioCaptureStateChanged事件。
1: public MainPage()
   2: {
		
		
   3:    InitializeComponent();
		
		4: Recognizer = new SpeechRecognizer();//初始化
5: Recognizer.Grammars.AddGrammarFromPredefinedType("Search",SpeechPredefinedGrammar.WebSearch);//設定WebSearch的Grammar
   6:    Recognizer.AudioCaptureStateChanged += Recognizer_AudioCaptureStateChanged;
		
		
   7:        }
		這裡的Grammars使用系統預設的Type,而辨識來源由Web提供,
3.系統會產生Recognizer_AudioCaptureStateChanged事件,裡面要處理辨識的狀態。
1: if (args.State == SpeechRecognizerAudioCaptureState.Capturing)
   2: {
		
		3: this.Dispatcher.BeginInvoke(delegate { textblock.Text = "Listening"; });
   4: }
		
		5: else if (args.State == SpeechRecognizerAudioCaptureState.Inactive)
   6:  {
		
		7: this.Dispatcher.BeginInvoke(delegate { textblock.Text = "Thinking"; });
   8:  }
		若Recognizer正在等待音訊輸入時,Textblock顯示「Listening」,若擷取完畢則顯示「Thniking」。
4.到Button事件裡處理語音辨識
1: private async void button_Click(object sender, RoutedEventArgs e)
   2: {
		
		3: SpeechRecognitionResult recoResult = await Recognizer.RecognizeAsync();//開啟識別
4: // 確認識別的結果是否與片語相符
5: if (recoResult.TextConfidence == SpeechRecognitionConfidence.Rejected)
   6:     {
		
		7: textblock.Text = "sorry,didn't catch that.\n\n say again.";
   8:     }
		
		9: //若太小聲則請使用者在說一次
10: else if (recoResult.TextConfidence == SpeechRecognitionConfidence.Low)
  11:     {
		
		12: textblock.Text = "not sure what you said.\n\nSay again";
  13:     }
		
		14: //若精確度在等級低之上,則輸出使用者所說的話
15: else if (recoResult.TextConfidence == SpeechRecognitionConfidence.Medium ||
  16:         recoResult.TextConfidence == SpeechRecognitionConfidence.High)
		
		
  17:     {
		
		18: textblock.Text = "Heard you say: \n\n" + recoResult.Text;
  19:     }
		
		
  20: }
		
Reference
Windows Phone 8 – SpeechRecognizer
Starting speech recognition for Windows Phone 8
若有觀念錯誤、內容錯誤,勞請告知。 謝謝。
若要轉載請註明出處,謝謝。