简体   繁体   中英

Voice recognition using System.Speech not working correctly

I have been trying to experiment with the system.speech feature I have seen various online videos and web articles teaching how to properly use it, but I can't get it to work somehow. I get no errors, the program compiles as it should but when I speak nothing happens, I have tried changing my language to en-UK and back to en-US but it did nothing. I am using VS17 and the code is as follows:

    SpeechRecognitionEngine recEngine = new SpeechRecognitionEngine(new CultureInfo("en-US"));

    public Form1()
    {
        InitializeComponent();
        this.TransparencyKey = (BackColor);
        this.StartPosition = FormStartPosition.Manual;
        this.Location = new Point(Convert.ToInt32(0.10), 300);
        textBox1.Visible = false;
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        Choices commands = new Choices();
        commands.Add(new string[] { "hello" });
        GrammarBuilder gr = new GrammarBuilder();
        gr.Append(commands);
        Grammar grammar = new Grammar(gr);

        recEngine.LoadGrammarAsync(grammar);
        recEngine.SetInputToDefaultAudioDevice();
        recEngine.SpeechRecognized += RecEngine_SpeechRecognized;
    }

    private void RecEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
    {
        switch (e.Result.Text)
        {
            case "hello":
                MessageBox.Show("Hello");
                break;
        }
    }

    private void label1_Click(object sender, EventArgs e)
    {
    }

    private void pictureBox2_Click(object sender, EventArgs e)
    {
        recEngine.RecognizeAsyncStop();
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
    }

    void button1_Click(object sender, EventArgs e)
    {
        recEngine.RecognizeAsync(RecognizeMode.Multiple);
    }
}

Edit: I downloaded and tested the program with the same code below on 2 different computers and it works fine with all of them except this one. I tried to use the mic of both computers which recognized my speech. But none of them worked, so the problem lies within my PC after all, I might have to download some windows update with the speech feature or something like that. Where can I find it though?

Apparently running as an admin fixed the issue. Somehow this thought never even reached my mind.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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