随着 Windows Phone 8.1 GDR1 + Cortana 中文版的发布,相信有很多用户或开发者都在调戏 Windows Phone 的语音私人助理 Cortana 吧,在世界杯的时候我亲测 Cortana 预测德国和阿根廷的比赛很准的。(题外话扯远了),可是作为开发者我们怎么将Cortana集成到应用中呢,今天我用一点时间给大家介绍一下如何使用 voice command 集成 Windows Phone 8.1 的应用。

首先要明确两个名词 Voice command & Voice Command Definition 即 VCD文件,相信做过windows Phone 8.0 开发的朋友应该有所了解,通过注册VCD文件 Windows phone 8.0 的应用当中就可以实现 voice command 的功能,如果你不了解请先读一下我之前的文章(这里我就不在过多介绍 8.0 Voice command 的重复内容了),Windows Phone 8 语音 - Speech for Windows Phone 8 快速了解一下Windows Phone 开发语音功能的前期准备工作。

简单的说在 Windows Phone 8.0 voice command 功能比较简单,主要是通过 Voice Command Name 判断预制在VCD文件中的几个命令。

在 Windows Phone 8.1 应用中 Cortana 提供了更强的自然语言识别(Natural language recognition)

当然 VCD 文件的中的 grammars  也得到了扩充,并且区别两个OS版本的

http://schemas.microsoft.com/voicecommands/1.0 for Windows Phone 8.0 Voice Command and Cortana compatible.

http://schemas.microsoft.com/voicecommands/1.1 only for Widnows Phone 8.1 Cortnan.

详细内容请参考

Windows Phone 8.0:  Voice command element and attribute reference for Windows Phone 8

Windows Phone 8.1:  Voice command elements and attributes

通过 8.0 和 8.1 VCD 文件属性支持情况来看有一个最主要的区别在8.1 VCD中支持 PhraseTopic 这个属性。

文字说的太抽象了还是贴出代码给大家说说吧:

这里我主要强调说一下 ListenFor  结点和 PhraseTopic 结点。 注意在 Listenfor 结点中的中括号 {dictatedSearchTerms} 是对应的 PhraseTopic  结点中的 Label 属性。同时我们可以把 PhraseTopic 理解成任意内容。最后都可以从Cortana回传到我们的应用当中来。

<VoiceCommands xmlns="http://schemas.microsoft.com/voicecommands/1.1">
<!-- The CommandSet Name is used to programmatically access the CommandSet -->
<CommandSet xml:lang="zh-CN" Name="chineseCommands">
<!-- The CommandPrefix provides an alternative to your full app name for invocation -->
<CommandPrefix> 微软 文档 </CommandPrefix>
<!-- The CommandSet Example appears in the global help alongside your app name -->
<Example> 搜索 构造 函数 </Example> <Command Name="MSDNSearch">
<!-- The Command example appears in the drill-down help page for your app -->
<Example> 搜索 构造 函数' </Example> <!-- ListenFor elements provide ways to say the command, including references to
{PhraseLists} and {PhraseTopics} as well as [optional] words -->
<ListenFor> 查找 {dictatedSearchTerms} </ListenFor>
<ListenFor> 搜 {dictatedSearchTerms} </ListenFor>
<ListenFor> 搜索 {dictatedSearchTerms} </ListenFor>
<ListenFor> 查 {dictatedSearchTerms} </ListenFor>
<ListenFor> 找 {dictatedSearchTerms} </ListenFor> <!--Feedback provides the displayed and spoken text when your command is triggered -->
<Feedback> 查找 MSDN... </Feedback> <!-- Navigate specifies the desired page or invocation destination for the Command-->
<Navigate Target="MainPage.xaml" />
</Command> <Command Name="MSDNNaturalLanguage">
<Example> 我 想 去 Windows 手机 开发 中心 </Example>
<ListenFor> {naturalLanguage} </ListenFor>
<Feedback> 启动 MSDN... </Feedback>
<Navigate Target="MainPage.xaml" />
</Command> <PhraseTopic Label="dictatedSearchTerms" Scenario="Search">
<Subject> MSDN </Subject>
</PhraseTopic> <PhraseTopic Label="naturalLanguage" Scenario="Natural Language">
<Subject> MSDN </Subject>
</PhraseTopic> </CommandSet>
</VoiceCommands>

了解完新的VCD文件,在这里我提醒下大家,其实在Windows Phone 8.0的应用中也可以兼容 Cortana的功能的,在8.0的应用当中我们只需要判断一下操作系统的版本然后选择不同的VCD文件向系统注册即可。

首先我们需要把两个版本的VCD文件都存放到项目中来

其次在注册VCD文件的时候根据系统版本进行一下判断即可。

        /// <summary>
/// Installs the Voice Command Definition (VCD) file associated with the application.
/// Based on OS version, installs a separate document based on version 1.0 of the schema or version 1.1.
/// </summary>
private async void InstallVoiceCommands()
{
const string wp80vcdPath = "ms-appx:///VoiceCommandDefinition_8.0.xml";
const string wp81vcdPath = "ms-appx:///VoiceCommandDefinition_8.1.xml";
const string chineseWp80vcdPath = "ms-appx:///ChineseVoiceCommandDefinition_8.0.xml";
const string chineseWp81vcdPath = "ms-appx:///ChineseVoiceCommandDefinition_8.1.xml"; try
{
bool using81orAbove = ((Environment.OSVersion.Version.Major >= )
&& (Environment.OSVersion.Version.Minor >= )); string vcdPath = using81orAbove ? wp81vcdPath : wp80vcdPath;
if (InstalledSpeechRecognizers.Default.Language.Equals("zh-CN", StringComparison.InvariantCultureIgnoreCase))
{
vcdPath = using81orAbove ? chineseWp81vcdPath : chineseWp80vcdPath;
} Uri vcdUri = new Uri(vcdPath);
await VoiceCommandService.InstallCommandSetsFromFileAsync(vcdUri);
}
catch (Exception vcdEx)
{
Dispatcher.BeginInvoke(() =>
{
MessageBox.Show(String.Format(
AppResources.VoiceCommandInstallErrorTemplate, vcdEx.HResult, vcdEx.Message));
});
}
}

最后在应用当中获取用户的语音输入方法,注意这里也是需要通过 PhraseTopic 结点的 Label 名称获取的。

        /// <summary>
/// Takes specific action for a retrieved VoiceCommand name.
/// </summary>
/// <param name="voiceCommandName"> the command name triggered to activate the application </param>
private void HandleVoiceCommand(string voiceCommandName)
{
// Voice Commands can be typed into Cortana; when this happens, "voiceCommandMode" is populated with the
// "textInput" value. In these cases, we'll want to behave a little differently by not speaking back.
bool typedVoiceCommand = (NavigationContext.QueryString.ContainsKey("commandMode")
&& (NavigationContext.QueryString["commandMode"] == "text")); string phraseTopicContents = null;
bool doSearch = false; switch (voiceCommandName)
{
case "MSDNNaturalLanguage":
if (NavigationContext.QueryString.TryGetValue("naturalLanguage", out phraseTopicContents)
&& !String.IsNullOrEmpty(phraseTopicContents))
{
// We'll try to process the input as a natural language query; if we're successful, we won't
// fall back into searching, since the query will have already been handled.
doSearch = TryHandleNlQuery(phraseTopicContents, typedVoiceCommand);
}
break;
case "MSDNSearch":
// The user explicitly asked to search, so we'll attempt to retrieve the query.
NavigationContext.QueryString.TryGetValue("dictatedSearchTerms", out phraseTopicContents);
doSearch = true;
break;
} if (doSearch)
{
HandleSearchQuery(phraseTopicContents, typedVoiceCommand);
}
}

整个过程就这么简单,心动不如行动,赶快把你的应用加入Cortana 功能让小伙伴儿们调戏一番。

更多参考资料:

Quickstart: Voice commands (XAML)

Speech for Windows Phone 8

快速入门:语音命令 (XAML)

源码下载:

MSDN Voice Search for Windows Phone 8.1

