g87616758 发表于 2018-6-16 06:07:23

Windows Phone 8 发音合成与语音识别

  《深入浅出Windows Phone 8应用开发》之发音合成与语音识别
  Windows Phone从一开始就具有了强大的语音功能,我们可以长按开始键就可以调用手机的语音识别界面,然后可以通过语音来进行启动一些任务。那么在Windows Phone 8里面,语音控制的编程接口都开放了相关的API给应用程序调用,所以在应用程序里面也一样可以实现语音的控制。
  发音的合成
  发音的合成是指把文本转化为语音由手机系统进行发音,从而实现了把文本自动转化为了更加自然化的声音。在Windows Phone 8里面可以使用SpeechSynthesizer类来实现发音合成的功能,通过SpeakTextAsync方法可以直接文本转化为声音并且播放。
  下面给出发音合成的示例:使用发音合成对文本的内容进行发音。
  代码清单:发音合成
  MainPage.xaml文件主要代码
  


[*]<Grid x:Name=&quot;ContentPanel&quot; Grid.Row=&quot;1&quot; Margin=&quot;12,0,12,0&quot;>
[*]   <StackPanel >
[*]      <TextBox Name=&quot;textBox1&quot; Text=&quot;Hello World!&quot;    />
[*]      <Button Content=&quot;发音&quot; Name=&quot;button1&quot;Click=&quot;button1_Click&quot; />
[*]       <TextBlock x:Name=&quot;erro&quot;/>
[*]    </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!=&quot;&quot;)
[*]         {
[*]             button1.IsEnabled = false;
[*]             await voice.SpeakTextAsync(textBox1.Text); //文本语音合成
[*]             button1.IsEnabled = true;
[*]         }
[*]         else
[*]         {
[*]             MessageBox.Show(&quot;请输入要读取的内容&quot;);
[*]         }
[*]   }
[*]   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=&quot;ContentPanel&quot; Grid.Row=&quot;1&quot; Margin=&quot;12,0,12,0&quot;>
[*]    <StackPanel>
[*]      <TextBlockText=&quot;语音识别的内容:&quot;/>
[*]      <TextBlock x:Name=&quot;tbOutPut&quot; Text=&quot;&quot;/>
[*]      <Button Content=&quot;开始识别&quot;Name=&quot;continuousRecoButton&quot; Click=&quot;continuousRecoButton_Click&quot; />
[*]    </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(&quot;Number&quot;, new List<string>() { &quot;one&quot;, &quot;two&quot;, &quot;three&quot;, &quot;four&quot;, &quot;five&quot;, &quot;six&quot;, &quot;seven&quot;, &quot;eight&quot;, &quot;nine&quot;, &quot;ten&quot; });
[*]            }
[*]            catch (Exception err)
[*]            {
[*]                tbOutPut.Text = &quot;Error: &quot; + err.Message;
[*]            }
[*]      }
[*]      //按钮单击事件处理
[*]      private async void continuousRecoButton_Click(object sender, RoutedEventArgs e)
[*]      {
[*]            if (this.recoEnabled)
[*]            {
[*]                this.recoEnabled = false;
[*]                this.continuousRecoButton.Content = &quot;开始识别&quot;;
[*]                this.recoOperation.Cancel();
[*]                return;
[*]            }
[*]            else
[*]            {
[*]                this.recoEnabled = true;
[*]               this.continuousRecoButton.Content = &quot;取消识别&quot;;
[*]            }
[*]            do
[*]            {
[*]                try
[*]                {
[*]                  // 捕获语音的结果
[*]                  this.recoOperation = recognizer.RecognizeAsync();
[*]                  var recoResult = await this.recoOperation;
[*]                  // 音量过低无法识别
[*]                  if ((int)recoResult.TextConfidence < (int)SpeechRecognitionConfidence.Medium)
[*]                  {
[*]                        tbOutPut.Text = &quot;说话声音太小&quot;;
[*]                  }
[*]                  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(&quot;尚未接受语音隐私协议。&quot;);
[*]                        this.recoEnabled = false;
[*]                        this.continuousRecoButton.Content = &quot;开始识别&quot;;
[*]                  }
[*]                  else
[*]                  {
[*]                        tbOutPut.Text = &quot;Error: &quot; + err.Message;
[*]                  }
[*]                }
[*]            } while (this.recoEnabled);//循环进行监听语音
[*]      }
[*]    }
[*]}
  

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

  图10.21 语音识别数字
  系统语音识别界面可以通过SpeechRecognizerUI类来实现,使用的基本语法与SpeechRecognizer类相类似,系统的语音识别界面通过SpeechRecognizerUI类Settings属性来设置,Settings.ListenText表示界面的标题,Settings.ExampleText表示界面的示例内容。
  下面给出数字语音识别系统界面的示例:使用系统地语音识别界面,对1到10的英文数字发音进行监控,如果监听到数字的发音则把英文数字单词显示出来。
  代码清单:数字语音识别
  MainPage.xaml文件主要代码
  


[*]<Grid x:Name=&quot;ContentPanel&quot; Grid.Row=&quot;1&quot; Margin=&quot;12,0,12,0&quot;>
[*]    <StackPanel>
[*]      <TextBlockText=&quot;语音识别的内容:&quot;/>
[*]      <TextBlock x:Name=&quot;tbOutPut&quot; Text=&quot;&quot;/>
[*]      <Button Content=&quot;开始识别&quot;Name=&quot;continuousRecoButton&quot; Click=&quot;continuousRecoButton_Click&quot; />
[*]    </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 = &quot;说出一个1到10的英文单词&quot;;
[*]                // 语音弹出框的示例内容
[*]                this.recognizer.Settings.ExampleText = &quot;例如, 'one' or 'two'&quot;;
[*]                // 添加监听的单词列表
[*]                this.recognizer.Recognizer.Grammars.AddGrammarFromList(&quot;Number&quot;, new List<string>() { &quot;one&quot;, &quot;two&quot;, &quot;three&quot;, &quot;four&quot;, &quot;five&quot;, &quot;six&quot;, &quot;seven&quot;, &quot;eight&quot;, &quot;nine&quot;, &quot;ten&quot; });
[*]            }
[*]            catch (Exception err)
[*]            {
[*]                tbOutPut.Text = &quot;Error: &quot; + 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 = &quot;音量太小&quot;;
[*]            }
[*]      }
[*]    }
[*]}
  

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

  图10.23 系统语音识别界面
页: [1]
查看完整版本: Windows Phone 8 发音合成与语音识别