我使用语音识别库得到以下代码:
var listen = new SpeechRecognitionEngine();
var reader = new Choices(File.ReadLines(@"C:\words.txt")
listen.LoadGrammar(new Grammar(new GrammarBuilder(reader)));
listen.SpeechRecognized += listen_SpeechRecognized;
listen.SpeechRecognitionRejected += listen_SpeechRecognitionRejected;
listen.SetInputToDefaultAudioDevice();
listen.RecognizeAsync(RecognizeMode.Multiple);
我有一个像这样的事件监听器......
static void listen_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
var talk = new SpeechSynthesizer();
if (e.Result.Text == "Search Stock Symbol")
{
talk.Speak("What symbol?");
//Do I have to create another event listener?
//a Listener .. symbol = a.Result.Text
//talk.Speak(GetQuote(symbol))
{
}
我是否必须为"会话"?的每个部分创建一个事件监听器?如果是这种情况,还有更好的方法吗?
示例对话:
答案 0 :(得分:3)
不,只是那个,然后根据收到的文字改变你的行为。在之前的一些代码中:
List<string> stockSymbols = new List<string>();
stockSymbols.Add("AAPL");
然后
string lastSpeechInput;
static void listen_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
var talk = new SpeechSynthesizer();
switch (e.Result.Text) {
case "Search Stock Symbol":
talk.Speak("What symbol?");
break;
default:
break;
}
if (stockSymbols.Contains(e.Result.Text) && lastSpeechInput == "Search Stock Symbol") {
talk.Speak(getStockPrice(e.Result.Text);
}
lastSpeechInput = e.Result.Text;
}