如何将 Cortana 与 Windows Phone 8.1 应用集成 ( Voice command - Natural language recognition )的更多相关文章

  1. Windows phone 8.1应用集成cortana语音命令

    微软推出小娜已经有一段时间了,最近恰好在研究其用法,就随便写点记录一下自己的心得. 在研究时参考了@王博_Nick的博客:http://www.cnblogs.com/sonic1abc/p/3868 ...

  2. WAMP Server助你在Windows上快速搭建PHP集成环境

    WAMP Server助你在Windows上快速搭建PHP集成环境 原文地址 我想只要爬过几天网的同学都会知道PHP吧,异次元的新版本就是基于PHP的WordPress程序制造出来的,还有国内绝大部分 ...

  3. 与众不同 windows phone (16) - Media(媒体)之编辑图片, 保存图片到相册, 与图片的上下文菜单“应用程序...”和“共享...”关联, 与 Windows Phone 的图片中心集成

    原文:与众不同 windows phone (16) - Media(媒体)之编辑图片, 保存图片到相册, 与图片的上下文菜单"应用程序..."和"共享..." ...

  4. Windows系统的Jenkins持续集成环境

    Windows系统的Jenkins持续集成环境 如题:本文将介绍如何在Windows环境下运用Jenkins部署持续集成环境.之所以写本文,是因为在最近工作当中,学习使用Jenkins时,确实遇到了一 ...

  5. Jenkins在Windows系统dotnet平台持续集成

            之前写过一篇文章是在CentOS上构建.net自动化编译环境, 今天这篇是针对于Windows平台的环境.        Jenkins是一个开源软件项目,旨在提供一个开放易用的软件平 ...

  6. 一步步部署基于Windows系统的Jenkins持续集成环境

    如题:本文将介绍如何在Windows环境下运用Jenkins部署持续集成环境.之所以写本文,是因为在最近工作当中,学习使用Jenkins时,确实遇到了一些问题,而大多数教程文档都是基于Mac或是Lin ...

  7. windows linux—unix 跨平台通信集成控制系统----系统硬件信息获取

    控制集成系统需要了解系统的各项硬件信息,之前我们设计的时候,习惯使用c函数来搞,后来可能发现程序的移植性收到了一些影响,比如unix内核的一些c函数在linux下面是没有的: 比如 苹果达尔文内核的如 ...

  8. windows linux—unix 跨平台通信集成控制系统----文件搜索

    跨平台的网络通信,跟设备的集成控制,牵扯到在各种平台下的文件搜索问题,windows下面的已经有了. 地址如下: http://blog.csdn.net/wangyaninglm/article/d ...

  9. windows linux—unix 跨平台通信集成控制系统

    首先,我们可以用到这个开源的开发包: mdk(Micro-Development-Kit)微量级软件开发包,提供几个常用类,主要实现了一个高性能的并发服务器引擎 使用c++开发,是一个跨平台的开发包, ...

随机推荐

  1. 冲刺阶段 day2

    day2 项目进展 今天本组五位同学聚在一起将项目启动,首先我们对项目进行了规划,分工,明确指出每个人负责哪些项目.由负责第一部分的组员开始编程,在已经搭建好的窗体内,对系部设置进行了编写,本校共六个 ...

  2. C++ Primer 变量和基本类型

    <C++ Primer 4th>读书摘要 基本上所有的语言都要提供下列特征: • 内置数据类型,如整型.字符型等. • 表达式和语句:表达式和语句用于操纵上述类型的值. • 变量:程序员可 ...

  3. Beta分布和Dirichlet分布

    在<Gamma函数是如何被发现的?>里证明了\begin{align*} B(m, n) = \int_0^1 x^{m-1} (1-x)^{n-1} \text{d} x = \frac ...

  4. Atitit. C# java 的api 目录封装结构映射总结

    Atitit. C#  java 的api 目录封装结构映射总结 C# java ref System.Reflection System.Type, java.lang.ref concurrent ...

  5. Leetcode 303 Range Sum Query - Immutable

    题意:查询一个数组在(i,j]范围内的元素的和. 思路非常简单,做个预处理,打个表就好 拓展:可以使用树状数组来完成该统计,算法复杂度为(logn),该数据结构强力的地方是实现简单,而且能完成实时更新 ...

  6. HTML常用命名和CSS reset代码【收集总结】

    CSS命名规则 头:header 内容:content/containe 尾:footer 导航:nav 侧栏:sidebar 栏目:column 页面外围控制整体布局宽度:wrapper 左右中:l ...

  7. 阿里云产品介绍(三):云数据库RDS

    写完云服务器ECS,本来想先写负载均衡的. 因为发现很多客户,都是直接将单台云服务器应用对外提供访问,如果云服务器宕机,应用就会停止服务.云服务器标称有99.95%的可用率,一年下来宕机四个多小时也是 ...

  8. Entity Framework一对多关系添加数据的两种方式

    当使用Entity Framework添加一对多关系数据的时候,通常先添加一的数据,然后再添加多的数据.类似这样: //添加一的数据 var category = new Category{Name= ...

  9. openCV_java 图像二值化

    较为常用的图像二值化方法有:1)全局固定阈值:2)局部自适应阈值:3)OTSU等. 局部自适应阈值则是根据像素的邻域块的像素值分布来确定该像素位置上的二值化阈值.这样做的好处在于每个像素位置处的二值化 ...

  10. eclipse快捷键调试总结【转】

    http://www.cnblogs.com/yxnchinahlj/archive/2012/02/22/2363542.html (1)Ctrl+M --切换窗口的大小(2)Ctrl+Q --跳到 ...