C#中的System.Speech命名空间初探
本程序是口算两位数乘法,随机生成两个两位数,用语音读出来。然后开启语音识别,接受用户输入,知道答案正确关闭语音识别。用户说答案时,可以说“再说一遍”重复题目。
关键是GrammarBuilder和Choices的用法。
首先来看看如何获得已安装的语音识别引擎
void showInstalled()
{
Console.WriteLine("installed recognizers");
foreach (var i in SpeechRecognitionEngine.InstalledRecognizers())
{
Console.WriteLine(String.Format("{0}\t{1}\t{2}\t{3}\n", i.Id, i.Name, i.Culture, i.Description));
}
}
下面是主程序
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Speech;
using System.Speech.Recognition;
using System.Globalization;
using System.Windows.Forms;
using System.Speech.Synthesis;
public class Haha
{
static void Main()
{
new Haha();
}
SpeechRecognitionEngine recognizer = new SpeechRecognitionEngine();
int x, y, z;
SpeechSynthesizer cout = null;
Haha()
{
recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);
recognizer.SetInputToDefaultAudioDevice();
String s = ";
; i <= ; i++) s += " " + i;
GrammarBuilder num = new GrammarBuilder(new Choices(s.Split(new char[] { ' ' })));
num = , );
Choices all = new Choices();
all.Add(num);
all.Add("再说一遍");
recognizer.LoadGrammarAsync(new Grammar(all));
run();
}
void run()
{
cout = new SpeechSynthesizer();
Random random = new Random();
while (true)
{
x = random.Next(, );
y = random.Next(, );
z = -;
cout.Speak(x + "成以" + y);
recognizer.RecognizeAsync(RecognizeMode.Multiple);
while (true)
{
)
{
if (z == x * y)
{
cout.Speak("正确,真聪明");
break;
}
else
{
cout.Speak(String.Format("不是{0},再算!",z));
z = -;
}
}
}
recognizer.RecognizeAsyncStop();
}
}
void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
string text = e.Result.Text;
Console.WriteLine(text);
if (text == "再说一遍")
{
cout.Speak(x + "成以" + y);
return;
}
try
{
z = int.Parse(text);
}
catch
{
z = -;
}
}
}
C#中的System.Speech命名空间初探的更多相关文章
- System.Diagnostics命名空间里的Debug类和Trace类的用途
在 .NET 类库中有一个 System.Diagnostics 命名空间,该命名空间提供了一些与系统进程.事件日志.和性能计数器进行交互的类库.当中包括了两个对开发人员而言十分有用的类--Debug ...
- System.Speech.Synthesis 添加暂停、继续功能
为了方便调用暂停.继续的方法.要将speech的功能写成一个类.直接附上代码: using System; using System.Collections.Generic; using System ...
- 背水一战 Windows 10 (122) - 其它: 通过 Windows.System.Profile 命名空间下的类获取信息, 查找指定类或接口的所在程序集的所有子类和子接口
[源码下载] 背水一战 Windows 10 (122) - 其它: 通过 Windows.System.Profile 命名空间下的类获取信息, 查找指定类或接口的所在程序集的所有子类和子接口 作者 ...
- Web开发.net framework 类库中必须掌握的命名空间(或者类)【转】
Web开发常用命名空间和类. System.Collections //命名空间包含接口和类,这些接口和类定义各种对象(如列表.队列.位数组.哈希表和字典)的集合.System.Collections ...
- 【C#】语音识别 - System.Speech
一个有趣的东西,今后可能用得上. C#语音识别:在命名空间 System.Speech下SpeechSynthesizer可以将文字转换成语音 贴出代码: public partial class F ...
- .net framework 类库中必须掌握的命名空间(或者类)
Web开发常用命名空间和类. System.Collections //命名空间包含接口和类,这些接口和类定义各种对象(如列表.队列.位数组.哈希表和字典)的集合.System.Collections ...
- 24.2 网络编程基础——System.Net 命名空间
使用C#进行网络编程时,通常要用到: System. Net 命名空间. System. Net. Sockets 命名空间. System. Net. Mail 命名空间. 24.2.1 Sy ...
- C#中的程序集和命名空间
C#中的程序集和命名空间 如果说命名空间是类库的逻辑组织形式,那么程序集就是类库的物理组织形式.只有同时指定类型所在的命名空间及实现该类型的程序集,才能完全限定该类型.<精通.NET核心技术-- ...
- C# 特性 System.ComponentModel 命名空间属性方法大全,System.ComponentModel 命名空间的特性
目录: System.ComponentModel 特性命名空间与常用类 System.ComponentModel.DataAnnotations ComponentModel - Classes ...
随机推荐
- Windows 安装ELK
在Windows服务器上安装ELK logstash在windows平台下不能监控磁盘文件,用nxlog代替,监控文件并把内容发送到logstash 部署环境 Os :Windows 7 logsta ...
- 奇 arch/i386/kernel/head.o(.text+0x3e): undefined reference to `stack_start'
当linux/linkage.h 是dos格式保存,即以\r\n作行结束,gcc-2.96/redhat-7.3报错
- 【JAVA 小结】Java关于类与对象的代码
分别建立2个类class works 和 Person import java.io.*; public class works { public static void main(String[] ...
- ELF Format 笔记(九)—— Elf32_Sym 结构的 st_value 和 st_shndx 成员
ilocker:关注 Android 安全(新手) QQ: 2597294287 前面的笔记中提到过 Elf32_Sym 结构,本篇笔记再写一下其中的 st_value 和 st_shndx 成员. ...
- Windows Azure支持七层负载均衡--Application Gateway
一直以来Windows Azure的负载均衡(Loadbalancer)功能一直被客户诟病,无法其竞争对手(特别是国内的云厂商)匹敌. Windows Azure的负载均衡器是四层的,前期的版本不支持 ...
- 准备使用 Office 365 中国版--购买
Office 365中国版支持两种购买方式,Web Direct(在线购买)和CSP(代理商购买).如果客户的企业规模不大(几十个用户,小于100用户)或者是个人/家庭购买,可以直接选择在线购买方式. ...
- Sql动态添加字段的正确姿势
如何给指定表动态添加字段? 一.创建一张表[Tbl_AutoFileds] (tableName表名,fieldName字段名,dataType数据类型,length长度 isnull 是否允许为n ...
- Font Awesome使用指南
Font Awesome介绍 Font Awesome是一款很流行的字体图标工具.随着Bootstrap的流行而逐渐被人所认识,现在FontAwesome不仅仅可以在bt上使用,还可以应用在各种web ...
- nginx作为负载均衡服务器——测试
i. 需求 nginx作为负载均衡服务器,用户请求先到达nginx,再由nginx根据负载配置将请求转发至 tomcat服务器. nginx负载均衡服务器:192.168.101.3 tomcat1服 ...
- zkw线段树详解
转载自:http://blog.csdn.net/qq_18455665/article/details/50989113 前言 首先说说出处: 清华大学 张昆玮(zkw) - ppt <统计的 ...