zoukankan      html  css  js  c++  java
  • C#中的System.Speech命名空间初探

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

    关键是GrammarBuilder和Choices的用法。

    首先来看看如何获得已安装的语音识别引擎

    void showInstalled()
        {
            Console.WriteLine("installed recognizers");
            foreach (var i in SpeechRecognitionEngine.InstalledRecognizers())
            {
                Console.WriteLine(String.Format("{0}	{1}	{2}	{3}
    ", 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 = "0";
            for (int i = 1; i <= 9; i++) s += " " + i;
            GrammarBuilder num = new GrammarBuilder(new Choices(s.Split(new char[] { ' ' })));
            num = new GrammarBuilder(num, 1, 4);
            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(11, 99);
                y = random.Next(11, 99);
                z = -1;
                cout.Speak(x + "成以" + y);
                recognizer.RecognizeAsync(RecognizeMode.Multiple);
                while (true)
                {
                    if (z != -1)
                    {
                        if (z == x * y)
                        {
                            cout.Speak("正确,真聪明");
                            break;
                        }
                        else
                        {
                            cout.Speak(String.Format("不是{0},再算!",z));
                            z = -1;
                        }
                    }
                }
                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;
            }
        }
    }
  • 相关阅读:
    【Element UI】el-tooltip组件(提示消息) 换行
    复合文件CFB的存储结构及格式解析
    luogu P3801 红色的幻想乡 |容斥+树状数组
    luogu P3602 Koishi Loves Segments |堆+离散化贪心
    luogu P2048 [NOI2010] 超级钢琴 |堆+RMQ
    钉钉机器人使用注意事项
    k8s-部署
    docker 总结
    Navicat15最新版本破解 亲测可用!!!(Navicat Premium 注册出现 No All Pattern Found! File Already Patched)
    继续开始web的学习
  • 原文地址:https://www.cnblogs.com/weiyinfu/p/5191945.html
Copyright © 2011-2022 走看看