我正在尝试使用C#开发SpeechRecognition演示应用。目前我的代码是 -
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;
}
}
如果我不想使用任何预定义的Choices
,有什么办法吗?