I'm working on a speech recognizer but every time I say a word it puts it in a loop and keeps saying:
hi my name is Jarvis how can I help you?
What should I do?
I've also tried RecognizeAsyncCancel() but it didn't work!
My code is below:
using System;
using System.IO;
using System.Speech.Recognition;
using System.Speech.Synthesis;
using System.Windows.Forms;
namespace Jarvis
{
public partial class Form1 : Form
{
SpeechRecognitionEngine RecEngine = new SpeechRecognitionEngine();
SpeechSynthesizer speech = new SpeechSynthesizer();
public Form1()
{
InitializeComponent();
Choices choices = new Choices();
string[] text = File.ReadAllLines(Environment.CurrentDirectory + "//grammer.txt");
choices.Add(text);
Grammar grammar = new Grammar(new GrammarBuilder(choices));
RecEngine.LoadGrammar(grammar);
RecEngine.SetInputToDefaultAudioDevice();
RecEngine.RecognizeAsync(RecognizeMode.Multiple);
RecEngine.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(RecEngine_SpeechRecognized);
speech.SelectVoiceByHints(VoiceGender.Male);
}
private void RecEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
string result = e.Result.Text;
if (result == "Hello")
{
result = "";
result = "Hi, my name is Jarvis, how can I help you";
}
if (result == "Hi")
{
result = "";
result = "It is currently " + DateTime.Now.ToLongTimeString();
}
speech.SpeakAsync(result);
lblresult.Text = result;
result = "";
MessageBox.Show("a");
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}