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

关键是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. andriod 动态设置TextView 和 RelativeLayou 高度

    XML布局 <RelativeLayout android:id="@+id/rlay_meeting_contact_context" android:layout_wid ...

  2. Android中 int,float,Double,String 互相转换

    1 如何将字串 String 转换成整数 int?  A. 有两个方法: 1). int i = Integer.parseInt([String]); 或 i = Integer.parseInt( ...

  3. ELF Format 笔记(六)—— 字符串表

    ilocker:关注 Android 安全(新入行,0基础) QQ: 2597294287 字符串表中包含若干以 null 结尾的字符串,这些字符串通常是 symbol 或 section 的名字.当 ...

  4. IO的多路复用和信号驱动

    Linux为多路复用IO提供了较多的接口,有select(),pselect(),poll()的方式,继承自BSD和System V 两大派系. select模型比较简单,“轮询”检测fd_set的状 ...

  5. I am back-电商网站开发&jQuery

    hi 之前有将近两周的时间没有更新,除了懒就是其他的事情耽误了.现在好了,回家了,虽然家里停水,外面又有积雪,天寒地冻的,但诸多不便,都比不过有点闲的好. 开搞每个学PHP的必经之路——电商网站的开发 ...

  6. Codeforces 410C.Team[构造]

    C. Team time limit per test 1 second memory limit per test 256 megabytes input standard input output ...

  7. 使用C#向ACCESS中插入数据

    使用C#向ACCESS中插入数据   1.创建并打开一个OleDbConnection对象 string strConn = " Provider = Microsoft.Jet.OLEDB ...

  8. MIT License

    早上看到微软的UWP例子,在代码里看到 Copyright (c) Microsoft. All rights reserved.// This code is licensed under the ...

  9. [No000056]你无法真正占有一个人,包括你的爱人,先生或太太、小孩,以及你自己....

    从一出生,我们的双手就握的紧紧的,好像深知自己会失去什么 很多人迷信多子多孙才是福,老来才有依靠,但太多新闻告诉我们,很多人老了,子孙为了分家产,反而让他生不如死,死了还无法入土为安. 现实也告诉我们 ...

  10. jsp的九大内置对象和四大作用域

    定义:可以不加声明就在JSP页面脚本(Java程序片和Java表达式)中使用的成员变量? JSP共有以下9种基本内置组件(可与ASP的6种内部组件相对应):? 1.request对象(作用域)? 客户 ...