Reputation: 7291
I'm trying to develop a SpeechRecognition demo app in C#. Currently my code is-
public partial class Form1 : Form
{
SpeechSynthesizer ss = new SpeechSynthesizer();
PromptBuilder pb = new PromptBuilder();
SpeechRecognitionEngine sre = new SpeechRecognitionEngine();
Choices choices;
public Form1()
{
InitializeComponent();
}
private void btnStart_Click(object sender, EventArgs e)
{
choices = new Choices();
choices.Add(new string[]{"hello","how are you","thank you"});
Grammar gr = new Grammar(new GrammarBuilder(choices));
try
{
sre.RequestRecognizerUpdate();
sre.LoadGrammar(gr);
sre.SpeechRecognized += sre_SpeechRecognized;
sre.SetInputToDefaultAudioDevice();
sre.RecognizeAsync(RecognizeMode.Multiple);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message,"Error");
}
}
void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
switch (e.Result.Text.ToString())
{
case "hello":
ss.SpeakAsync("hello");
break;
case "how are you":
ss.SpeakAsync("how are you");
break;
case "thank you":
ss.SpeakAsync("thank you");
break;
default:
break;
}
txtVoiceToText.Text += e.Result.Text.ToString() + Environment.NewLine;
}
}
If I don't want to use any predefined Choices
, is there any way out?
Upvotes: 1
Views: 672