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;
            }
        }
    }
  • 相关阅读:
    matlab colormap
    张量的基本概念
    河南省测绘资质单位大全
    Meanshift算法
    图形图像的绘制 GandyDraw
    leetcode
    Java 实现装饰(Decorator)模式
    Python
    Asp.Net+Easyui实现重大CRUD
    Scriptcase演示程序,现在,他们使用SC多么简单的开发系统
  • 原文地址:https://www.cnblogs.com/weiyinfu/p/5191945.html
Copyright © 2011-2022 走看看