Xml code

--------------------------------

<Page

x:Class="MyApp.MainPage"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

xmlns:local="using:MyApp"

xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

mc:Ignorable="d"

Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

<StackPanel>

<TextBlock Text="请说出你最喜欢的体育运动:" FontSize="18"/>

<Button Content="开始识别" Width="200" Click="OnClick"/>

<ListBox Name="lb">

<x:String>足球</x:String>

<x:String>排球</x:String>

<x:String>跑步</x:String>

<x:String>羽毛球</x:String>

<x:String>篮球</x:String>

</ListBox>

</StackPanel>

</Page>

C# code

------------------------

public sealed partial class MainPage : Page

{

SpeechRecognizer _recognizer = null;

public MainPage()

{

this.InitializeComponent();

this.NavigationCacheMode = NavigationCacheMode.Required;

this.Loaded += Page_Loaded;

this.Unloaded += Page_Unloaded;

}

private void Page_Unloaded(object sender, RoutedEventArgs e)

{

// 释放资源

_recognizer.Dispose();

}

private async void Page_Loaded(object sender, RoutedEventArgs e)

{

_recognizer = new SpeechRecognizer();

// 创建自定义短语约束

string[] array = { "足球", "排球", "跑步", "羽毛球", "篮球" };

SpeechRecognitionListConstraint listConstraint = new SpeechRecognitionListConstraint(array);

// 添加约束实例到集合中

_recognizer.Constraints.Add(listConstraint);

// 编译约束

await _recognizer.CompileConstraintsAsync();

}

private async void OnClick(object sender, RoutedEventArgs e)

{

Button btn = sender as Button;

btn.IsEnabled = false;

try

{

SpeechRecognitionResult res = await _recognizer.RecognizeAsync();

if (res.Status == SpeechRecognitionResultStatus.Success)

{

// 处理识别结果

this.lb.SelectedItem = res.Text;

}

}

catch { /* 忽略异常 */ }

btn.IsEnabled = true;

}

}

xml 文件

语音识别

public sealed partial class App : Application

{

/// <summary>

/// Initializes the singleton application object.  This is the first line of authored code

/// executed, and as such is the logical equivalent of main() or WinMain().

/// </summary>

public App()

{

this.InitializeComponent();

this.Suspending += this.OnSuspending;

}

/// <summary>

/// Invoked when the application is launched normally by the end user.  Other entry points

/// will be used when the application is launched to open a specific file, to display

/// search results, and so forth.

/// </summary>

/// <param name="e">Details about the launch request and process.</param>

protected async override void OnLaunched(LaunchActivatedEventArgs e)

{

StorageFile vcdFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///vcd.xml"));

// 安装VCD文件

await VoiceCommandDefinitionManager.InstallCommandDefinitionsFromStorageFileAsync(vcdFile);

Frame rootFrame = Window.Current.Content as Frame;

// Do not repeat app initialization when the Window already has content,

// just ensure that the window is active

if (rootFrame == null)

{

// Create a Frame to act as the navigation context and navigate to the first page

rootFrame = new Frame();

// TODO: change this value to a cache size that is appropriate for your application

rootFrame.CacheSize = 1;

// Set the default language

rootFrame.Language = "zh-CN";

if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)

{

// TODO: Load state from previously suspended application

}

// Place the frame in the current Window

Window.Current.Content = rootFrame;

}

if (rootFrame.Content == null)

{

// When the navigation stack isn't restored navigate to the first page,

// configuring the new page by passing required information as a navigation

// parameter

if (!rootFrame.Navigate(typeof(MainPage), e.Arguments))

{

throw new Exception("Failed to create initial page");

}

}

// Ensure the current window is active

Window.Current.Activate();

}

/// <summary>

/// Invoked when application execution is being suspended.  Application state is saved

/// without knowing whether the application will be terminated or resumed with the contents

/// of memory still intact.

/// </summary>

/// <param name="sender">The source of the suspend request.</param>

/// <param name="e">Details about the suspend request.</param>

