Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have written the following code to perform speech recognition using the Speech API built into .NET, but it's not recognising speech. Could you please help?
C#
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;
using System.Speech.Synthesis;
using System.Speech.Recognition;
using System.Speech.Recognition.SrgsGrammar;
using System.Threading;


namespace SpeechToText
{
    public partial class Form1 : Form
    {
        SpeechSynthesizer sSynth = new SpeechSynthesizer();
        PromptBuilder pBuilder = new PromptBuilder();
        SpeechRecognitionEngine sRecognize = new SpeechRecognitionEngine();
       
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
          pBuilder.ClearContent();
            pBuilder.AppendText(textBox1.Text);
            sSynth.Speak(pBuilder);
        }

        private void button2_Click(object sender, EventArgs e)
        {
           button2.Enabled = false;
            button3.Enabled= true;
            Choices sList = new Choices();
            sList.Add(new string[] { "hello","hi","how","are","you","srinivas","test","i"});
            Grammar gr = new Grammar(new GrammarBuilder(sList));
            try
            {
                sRecognize.RequestRecognizerUpdate();
                sRecognize.LoadGrammar(gr);
                sRecognize.SpeechRecognized += sRecognize_SpeechRecognized;

                sRecognize.SetInputToDefaultAudioDevice();
                sRecognize.RecognizeAsync(RecognizeMode.Multiple);

            }
            catch
            {
                return;
            }
          /*  sRecognize.SetInputToDefaultAudioDevice();
            sRecognize.LoadGrammar(new DictationGrammar());
            sRecognize.SpeechRecognized += new EventHandler(sRecognize_SpeechRecognized);
            sRecognize.RecognizeAsync(RecognizeMode.Multiple);*/

        }

        void sRecognize_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
           /* foreach (RecognizedWordUnit word in e.Result.Words)
            {
                listBox1.Items.Add(word.Text);
            }*/
            if (e.Result.Text == "exit")
            {
                Application.Exit();
            }
            else
            {
                textBox1.Text = textBox1.Text + " " + e.Result.ToString();
            }
        }

       

        private void Form1_Load(object sender, EventArgs e)
        {
          // sRecognize.SpeechRecognized += new EventHandler(sRecognize_SpeechRecognized);

        }

       
       
    }
}
Posted
Updated 19-Nov-13 22:53pm
v2

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900