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 ...
随机推荐
- linux下怎么查看ssh的用户登录日志
linux下登录日志在下面的目录里: cd /var/log 查看ssh用户的登录日志: less secure linux日志管理: 1. 日志简介 日志对于安全来说,非常重要,他记录了系统每天发生 ...
- [转]关于信息安全认证CISP与CISSP的对比及分析
本文转自:https://www.douban.com/group/topic/89081816/ 最近好多信息安全行业或者打算转行的职场人在纠结学CISP还是学CISSP,我给大家就CISP和CIS ...
- IDEA使用(1)intellIJ idea 配置 svn
以前开发工具一直用的是Eclipse/MyEclipse,虽然早就听说过Idea而且也尝试用过几次, 说实话一开始使用idea真是很不习惯,不只是快捷键不同:比如项目和模块.服务器(如Tomcat)配 ...
- 【HTML】嵌入图像
img元素允许我们在HTML文档里嵌入图像. 要嵌入一张图像需要使用src和alt属性,代码如下: <img src="../img/example/img-map.jpg" ...
- Visual Studio 代码折叠快捷键(摘要)
代码编辑器的展开和折叠代码确实很方便和实用.以下是展开代码和折叠代码所用到的快捷键,很常用: Ctrl + M + O: 折叠所有方法 Ctrl + M + M: 折叠或者展开当前方法 Ctrl + ...
- Student学生管理系统
1.定义各个层 2.添加各个层之间的引用 DAL 层调用Model BLL层调用DAL和Model UI层调用BLL和Model层 Model层供各个层调用 3.根据数据库建立实体类,每张表对应一个实 ...
- Unity 碰撞器
Unity引擎的碰撞器 常见的有Box Collider,Capsule Collider等等 IsTrigger(触发器) 在这些碰撞器中有一个属性 Is Trigger(是否使用触发器) 触发器事 ...
- java 26 - 7 网络编程之 TCP协议代码优化
上次所写的代码中,客户端和服务器端所进行的数据传输所用的是字节流. 优化: A:这次,为了高效,对这个字节流通过转换流来进行包装,包装成高效字符流. B:这次,传输的数据是通过键盘录入的数据. 服务器 ...
- Java—FileOperator
//基本用法 JFileChooser jfc = new JFileChooser(); int result = jfc.showOpenDialog(this); if(result != JF ...
- c# HttpClient禁止缓存
using (var client = new HttpClient()) { //方法1: CacheContr ...