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

时间:2012-01-23 11:47:19

标签: c# .net speech-recognition voice voice-recognition

我在语音识别引擎中编写代码时遇到了麻烦。 任务是,当用户说“圆圈”时,引擎应自动在表单上绘制一个圆圈:

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

我用于语音识别的代码是......

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);

我不知道如何合并上面的代码来执行一个圆圈。任何相关的答案都会有很大的帮助!

1 个答案:

答案 0 :(得分:2)

e.Result.Text会告诉你这个人说的话。所以,如果你想在他们说"circle"时画一个圆圈:

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