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

关键是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 = -;
        }
    }
}

最新文章

  1. while用法一例
  2. BZOJ 2844 albus就是要第一个出场 ——高斯消元 线性基
  3. 2.4G/5G频段WLAN各国使用信道表
  4. 关于Interface忽然想到的
  5. Probabilistic Graphical Models
  6. hibernate.properties
  7. MySQL的事务
  8. PHP中的中文截取乱码问题_gb2312_utf-8
  9. 利用Linq + Jquery + Ajax 异步分页的实现
  10. Fatal error: Class &#39;ZipArchive&#39; not found的解决办法
  11. 图论(对偶图):COGS 470. [NOI2010]海拔
  12. ArrayList实现借壳
  13. [置顶] Vector ArrayList区别剖析
  14. 【DOS】这个命令太牛逼了
  15. mysql原生语句基础知识
  16. Server酱
  17. Django-rest-framework 接口实现 认证:(auth | authentication)
  18. bootstrap AdminLTE模版的使用
  19. inf
  20. 细解javascript中的this关键字

热门文章

  1. Linux命令行上传文件到百度网盘
  2. windows下 MySQL手动安装与卸载
  3. Hadoop安装lzo实验
  4. nodejs模块——Event模块
  5. [转]jqGrid 属性、事件全集
  6. MVC、MVVM、MVP小结
  7. (一)观察者模式-C++实现
  8. Codeforces 461B. Appleman and Tree[树形DP 方案数]
  9. [No00001D]解决5个问题,马上开口说英语
  10. Android Fragment 你应该知道的一切