0

我使用语音识别创建了一个 C# 项目,其中我有一个具有下一个和最后一个按钮的表单我想要做的是,当我说下一步时,按钮将带我到下一个文件,或者如果我说回来,它将转到以前的文件。但是在调试项目时,它只会向我展示我所说的而不是做的。有谁知道我该如何修复它?

这是我制作的代码:

    private void Form1_Load(object sender, EventArgs e)
    {
        SpeechRecognizer recognizer = new SpeechRecognizer();
        Choices command = new Choices();
        command.Add(new string[] { "next", "last", "first" });

        GrammarBuilder gb = new GrammarBuilder();
        gb.Append(command);

        Grammar g = new Grammar(gb);
        recognizer.LoadGrammar(g);

        recognizer.SpeechRecognized +=
              new EventHandler<SpeechRecognizedEventArgs>(sre_SpeechRecognized); 
    }


    void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
    {
        MessageBox.Show("Speech recognized: " + e.Result.Text);
    }
}
4

1 回答 1

2

几年前,我有一个关于这个主题的案例研究。如果您将我的代码与您的代码进行比较,您会发现一些东西。下面的代码改变了灯泡的状态。

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


namespace SesTanima
{
    public partial class Form1 : Form
    {
        private SpeechRecognitionEngine recognizer = new SpeechRecognitionEngine();

        public Form1()
        {
            InitializeComponent();
        }

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


        private void LoadGrammars() 
        {
            Choices choices = new Choices( new string[] {"Lights on", "Exit", "Zoom out", "Zoom in", "Reset", "Lights off" } );
            GrammarBuilder grammarBuilder = new GrammarBuilder(choices);
            Grammar grammar = new Grammar(grammarBuilder);
            recognizer.LoadGrammar(grammar);
        }

        private void StartRecognition() 
        {
            recognizer.SpeechDetected += new EventHandler<SpeechDetectedEventArgs>(recognizer_SpeechDetected);
            recognizer.SpeechRecognitionRejected += new EventHandler<SpeechRecognitionRejectedEventArgs>(recognizer_SpeechRecognitionRejected);
            recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);
            recognizer.RecognizeCompleted += new EventHandler<RecognizeCompletedEventArgs>(recognizer_RecognizeCompleted);


            Thread t1 = new Thread(delegate()
            {            
                recognizer.SetInputToDefaultAudioDevice();
                recognizer.RecognizeAsync(RecognizeMode.Single);
            });
            t1.Start();
        }

        private void recognizer_SpeechDetected(object sender, SpeechDetectedEventArgs e) 
        {
            textBox1.Text = "Recognizing voice command...";     
        }

        private void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e) 
        {
            if (e.Result.Text == "Lights on")
            {
                pictureBox1.Image = Properties.Resources.lightsOn;
            }
            else if (e.Result.Text == "Lights off")
            {
                pictureBox1.Image = Properties.Resources.lightsOff;
            }
            else if ( e.Result.Text == "Exit" )
            {
                recognizer.Dispose();
                Application.Exit();

            }
            else if ( e.Result.Text == "Zoom out" )
            {
                pictureBox1.Size = new System.Drawing.Size( 135, 107 );
            }
            else if ( e.Result.Text == "Zoom in" )
            {
                pictureBox1.Size = new System.Drawing.Size( 538, 426 );
            }
            else if ( e.Result.Text == "Reset" )
            {
                pictureBox1.Size = new System.Drawing.Size( 269, 213 );
            }
            textBox1.Text = e.Result.Text;
        }

        private void recognizer_SpeechRecognitionRejected(object sender, SpeechRecognitionRejectedEventArgs e) 
        {
            textBox1.Text = "Failure.";
        }

        private void recognizer_RecognizeCompleted(object sender, RecognizeCompletedEventArgs e) 
        {
            recognizer.RecognizeAsync();
        }
    }
}
于 2013-04-14T19:07:29.400 回答