private void OnSuspending(object sender, SuspendingEventArgs e)

{

var deferral = e.SuspendingOperation.GetDeferral();

// TODO: Save application state and stop any background activity

deferral.Complete();

}

protected override void OnActivated(IActivatedEventArgs args)

{

if (args.Kind == ActivationKind.VoiceCommand)

{

VoiceCommandActivatedEventArgs varg = (VoiceCommandActivatedEventArgs)args;

// 处理识别结果

SpeechRecognitionResult res = varg.Result;

// 获取已识别的指令名字

string cmdName = res.RulePath[0];

if (cmdName == "open")

{

// 获取PhraseList中被识别出来的项

var interpretation = res.SemanticInterpretation;

if (interpretation != null)

{

// 通过PhraseList的Label属性可以查询出被识别的Item

string item = interpretation.Properties["pages"].FirstOrDefault();

if (!string.IsNullOrEmpty(item))

{

// 导航到对应页面

Frame root = Window.Current.Content as Frame;

if (root == null)

{

root = new Frame();

Window.Current.Content = root;

}

switch (item)

{

case "我的音乐":

root.Navigate(typeof(MyMusicPage));

break;

case "我的视频":

root.Navigate(typeof(MyVedioPage));

break;

case "我的照片":

root.Navigate(typeof(MyPhotoPage));

break;

case "主页":

root.Navigate(typeof(MainPage));

break;

default:

root.Navigate(typeof(MainPage));

break;

}

}

}

}

}

Window.Current.Activate();

}

}

识别的xml 文件

<?xml version="1.0" encoding="utf-8"?>

<VoiceCommands xmlns="http://schemas.microsoft.com/voicecommands/1.1">

<CommandSet xml:lang="zh-cn">

<CommandPrefix>测试应用</CommandPrefix>

<Example>“打开 主页”或“打开 我的音乐”或“我的音乐”或“打开我的视频”或“我的视频”……</Example>

<Command Name="open">

<Example>“打开 我的音乐”或“我的音乐”</Example>

<ListenFor>[打开]{pages}</ListenFor>

<Feedback>好的,正在努力打开中……</Feedback>

<Navigate/>

</Command>

<PhraseList Label="pages">

<Item>主页</Item>

<Item>我的音乐</Item>

<Item>我的视频</Item>

<Item>我的照片</Item>

</PhraseList>

</CommandSet>

</VoiceCommands>

uwp 语音指令的更多相关文章

  1. Win10/UWP开发—使用Cortana语音指令与App的前台交互

    Win10开发中最具有系统特色的功能点绝对少不了集成Cortana语音指令,其实Cortana语音指令在以前的wp8/8.1时就已经存在了,发展到了Win10,Cortana最明显的进步就是开始支持调 ...

  2. Win10/UWP开发—使用Cortana语音指令启动前台App

    这两天进群(53078485)找大咖的童鞋比较多,只是大咖比较忙,目前Demo还没有要到,这里先给大家转载一篇Aran大咖的博客学习下,以下是原文: Win10开发中最具有系统特色的功能点绝对少不了集 ...

  3. WP中的语音识别(下):语音指令

    除了系统集成的可以用于搜索.启动应用程序等语音命令外,在我们的应用程序内部还能自己定义语音指令,使得我们的APP能与语音操控结合得更加完全. 语音指令是通过一个XML文件来定义的.比如,咱小舅子开了家 ...

  4. anki vector robot入门语音指令大全

    vector机器人功能不断完善. 一:刚开始支持一些基础指令,你跟他说话他能在本机识别,然后做出相应的响应.在说这部分指令之前,需要加上Hey Vector.(嘿,维课的),然后他会准备听取你的指令, ...

  5. Win10/UWP开发—使用Cortana语音与App后台Service交互

    上篇文章中我们介绍了使用Cortana调用前台App,不熟悉的移步到:Win10/UWP开发—使用Cortana语音指令与App的前台交互,这篇我们讲讲如何使用Cortana调用App的后台任务,相比 ...

  6. 在UWP应用中加入Cortana语音指令集

    本文介绍小娜语音指令集的使用场景,如何将UWP应用接入小娜的语音指令集,使用户直接通过小娜启动应用并使用应用中 一些轻量级的功能.文中以必应词典作为实例讲解必应词典UWP版本是如何接入小娜语音功能的. ...

  7. HoloLens开发手记 - 语音输入 Voice input

    语音是HoloLens三大重要输入形式之一.它允许你直接通过语言控制全息图像,而不用借助手势.你只要凝视全息图像然后说出语音命令即可.语音输入是自然的交互方式,它能够很好的改善复杂的交互,因为通过一条 ...

  8. 3D语音天气球(源码分享)——完结篇

    转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! 开篇废话: 由于这篇文章是本系列最后一篇,有必要进行简单的回顾和思路整理. 这个程序是由两 ...

  9. 微软Hololens学院教程-Hologram 212-Voice(语音)【微软教程已经更新,本文是老版本】

    这是老版本的教程,为了不耽误大家的时间,请直接看原文,本文仅供参考哦!原文链接:https://developer.microsoft.com/EN-US/WINDOWS/HOLOGRAPHIC/ho ...

