《深入淺出Windows Phone 8應用開發》之發音合成與語音識別
Windows Phone從一開始就具有了強大的語音功能,我們可以長按開始鍵就可以調用手機的語音識別界面,然后可以通過語音來進行啟動一些任務。那么在Windows Phone 8里面,語音控制的編程接口都開放了相關的API給應用程序調用,所以在應用程序里面也一樣可以實現語音的控制。
發音的合成
發音的合成是指把文本轉化為語音由手機系統進行發音,從而實現了把文本自動轉化為了更加自然化的聲音。在Windows Phone 8里面可以使用SpeechSynthesizer類來實現發音合成的功能,通過SpeakTextAsync方法可以直接文本轉化為聲音並且播放。
下面給出發音合成的示例:使用發音合成對文本的內容進行發音。
代碼清單:發音合成
MainPage.xaml文件主要代碼
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> <StackPanel > <TextBox Name="textBox1" Text="Hello World!" /> <Button Content="發音" Name="button1" Click="button1_Click" /> <TextBlock x:Name="erro"/> </StackPanel> </Grid>
MainPage.xaml.cs文件主要代碼
SpeechSynthesizer voice;//語音合成對象 public MainPage() { InitializeComponent(); this.voice = new SpeechSynthesizer(); } private async void button1_Click(object sender, RoutedEventArgs e) { try { if (textBox1.Text!="") { button1.IsEnabled = false; await voice.SpeakTextAsync(textBox1.Text); //文本語音合成 button1.IsEnabled = true; } else { MessageBox.Show("請輸入要讀取的內容"); } } catch (Exception ex) { erro.Text = ex.ToString(); } }
程序運行的效果如圖10.21所示。
圖10.21 發音合成
語音識別
語音識別是指讓手機通過識別和理解過程把語音信號轉變為相應的文本或命令。在Windows Phone 8里面語音識別分為兩種類型一種是使用用戶自定義的UI頁面,另外一種是使用系統默認的語音識別界面也就是我們長按開始鍵的語音識別界面。使用語音識別的功能需要在WMAppManifest.xml文件中添加兩種功能要求ID_CAP_SPEECH_RECOGNITION和ID_CAP_MICROPHONE。下面分別來介紹一下這兩種語音識別的編程。
自定義語音識別界面可以通過SpeechRecognizer類來實現,首先需要先添加監聽的語法,然后通過使用SpeechRecognizer類RecognizeAsync方法來監聽語音的識別。
下面給出數字語音識別的示例:對1到10的英文數字發音進行監控,如果監聽到數字的發音則把英文數字單詞顯示出來。
代碼清單:數字語音識別
MainPage.xaml文件主要代碼
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> <StackPanel> <TextBlock Text="語音識別的內容:"/> <TextBlock x:Name="tbOutPut" Text=""/> <Button Content="開始識別" Name="continuousRecoButton" Click="continuousRecoButton_Click" /> </StackPanel> </Grid>
MainPage.xaml.cs文件主要代碼
using System; using System.Collections.Generic; using System.Windows; using Microsoft.Phone.Controls; using Windows.Foundation; using Windows.Phone.Speech.Recognition; namespace SpeechRecognizerDemo { public partial class MainPage : PhoneApplicationPage { SpeechRecognizer recognizer; //語音識別對象 IAsyncOperation<SpeechRecognitionResult> recoOperation; //語音識別操作任務 bool recoEnabled = false; //判斷是否停止監聽 public MainPage() { InitializeComponent(); try { //創建一個語音識別類 this.recognizer = new SpeechRecognizer(); // 添加監聽的單詞列表 this.recognizer.Grammars.AddGrammarFromList("Number", new List<string>() { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten" }); } catch (Exception err) { tbOutPut.Text = "Error: " + err.Message; } } //按鈕單擊事件處理 private async void continuousRecoButton_Click(object sender, RoutedEventArgs e) { if (this.recoEnabled) { this.recoEnabled = false; this.continuousRecoButton.Content = "開始識別"; this.recoOperation.Cancel(); return; } else { this.recoEnabled = true; this.continuousRecoButton.Content = "取消識別"; } do { try { // 捕獲語音的結果 this.recoOperation = recognizer.RecognizeAsync(); var recoResult = await this.recoOperation; // 音量過低無法識別 if ((int)recoResult.TextConfidence < (int)SpeechRecognitionConfidence.Medium) { tbOutPut.Text = "說話聲音太小"; } else { tbOutPut.Text = recoResult.Text; } } catch (System.Threading.Tasks.TaskCanceledException) { // 忽略語音識別的取消異常 } catch (Exception err) { const int privacyPolicyHResult = unchecked((int)0x80045509); if (err.HResult == privacyPolicyHResult) { MessageBox.Show("尚未接受語音隱私協議。"); this.recoEnabled = false; this.continuousRecoButton.Content = "開始識別"; } else { tbOutPut.Text = "Error: " + err.Message; } } } while (this.recoEnabled);//循環進行監聽語音 } } }
程序運行的效果如圖10.22所示。
圖10.21 語音識別數字
系統語音識別界面可以通過SpeechRecognizerUI類來實現,使用的基本語法與SpeechRecognizer類相類似,系統的語音識別界面通過SpeechRecognizerUI類Settings屬性來設置,Settings.ListenText表示界面的標題,Settings.ExampleText表示界面的示例內容。
下面給出數字語音識別系統界面的示例:使用系統地語音識別界面,對1到10的英文數字發音進行監控,如果監聽到數字的發音則把英文數字單詞顯示出來。
代碼清單:數字語音識別
MainPage.xaml文件主要代碼
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> <StackPanel> <TextBlock Text="語音識別的內容:"/> <TextBlock x:Name="tbOutPut" Text=""/> <Button Content="開始識別" Name="continuousRecoButton" Click="continuousRecoButton_Click" /> </StackPanel> </Grid>
MainPage.xaml.cs文件主要代碼
using System; using System.Collections.Generic; using System.Windows; using Microsoft.Phone.Controls; using Windows.Phone.Speech.Recognition; namespace SpeechRecognizerUIDemo { public partial class MainPage : PhoneApplicationPage { SpeechRecognizerUI recognizer; //語音識別對象 public MainPage() { InitializeComponent(); try { //創建一個語音識別類 this.recognizer = new SpeechRecognizerUI(); // 語音彈出框的標題 this.recognizer.Settings.ListenText = "說出一個1到10的英文單詞"; // 語音彈出框的示例內容 this.recognizer.Settings.ExampleText = "例如, 'one' or 'two'"; // 添加監聽的單詞列表 this.recognizer.Recognizer.Grammars.AddGrammarFromList("Number", new List<string>() { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten" }); } catch (Exception err) { tbOutPut.Text = "Error: " + err.Message; } } //按鈕單擊事件處理 private async void continuousRecoButton_Click(object sender, RoutedEventArgs e) { // 開始啟動系統的語音識別UI並且等待用戶的回答 SpeechRecognitionUIResult recognizerResult = await this.recognizer.RecognizeWithUIAsync(); // 確認識別是否成功和音量達到要求 if (recognizerResult.ResultStatus == SpeechRecognitionUIStatus.Succeeded&& recognizerResult.RecognitionResult.TextConfidence == SpeechRecognitionConfidence.High) { tbOutPut.Text = recognizerResult.RecognitionResult.Text; } else { tbOutPut.Text = "音量太小"; } } } }
程序運行的效果如圖10.23所示。
圖10.23 系統語音識別界面