Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

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

From Wiki
Jump to: navigation, search
WP Metro Icon Multimedia.png
SignpostIcon XAML 40.png
WP Metro Icon WP8.png
Article Metadata

兼容于
文章
zhenglin 在 07 Feb 2013 创建
最后由 hamishwillee 在 11 Sep 2013 编辑

Introduction

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

Summary

发音的合成

发音的合成是指把文本转化为语音由手机系统进行发音,从而实现了把文本自动转化为了更加自然化的声音。在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();
}
}

语音识别

语音识别是指让手机通过识别和理解过程把语音信号转变为相应的文本或命令。在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);//循环进行监听语音
}
}
}
This page was last modified on 11 September 2013, at 03:35.
217 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×