随机推荐

  1. 求数组的子数组之和的最大值II

    这次在求数组的子数组之和的最大值的条件下又增加了新的约束:  1.要求数组从文件读取.      2.如果输入的数组很大,  并且有很多大的数字,  就会产生比较大的结果 (考虑一下数的溢出), 请保 ...

  2. 常见最基础的Dos命令.

    打开cmd的方式. 1.+系统+命令提示符 2.Win+R 输入cmd 打开命令台 (推荐使用) 3.在任意的文件夹下按住SHIFT 加鼠标右键 在此处打开命令行窗口 4.资源管理器的地址栏前面加上 ...

  3. 软件开发(js+java开发)的启发

    发现了个很重要的意义 1,一个对象,既包含被监听的参数,也包括监听处理本身 2,基于1的开发模式 3,在函数中定义监听器 4,1)高内聚: 统一面向对象,一个功能一个对象 不同对象不互相调用,不互相引 ...

  4. Python高阶之多线程锁机制

    '''1.多进程的优势:为了同步完成多项任务,通过提高资源使用效率来提高系统的效率.2.查看线程数:threading.enumerate()函数便可以看到当前线程的数量.3.查看当前线程的名字:th ...

  5. [c++] 面向对象课程(二)-- 带指针类的设计

    class with pointer menbers string_test.cpp 1 #include "string.h" 2 #include <iostream&g ...

  6. 使用Maven打包可运行jar和javaagent.jar的区别

    简介 javaagent 是 Java1.5 之后引入的新特性,其主要作用是在class被加载之前对其拦截,以插入我们的字节码. java1.5 之前使用的是JVMTI(jvm tool interf ...

  7. At 、Crontabl定时任务

    之前笔者是在本地写的博客,然后用 windows 定时任务启动写的脚本上传到 Github 上,现在又遇到了 Linux 上的定时任务,项目还要用到 Quartz 定时任务框架 1. 一次性定时任务 ...

  8. tomcat与springmvc 结合 之---第16篇 servlet如何解析成员变量和DispatcherServlet如何解析

    writedby 张艳涛,用了两个星期将深入刨析tomcat看完了,那么接下来该看什么呢?真是不知道,知识这东西上一个月看的jvm,锁.多线程并发 又都忘了.... tomcat学完,我打算看spri ...

  9. SpringBoot整合Guacamole教程

    前言 本文主要介绍的是SpringBoot如何整合Guacamole在浏览器是远程桌面的访问. Guacamole 介绍 Apache Guacamole 是一个无客户端远程桌面网关.它支持标准协议, ...

  10. 面试官:展开说说,Spring中Bean对象是如何通过注解注入的?

    作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 章节目录(手写Spring,让你了解更多) [x] 第 01 章:开篇介绍,我要带你撸 Spr ...