设为首页 收藏本站
查看: 1775|回复: 0

[经验分享] Windows Phone 8 发音合成与语音识别

[复制链接]

尚未签到

发表于 2018-6-16 06:07:23 | 显示全部楼层 |阅读模式
  《深入浅出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所示。
DSC0000.png

  图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>
  •         <TextBlock  Text=&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所示。
DSC0001.png

  图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>
  •         <TextBlock  Text=&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所示。
DSC0002.png

  图10.23 系统语音识别界面

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-524329-1-1.html 上篇帖子: Windows XP所有系统DLL、EXE、SYS文件 下篇帖子: windows下比较好的远程管理软件
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表