Gabriel Brandao
Gabriel Brandao

Reputation: 537

Blank Form in C# using speech.recognition

Hi i have situation here i don't know how to solve this. This code is fine with 0 Errors but whenever i do F5 it shows me a blank form with no voice and no recognition please help . Please i really need help can someone help me please.
Now this line is only for "it look like your post is mostly code; please add some more details";

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

namespace a.i
{
    public partial class Form1 : Form
    {
        SpeechSynthesizer s = new SpeechSynthesizer();
        Choices list = new Choices();
        public Form1()
        {

            SpeechRecognitionEngine rec = new SpeechRecognitionEngine();
            list.Add(new String[] { "hello", "how are you" });

            Grammar gr = new Grammar(new GrammarBuilder(list));

            try
            {
                rec.RequestRecognizerUpdate();
                rec.LoadGrammar(gr);
                rec.SpeechRecognized += rec_SpeachRecognized;
                rec.SetInputToDefaultAudioDevice();
                rec.RecognizeAsync(RecognizeMode.Multiple);
            }
            catch { return; }
            s.SelectVoiceByHints(VoiceGender.Neutral);

            s.Speak("Hello, my name is Gabriel ChatterBot");

            InitializeComponent();
        }

        public void say(string h)
        {
            s.Speak(h);
        }
        private void rec_SpeachRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            String r = e.Result.Text;
            //what you say
            if (r == "hello")
            {
                // what it says
                say("hi");
            }
            //what you say
            if (r == "how are you")
            {
                // what it says
                say("Great, and you!");
            }
        }
    }
}

Upvotes: 0

Views: 1455

Answers (1)

Thomas Voß
Thomas Voß

Reputation: 1165

From the exception you posted, I think that you have not initionalized the Grammar and SpeechRecognitionEngine correctly. It seems that you need to specify a language / culture for it. From the documentation at: https://msdn.microsoft.com/en-us/library/hh378426(v=office.14).aspx

// Create a new SpeechRecognitionEngine instance.
SpeechRecognitionEngine sre = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("en-US"));

Upvotes: 1

Related Questions