简体   繁体   English

如何在C#中使用语音作为命令?

[英]How to use voice as command in C#?

I am having trouble in writing a code in my speech recognition engine. 我在语音识别引擎中编写代码时遇到了麻烦。 The task is, when the user says 'circle', the engine should automatically draw a circle on the form: 任务是,当用户说'circle'时,引擎应该自动在表单上绘制一个圆圈:

if(Speech == circle)
{
    DrawCircle();
}

The code I'm using for speech recognition is... 我用于语音识别的代码是......

namespace speechexampl
{
    public partial class Form1 : Form
    {

        SpeechRecognizer rec = new SpeechRecognizer();

        public Form1()
        {

            InitializeComponent();

            rec.SpeechRecognized += rec_SpeechRecognized;

        }


        void rec_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            lblLetter.Text = e.Result.Text;
        }

        void Form1_Load(object sender, EventArgs e)
        {

             var c = new Choices();
             for (var i = 0; i <= 100; i++)

             c.Add(i.ToString());

             var gb = new GrammarBuilder(c);

             var g = new Grammar(gb);

             rec.LoadGrammar(g);

             rec.Enabled = true;

         }
     }
}

//**

//> and to draw circle or rectangle:

//**

Pen myPen2 = new Pen(System.Drawing.Color.Red, 3);
Rectangle myRectangle2 = new Rectangle(95, 130, 100, 100);
graphicsObj.DrawEllipse(myPen2, myRectangle2);

I don't know how to merge the above code to execute a circle when said so. 如果这样说,我不知道如何合并上面的代码来执行一个圆圈。 Any related answer would be a great help! 任何相关的答案都将是一个很大的帮助!

e.Result.Text will give you what the person said. e.Result.Text会告诉你这个人说的话。 So if you want to draw a circle when they say "circle" : 所以,如果你想说"circle"时画一个圆圈:

if (e.Result.Text == "circle") {
    //Draw a cricle
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM