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

关键是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. 四、Android学习第四天——JAVA基础回顾(转)

    (转自:http://wenku.baidu.com/view/af39b3164431b90d6c85c72f.html) 四.Android学习第四天——JAVA基础回顾 这才学习Android的 ...

  2. Python 变量类型

    Python 变量类型 变量存储在内存中的值.这就意味着在创建变量时会在内存中开辟一个空间. 基于变量的数据类型,解释器会分配指定内存,并决定什么数据可以被存储在内存中. 因此,变量可以指定不同的数据 ...

  3. Install pyodbc in OpenSUSE

    Install pyodbc in OpenSUSE: Install unixODBC-2.3.2 ./configure --prefix=/usr/local/unixODBC --enable ...

  4. Network Instructions in Linux

    I will gradually learn some instructions about Network in Linux. First, here are somethings about IP ...

  5. Application中的路径

    前提条件 项目工程目录:E:/Work/cosmosbox/cb-client/ 我电脑当前的用户名:qingqing PersistentDataPath Application.persisten ...

  6. SQL/LINQ/Lamda

    SQL LINQ Lambda SELECT * FROM HumanResources.Employee from e in Employees select e Employees   .Sele ...

  7. oracle数据库误操作把表删除了,怎样恢复

    一:表的恢复 对误删的表,只要没有使用PURGE永久删除选项,那么从flash back区恢复回来希望是挺大的.一般步骤有:1.从flash back里查询被删除的表 select * from re ...

  8. <转>SQL语句大全

    本文为转载,原文地址:http://www.cnblogs.com/cangqiongbingchen/p/4530333.html 一.基  础 1.说明:创建数据库CREATE DATABASE ...

  9. java.sql.preparedstatement和java.sql.statement的区别

    本文转自CSDN,然后整理了一遍.原文出处:CSDN JDBC(java database connectivity,java数据库连接)的api中的主要的四个类之一的java.sql.stateme ...

  10. Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...