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

关键是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. linux运维常用命令及知识

    1.查找当前目录下所有以.tar结尾的文件然后移动到指定目录: find . -name “*.tar” -exec mv {} ./backup/ ; 查找当前目录30天以前大于100M的LOG文件 ...

  2. SqlServer时间格式化

    最近用的SqlServer比较多,时间格式化老是忘记,现整理如下:(来源于网上,具体来源地址忘记了,归根到底MSDN吧) SELECT CONVERT(varchar(50), GETDATE(), ...

  3. Linux压缩指令

    1.windows系统和Linux系统的压缩文件只有zip格式的是通用的 2.windows系统可以解压几乎所有的Linux压缩格式 3.Linux压缩指令 gzip 指令 格式: gzip 压缩的文 ...

  4. java报表工具FineReport的SQL编辑框的语法简介

    感谢大家捧场,这里继续分享关于SQL编辑框的一些语法心得总结,因为数据集定义的面板,也是FineReport报表中最常用的模块之一. 1.我理解的执行过程. 这里其实是生成一个字符串,FineRepo ...

  5. [转]C#网络编程(异步传输字符串) - Part.3

    本文转自:http://www.tracefact.net/CSharp-Programming/Network-Programming-Part3.aspx 这篇文章我们将前进一大步,使用异步的方式 ...

  6. [转]jQuery: how to get which button was clicked upon form submission?

    本文转自:http://stackoverflow.com/questions/5721724/jquery-how-to-get-which-button-was-clicked-upon-form ...

  7. 用CSS开启硬件加速来提高网站性能(转)

    翻译文章,原文地址:http://blog.teamtreehouse.com/increase-your-sites-performance-with-hardware-accelerated-cs ...

  8. ANE接入平台心得记录(安卓)

    开发环境:FlashBuilder4.7 AIR13.0 Eclipse 由于我懒得陪安卓的开发环境所以我下载了包含安卓SDK Manager的Eclipse,其实直接用FlashBuilder开发A ...

  9. java 27 - 4 反射之 通过反射获取成员变量并使用

    类Field: 提供有关类或接口的单个字段的信息,以及对它的动态访问权限. A:获得类的成员变量 数组: 1.getFields(公共类的) 2.getDeclaredFields(所有类型的) B: ...

  10. HTML 学习笔记 JavaScript(数据类型)

    字符串 数字 布尔 数组 对象 Null Undefined JavaScript 拥有动态类型 JavaScript拥有动态类型 这意味着相同的变量可用作不同的类型: 实例: var x // x ...