Click here to Skip to main content

Ritwesh asked:

Open original thread
Hi friends,
I was using the following code detect what the user say and change the BackColor of the form accordingly. However I am encountering run-time problems. Firstly, while I am clicking the button1 The following error comes up : 'There is no source code available for the current location.' Immediately after that the following error comes on the screen: 'At least one grammar must be loaded before doing a recognition.'
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Diagnostics;
using System.Speech;
using System.Speech.Recognition;
using System.Speech.AudioFormat;
using System.Speech.Synthesis;
 
namespace Trying
{
    public partial class Form1 : Form
    {
        private SpeechRecognitionEngine recognitionEngine;
        public Form1()
        {
            InitializeComponent();
            try
            {
                recognitionEngine = new SpeechRecognitionEngine();
                recognitionEngine.SetInputToDefaultAudioDevice();
                recognitionEngine.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(recognitionEngine_SpeechRecognized);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            
 
            
        }
 
        void recognitionEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            try
            {
                string line = "";
                foreach (RecognizedWordUnit word in e.Result.Words)
                {
                    if (word.Confidence >= 0.5f)
                        line += word.Text + " ";
                }
                string command = Regex.Replace(line, "Change", "");
                switch (command)
                {
                    case "Red":
                        this.BackColor = Color.Red;
                        break;
                    case "Blue":
                        this.BackColor = Color.Blue;
                        break;
                    case "Green":
                        this.BackColor = Color.Green;
                        break;
                }
                txtOutput.Text += line;//txtOutput is a RichTextBox
                txtOutput.Text += Environment.NewLine;
            }
            catch (Exception ex)
            {
                MessageBox.Show("2:" + ex.Message);
            }
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                recognitionEngine.RecognizeAsync(RecognizeMode.Multiple);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
 
        private Grammar CreateGrammarObject()
        {
            try
            {
                Choices commandChoices = new Choices("Red", "Green", "Blue");
                GrammarBuilder grammarBuilder = new GrammarBuilder("Change");
                grammarBuilder.Append(commandChoices);
                Grammar g = new Grammar(grammarBuilder);
                return g;
            }
            catch (Exception ex1)
            {
                MessageBox.Show("3:" + ex1.Message);
            }
        }
 
        private void button2_Click(object sender, EventArgs e)
        {
            recognitionEngine.RecognizeAsyncStop();
        }
    }
}
However, none of the errors come through the catch blocks. Can anyone please help me out?
Tags: C#, .NET3.5

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the The Code Project Open License (CPOL).



Advertise | Privacy | Mobile
Web03 | 2.8.141223.1 | Last Updated 26 Mar 2009
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100