我无法理解为什么这段代码不起作用。 (C#)

时间:2012-05-17 13:02:13

标签: c# mouse voice speech

我是C#的新手并且要学习,我正在观看并试图通过网络进行例证。

我看到了这个例子:

using System;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Windows.Forms;

using System.Speech.Recognition;

namespace MouseController
{
    public partial class Form1 : Form
    {
        SpeechRecognitionEngine recognitionEngine; 

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        Initialize();
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        recognitionEngine.RecognizeAsyncStop();
    }

    private void Initialize()
    {
        recognitionEngine = new SpeechRecognitionEngine();
        recognitionEngine.SetInputToDefaultAudioDevice();
        recognitionEngine.SpeechRecognized += (s, args) =>
        {
            string line = "";
            foreach (RecognizedWordUnit word in args.Result.Words)
            {
                if (word.Confidence > 0.5f)
                    line += word.Text + " ";
            }

            string command = line.Trim();

            switch (command)
            {
                case "left":
                    MoveMouse(Cursor.Position.X - 50, Cursor.Position.Y);
                    break;
                case "right":
                    MoveMouse(Cursor.Position.X + 50, Cursor.Position.Y);
                    break;
                case "up":
                    MoveMouse(Cursor.Position.X, Cursor.Position.Y - 50);
                    break;
                case "down":
                    MoveMouse(Cursor.Position.X, Cursor.Position.Y + 50);
                    break;
            }

            txtOutput.Text += line;
            txtOutput.Text += Environment.NewLine;
        };

        recognitionEngine.UnloadAllGrammars();
        recognitionEngine.LoadGrammar(CreateGrammars());
        recognitionEngine.RecognizeAsync(RecognizeMode.Multiple);
    }

    private Grammar CreateGrammars()
    {
        Choices commandChoices = new Choices("left", "right", "up", "down");
        GrammarBuilder grammarBuilder = new GrammarBuilder();
        grammarBuilder.Append(commandChoices);
        return new Grammar(grammarBuilder);
    }

    private void MoveMouse(int x, int y)
    {
        this.Cursor = new Cursor(Cursor.Current.Handle);
        Cursor.Position = new Point(x, y);
        Cursor.Clip = new Rectangle(this.Location, this.Size);
    }
}
}

但是当我说“向上”,“向下”,“向左”或“向右”时没有任何反应......

我也试过这个:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Speech.Recognition;

namespace SpeechRecognitionExample
{
    public partial class Form1 : Form
    {
        private SpeechRecognitionEngine recognitionEngine;

        public Form1()
        {
            InitializeComponent();

            recognitionEngine = new SpeechRecognitionEngine();
            recognitionEngine.SetInputToDefaultAudioDevice();
            recognitionEngine.SpeechRecognized += (s, args) =>
            {
                foreach (RecognizedWordUnit word in args.Result.Words)
                {
                    if (word.Confidence > 0.8f)
                        txtOutput.Text += word.Text + " ";
                }
                txtOutput.Text += Environment.NewLine;
            };
            recognitionEngine.LoadGrammar(new DictationGrammar());
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            recognitionEngine.RecognizeAsync(RecognizeMode.Multiple);
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            recognitionEngine.RecognizeAsyncStop();
        }
    }
}

它不起作用......

在这两个代码中,我都没有从Visual Studio中收到任何错误。

为什么不起作用?

我检查了我的麦克风,它正在工作。

1 个答案:

答案 0 :(得分:0)

Initialize()的开头设置断点,并在lambda语句中设置string line = "";。逐步执行每一行并观察变量的值。如果你没有对这些断点进行任何点击,那么引擎设置或输入设备有问题,我猜。

另外,您注意到您在line上连接多个单词,然后在switch语句中查找单个单词。如果您收到多个单词,例如“up up”,则会与您的case条件无关。

让我们知道您的发现,我们可以帮助您。