深入浅出Windows Phone 8应用开发》之发音合成与语音识别

    Windows Phone从一开始就具有了强大的语音功能,我们可以长按开始键就可以调用手机的语音识别界面,然后可以通过语音来进行启动一些任务。那么在Windows Phone 8里面,语音控制的编程接口都开放了相关的API给应用程序调用,所以在应用程序里面也一样可以实现语音的控制。

发音的合成

    发音的合成是指把文本转化为语音由手机系统进行发音,从而实现了把文本自动转化为了更加自然化的声音。在Windows Phone 8里面可以使用SpeechSynthesizer类来实现发音合成的功能,通过SpeakTextAsync方法可以直接文本转化为声音并且播放。

    下面给出发音合成的示例:使用发音合成对文本的内容进行发音。

代码清单:发音合成

MainPage.xaml文件主要代码

 
  
  1. <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 
  2.    <StackPanel > 
  3.         <TextBox Name="textBox1" Text="Hello World!"    /> 
  4.         <Button Content="发音" Name="button1"  Click="button1_Click" /> 
  5.        <TextBlock x:Name="erro"/> 
  6.     </StackPanel> 
  7. </Grid>

MainPage.xaml.cs文件主要代码

 
  
  1. SpeechSynthesizer voice;//语音合成对象 
  2.  public MainPage() 
  3.  { 
  4.      InitializeComponent(); 
  5.      this.voice = new SpeechSynthesizer(); 
  6.  } 
  7.  private async void button1_Click(object sender, RoutedEventArgs e) 
  8.  { 
  9.      try 
  10.      { 
  11.          if (textBox1.Text!=""
  12.          { 
  13.              button1.IsEnabled = false
  14.              await voice.SpeakTextAsync(textBox1.Text); //文本语音合成 
  15.              button1.IsEnabled = true
  16.          } 
  17.          else 
  18.          { 
  19.              MessageBox.Show("请输入要读取的内容"); 
  20.          } 
  21.      } 
  22.      catch (Exception ex) 
  23.      { 
  24.          erro.Text = ex.ToString(); 
  25.      } 
  26.  }

程序运行的效果如图10.21所示。

 

     图10.21 发音合成

语音识别

    语音识别是指让手机通过识别和理解过程把语音信号转变为相应的文本或命令。在Windows Phone 8里面语音识别分为两种类型一种是使用用户自定义的UI页面,另外一种是使用系统默认的语音识别界面也就是我们长按开始键的语音识别界面。使用语音识别的功能需要在WMAppManifest.xml文件中添加两种功能要求ID_CAP_SPEECH_RECOGNITION和ID_CAP_MICROPHONE。下面分别来介绍一下这两种语音识别的编程。

    自定义语音识别界面可以通过SpeechRecognizer类来实现,首先需要先添加监听的语法,然后通过使用SpeechRecognizer类RecognizeAsync方法来监听语音的识别。

    下面给出数字语音识别的示例:对1到10的英文数字发音进行监控,如果监听到数字的发音则把英文数字单词显示出来。

代码清单:数字语音识别

MainPage.xaml文件主要代码

 
  
  1. <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 
  2.     <StackPanel> 
  3.         <TextBlock  Text="语音识别的内容:"/> 
  4.         <TextBlock x:Name="tbOutPut" Text=""/> 
  5.         <Button Content="开始识别"  Name="continuousRecoButton" Click="continuousRecoButton_Click" /> 
  6.     </StackPanel> 
  7. </Grid> 

MainPage.xaml.cs文件主要代码

 
  
  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Windows; 
  4. using Microsoft.Phone.Controls; 
  5. using Windows.Foundation; 
  6. using Windows.Phone.Speech.Recognition; 
  7. namespace SpeechRecognizerDemo 
  8.     public partial class MainPage : PhoneApplicationPage 
  9.     { 
  10.         SpeechRecognizer recognizer;     //语音识别对象 
  11.         IAsyncOperation<SpeechRecognitionResult> recoOperation; //语音识别操作任务 
  12.         bool recoEnabled = false;    //判断是否停止监听 
  13.         public MainPage() 
  14.         { 
  15.             InitializeComponent(); 
  16.             try 
  17.             { 
  18.                 //创建一个语音识别类 
  19.                 this.recognizer = new SpeechRecognizer(); 
  20.                 // 添加监听的单词列表 
  21.                 this.recognizer.Grammars.AddGrammarFromList("Number"new List<string>() { "one""two""three""four""five""six""seven""eight""nine""ten" }); 
  22.             } 
  23.             catch (Exception err) 
  24.             { 
  25.                 tbOutPut.Text = "Error: " + err.Message; 
  26.             } 
  27.         } 
  28.         //按钮单击事件处理 
  29.         private async void continuousRecoButton_Click(object sender, RoutedEventArgs e) 
  30.         { 
  31.             if (this.recoEnabled) 
  32.             { 
  33.                 this.recoEnabled = false
  34.                 this.continuousRecoButton.Content = "开始识别"
  35.                 this.recoOperation.Cancel(); 
  36.                 return
  37.             } 
  38.             else 
  39.             { 
  40.                 this.recoEnabled = true
  41.                this.continuousRecoButton.Content = "取消识别"
  42.             } 
  43.             do 
  44.             { 
  45.                 try 
  46.                 { 
  47.                     // 捕获语音的结果 
  48.                     this.recoOperation = recognizer.RecognizeAsync(); 
  49.                     var recoResult = await this.recoOperation; 
  50.                     // 音量过低无法识别 
  51.                     if ((int)recoResult.TextConfidence < (int)SpeechRecognitionConfidence.Medium) 
  52.                     { 
  53.                         tbOutPut.Text = "说话声音太小"
  54.                     } 
  55.                     else 
  56.                     { 
  57.                         tbOutPut.Text = recoResult.Text; 
  58.                     } 
  59.                 } 
  60.                 catch (System.Threading.Tasks.TaskCanceledException) 
  61.                 { 
  62.                     // 忽略语音识别的取消异常 
  63.                 } 
  64.                 catch (Exception err) 
  65.                 { 
  66.                     const int privacyPolicyHResult = unchecked((int)0x80045509); 
  67.                     if (err.HResult == privacyPolicyHResult) 
  68.                     { 
  69.                         MessageBox.Show("尚未接受语音隐私协议。"); 
  70.                         this.recoEnabled = false
  71.                         this.continuousRecoButton.Content = "开始识别"
  72.                     } 
  73.                     else 
  74.                     { 
  75.                         tbOutPut.Text = "Error: " + err.Message; 
  76.                     } 
  77.                 } 
  78.             } while (this.recoEnabled);//循环进行监听语音 
  79.         } 
  80.     } 

 

 

程序运行的效果如图10.22所示。

 

     图10.21 语音识别数字

    系统语音识别界面可以通过SpeechRecognizerUI类来实现,使用的基本语法与SpeechRecognizer类相类似,系统的语音识别界面通过SpeechRecognizerUI类Settings属性来设置,Settings.ListenText表示界面的标题,Settings.ExampleText表示界面的示例内容。

    下面给出数字语音识别系统界面的示例:使用系统地语音识别界面,对1到10的英文数字发音进行监控,如果监听到数字的发音则把英文数字单词显示出来。

代码清单:数字语音识别

 MainPage.xaml文件主要代码 

 

 
  
  1. <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 
  2.     <StackPanel> 
  3.         <TextBlock  Text="语音识别的内容:"/> 
  4.         <TextBlock x:Name="tbOutPut" Text=""/> 
  5.         <Button Content="开始识别"  Name="continuousRecoButton" Click="continuousRecoButton_Click" /> 
  6.     </StackPanel> 
  7. </Grid> 

 MainPage.xaml.cs文件主要代码  

 
  
  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Windows; 
  4. using Microsoft.Phone.Controls; 
  5. using Windows.Phone.Speech.Recognition; 
  6. namespace SpeechRecognizerUIDemo 
  7.     public partial class MainPage : PhoneApplicationPage 
  8.     { 
  9.         SpeechRecognizerUI recognizer;     //语音识别对象 
  10.         public MainPage() 
  11.         { 
  12.             InitializeComponent(); 
  13.             try 
  14.             { 
  15.                 //创建一个语音识别类 
  16.                 this.recognizer = new SpeechRecognizerUI(); 
  17.                 // 语音弹出框的标题                 
  18.                 this.recognizer.Settings.ListenText = "说出一个1到10的英文单词"
  19.                 // 语音弹出框的示例内容 
  20.                 this.recognizer.Settings.ExampleText = "例如, 'one' or 'two'"
  21.                 // 添加监听的单词列表 
  22.                 this.recognizer.Recognizer.Grammars.AddGrammarFromList("Number"new List<string>() { "one""two""three""four""five""six""seven""eight""nine""ten" }); 
  23.             } 
  24.             catch (Exception err) 
  25.             { 
  26.                 tbOutPut.Text = "Error: " + err.Message; 
  27.             } 
  28.         } 
  29.         //按钮单击事件处理 
  30.         private async void continuousRecoButton_Click(object sender, RoutedEventArgs e) 
  31.         { 
  32.             // 开始启动系统的语音识别UI并且等待用户的回答 
  33.             SpeechRecognitionUIResult recognizerResult = await this.recognizer.RecognizeWithUIAsync(); 
  34.             // 确认识别是否成功和音量达到要求 
  35.             if (recognizerResult.ResultStatus == SpeechRecognitionUIStatus.Succeeded&& recognizerResult.RecognitionResult.TextConfidence == SpeechRecognitionConfidence.High) 
  36.             { 
  37.                 tbOutPut.Text = recognizerResult.RecognitionResult.Text; 
  38.             } 
  39.             else 
  40.             { 
  41.                 tbOutPut.Text = "音量太小"
  42.             } 
  43.         } 
  44.     } 

程序运行的效果如图10.23所示。

 

     图10.23 系统语音识别界面