Quantcast
Channel: Windows Forms General forum
Viewing all articles
Browse latest Browse all 12583

System.Speech SpeechRecognized event only runs once without restarting program

$
0
0

Hello all, I've created a sample speech recognition application in Visual Studio 2010, C# 4.0, which is to become part of a larger application.

The application reads back the words that are part of the loaded grammar successfully.

The problem is that my System.Speech SpeechRecognized event only runs once when I speak a recognized word in the Grammar.
If I attempt to call it again after it has run once, I get no response.
Only if I stop and restart my application will the event run.

I have enclosed the code I'm using.
Please suggest what I need to add or change to have the SpeechRecognizedEvent run when called, as I desire for additional recognized words to execute different portions of the voice program.

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.Speech.Synthesis;
using System.Threading;

namespace SpeechRecogitionTest1
{
    public partial class Form1 : Form
    {
        public SpeechRecognitionEngine _recognizer = new SpeechRecognitionEngine();
        public SpeechSynthesizer _synth; 

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //Create grammar
            Choices words = new Choices();
            words.Add("Command");
            words.Add("Commands");
            Grammar wordsList = new Grammar(new GrammarBuilder(words));
            wordsList.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(wordsList_SpeechRecognized);
            _recognizer.LoadGrammarAsync(wordsList);
            _recognizer.SetInputToDefaultAudioDevice();
            _recognizer.RecognizeAsync();
        }

        public void  wordsList_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            if (e.Result.Text == "Command" || e.Result.Text == "Commands") //more e.Result.Text to be added
            {
                if (_synth == null)
                {
                    _synth = new SpeechSynthesizer();
                }
                PromptBuilder pb = new PromptBuilder();
                pb.StartParagraph();
                pb.StartSentence();
                pb.AppendText("This is sample text to speak");
                pb.EndSentence();
                pb.EndParagraph();
                pb.StartParagraph();
                pb.StartSentence();
                pb.AppendText("This is some more sample text to speak");	
                _synth.SpeakAsync(pb);
                pb.ClearContent();
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (_recognizer != null)
            {
                _recognizer.Dispose();
            }
            if (_synth != null)
            {
                _synth.Dispose();
            }
            
        }
     
    }
}
One other thing. How can I get the SpeechRecognized event to recognize the word RESUME (as to begin again), instead of resume as in (Curriculum Vitae) or employment record. Thank you


Viewing all articles
Browse latest Browse all 12583

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>