C#中的System.Speech命名空间初探
2024-10-20 08:31:22
本程序是口算两位数乘法,随机生成两个两位数,用语音读出来。然后开启语音识别,接受用户输入,知道答案正确关闭语音识别。用户说答案时,可以说“再说一遍”重复题目。
关键是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 = -; } } }
最新文章
- while用法一例
- BZOJ 2844 albus就是要第一个出场 ——高斯消元 线性基
- 2.4G/5G频段WLAN各国使用信道表
- 关于Interface忽然想到的
- Probabilistic Graphical Models
- hibernate.properties
- MySQL的事务
- PHP中的中文截取乱码问题_gb2312_utf-8
- 利用Linq + Jquery + Ajax 异步分页的实现
- Fatal error: Class &#39;ZipArchive&#39; not found的解决办法
- 图论(对偶图):COGS 470. [NOI2010]海拔
- ArrayList实现借壳
- [置顶] Vector ArrayList区别剖析
- 【DOS】这个命令太牛逼了
- mysql原生语句基础知识
- Server酱
- Django-rest-framework 接口实现 认证:(auth | authentication)
- bootstrap AdminLTE模版的使用
- inf
- 细解javascript中的this关键字