本程序是口算两位数乘法,随机生成两个两位数,用语音读出来。然后开启语音识别,接受用户输入,知道答案正确关闭语音识别。用户说答案时,可以说“再说一遍”重复题目。

关键是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命名空间初探的更多相关文章

  1. System.Diagnostics命名空间里的Debug类和Trace类的用途

    在 .NET 类库中有一个 System.Diagnostics 命名空间,该命名空间提供了一些与系统进程.事件日志.和性能计数器进行交互的类库.当中包括了两个对开发人员而言十分有用的类--Debug ...

  2. System.Speech.Synthesis 添加暂停、继续功能

    为了方便调用暂停.继续的方法.要将speech的功能写成一个类.直接附上代码: using System; using System.Collections.Generic; using System ...

  3. 背水一战 Windows 10 (122) - 其它: 通过 Windows.System.Profile 命名空间下的类获取信息, 查找指定类或接口的所在程序集的所有子类和子接口

    [源码下载] 背水一战 Windows 10 (122) - 其它: 通过 Windows.System.Profile 命名空间下的类获取信息, 查找指定类或接口的所在程序集的所有子类和子接口 作者 ...

  4. Web开发.net framework 类库中必须掌握的命名空间(或者类)【转】

    Web开发常用命名空间和类. System.Collections //命名空间包含接口和类,这些接口和类定义各种对象(如列表.队列.位数组.哈希表和字典)的集合.System.Collections ...

  5. 【C#】语音识别 - System.Speech

    一个有趣的东西,今后可能用得上. C#语音识别:在命名空间 System.Speech下SpeechSynthesizer可以将文字转换成语音 贴出代码: public partial class F ...

  6. .net framework 类库中必须掌握的命名空间(或者类)

    Web开发常用命名空间和类. System.Collections //命名空间包含接口和类,这些接口和类定义各种对象(如列表.队列.位数组.哈希表和字典)的集合.System.Collections ...

  7. 24.2 网络编程基础——System.Net 命名空间

    使用C#进行网络编程时,通常要用到: System. Net  命名空间. System. Net. Sockets  命名空间. System. Net. Mail  命名空间. 24.2.1 Sy ...

  8. C#中的程序集和命名空间

    C#中的程序集和命名空间 如果说命名空间是类库的逻辑组织形式,那么程序集就是类库的物理组织形式.只有同时指定类型所在的命名空间及实现该类型的程序集,才能完全限定该类型.<精通.NET核心技术-- ...

  9. C# 特性 System.ComponentModel 命名空间属性方法大全,System.ComponentModel 命名空间的特性

    目录: System.ComponentModel 特性命名空间与常用类 System.ComponentModel.DataAnnotations ComponentModel - Classes ...

随机推荐

  1. 使用.Net自带的GZipStream进行流压缩与解压

    using System.IO; using System.IO.Compression; using System.Text; namespace CS.Utility { /// <summ ...

  2. 解决Python2.7的UnicodeEncodeError: 'ascii' codec can’t encode异常错误

    import sys reload(sys) sys.setdefaultencoding('utf-8') 好了,通过上面短短的三行,我们算是很好的解决了这个问题了,同样的方式也可以应用到Unico ...

  3. Ezchip Tilera Tile-Mx100: Der 100-ARM-Netzwerkprozessor

    Ezchip Tilera Tile-Mx100: Der 100-ARM-Netzwerkprozessor ARM-Kerne statt VLIW-Einheiten: Tileras neue ...

  4. canvas作为背景

    比如canvas的id是HB, 画好后执行document.body.style.background = "url('"+HB.toDataURL()+"')" ...

  5. C语言中链表任意位置怎么插入数据?然后写入文件中?

    链表插入示意图:(图是个人所画)因为链表指针指来指去,难以理解,所以辅助画图更加方便. 插入某个学号后面图: 定义的结构体: struct student { ]; //学生学号 ]; //学生姓名 ...

  6. gFTP的安装与使用

    gFTP是X Window下的一个用Gtk开发的多线程FTP客户端工具,它与Microsoft Windows下运行的CuteFTP等FTP工具极为类似.本文介绍了gFTP的编译.安装.启动及使用方法 ...

  7. 上一周,小白的我试着搭建了两个个人博客:在github和openshift上

    上一周,突发奇想,想搭建个自己的博客. 由于是突发奇想,自然想先找免费的试试手.仔细搜索下,选定了目标Openshift和Github. Openshift 安装WordPress OpenShift ...

  8. C 结构体位域

    位域 : 有些信息在存储时,并不需要占用一个完整的字节, 而只需占几个或一个二进制位.例如在存放一个开关量时,只有0和1 两种状态, 用一位二进位即可.为了节省存储空间,并使处理简便,C语言又提供了一 ...

  9. 中国版的 Office 365

    与Windows Azure一样,中国版的Office 365也是由世纪互联运营的——与国际版完全隔离的定制版.而言,从功能方面来看,中国版的Office 365并没有损失太多功能,并且其更新速度也基 ...

  10. [转]html5 js 访问 sqlite 数据库的操作类

    本文转自:http://blog.csdn.net/tsxw24/article/details/7613815 webkit 核心的浏览器提供了 3个 api接口,用于访问本地sqlite数据,但使 ...