Windows Phone 8 发音合成与语音识别
《深入浅出Windows Phone 8应用开发》之发音合成与语音识别Windows Phone从一开始就具有了强大的语音功能,我们可以长按开始键就可以调用手机的语音识别界面,然后可以通过语音来进行启动一些任务。那么在Windows Phone 8里面,语音控制的编程接口都开放了相关的API给应用程序调用,所以在应用程序里面也一样可以实现语音的控制。
发音的合成
发音的合成是指把文本转化为语音由手机系统进行发音,从而实现了把文本自动转化为了更加自然化的声音。在Windows Phone 8里面可以使用SpeechSynthesizer类来实现发音合成的功能,通过SpeakTextAsync方法可以直接文本转化为声音并且播放。
下面给出发音合成的示例:使用发音合成对文本的内容进行发音。
代码清单:发音合成
MainPage.xaml文件主要代码
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文件主要代码
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 recoOperation; //语音识别操作任务
bool recoEnabled = false; //判断是否停止监听
public MainPage()
{
InitializeComponent();
try
{
//创建一个语音识别类
this.recognizer = new SpeechRecognizer();
// 添加监听的单词列表
this.recognizer.Grammars.AddGrammarFromList("Number", new List() { "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文件主要代码
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() { "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 系统语音识别界面
页:
[1]