视频 : http://v.youku.com/v_show/id_XNDY5NzgxMjg4.html
Windows Phone 8
具有相当优秀的语音识别能力,我们不仅可以轻易的让我们自己撰写的App,透过语音来启动,甚至也可以在App当中利用语音识别功能来实作出一些效果。
接着我们就要来看,如何让我们自己撰写的App,也支持语音识别功能。首先来看最基本的,我们要让用户可以透过语音来开启我们的App。
实作的方式很简单,首先,请先设定好你的App的WMAppManifest.xml,主要的是App名称以及Capabilities中的Speech_Recognition:
设定好了之后,请在你的App中加入一个 XML档案(SupportedVoiceCommands.xml),格式如下:
- <voicecommands xmlns="http://schemas.microsoft.com/voicecommands/1.0">
- <commandset xml:lang="zh-tw">
- <commandprefix> 我的語音程式 </commandprefix>
- <example> 試試看說:開啟 我的語音程式 </example>
- <command name="測試"></command>
- <example>跳到測試頁</example>
- <listenfor>跳到測試頁</listenfor>
- <feedback>我的語音程式 顯示測試頁</feedback>
- <navigate target="Page1.xaml">
- </navigate></commandset>
- </voicecommands>
<voicecommands xmlns="http://schemas.microsoft.com/voicecommands/1.0">
<commandset xml:lang="zh-tw">
<commandprefix> 我的語音程式 </commandprefix>
<example> 試試看說:開啟 我的語音程式 </example>
<command name="測試"></command>
<example>跳到測試頁</example>
<listenfor>跳到測試頁</listenfor>
<feedback>我的語音程式 顯示測試頁</feedback>
<navigate target="Page1.xaml">
</navigate></commandset>
</voicecommands>
上面这段XML主要在描述我们这支App要支持那些语音识别功能。
接着,再到App.xaml.cs当中,加入底下这段程序代码:
- async private static void InitializeVoiceCommands()
- {
- var filename = "SupportedVoiceCommands.xml";
- try
- {
- var location = Package.Current.InstalledLocation.Path;
- var fileUriString = String.Format("file://{0}/{1}", location, filename);
- await VoiceCommandService.InstallCommandSetsFromFileAsync
- (new Uri(fileUriString));
- }
- catch (Exception ex)
- {
- System.Diagnostics.Debug.WriteLine(ex.Message);
- }
- }
async private static void InitializeVoiceCommands()
{
var filename = "SupportedVoiceCommands.xml";
try
{
var location = Package.Current.InstalledLocation.Path;
var fileUriString = String.Format("file://{0}/{1}", location, filename);
await VoiceCommandService.InstallCommandSetsFromFileAsync
(new Uri(fileUriString));
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
}
上面这段程序代码,是让我们所撰写的语音指令XML档案,透过VoiceCommandService进行注册。 当然,在App.xaml.cs的public App()当中,我们要补上一句:
- public App()
- {
- ...略...
- InitializeVoiceCommands();
- }