0

我正在尝试构建一个显示消息框的应用程序,该消息框在说出音素的确切时刻显示音素。问题是这些消息框在演讲结束后等待显示音素。我怎样才能同时做这两件事?

void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{  
    speech = new SpeechSynthesizer();      
    speech.SetOutputToDefaultAudioDevice();
    speech.SpeakAsync(e.Result.Text);
    speech.PhonemeReached += speech_PhonemeReached;
}
void speech_PhonemeReached(object sender, PhonemeReachedEventArgs e)
{
    if (new[] {"a͡i","o"}.Contains(e.Phoneme))
       MessageBox.Show(e.Phoneme);
}

我还在学习 c#,我不太确定如何使用线程,但这是我与语音识别器相关的代码部分:

Thread q = new Thread( ()=> sre.SpeechRecognized += (s, args) =>
    {
         foreach (RecognizedPhrase phrase in args.Result.Alternates)
         {
            if (phrase.Confidence > 0.9f)
                sre_SpeechRecognized(s, args);
         }
    });
q.Start();

希望任何人都可以帮助我...

4

1 回答 1

1

在它自己的线程中运行语音识别器,并使用 Invoke 在主线程上显示消息框。

void speech_PhonemeReached(object sender, PhonemeReachedEventArgs e)
{
    if (new[] {"a͡i","o"}.Contains(e.Phoneme))
       this.Invoke(new Action(() => { MessageBox.Show(this, e.Phoneme); }));
}
于 2012-12-05T18:54:53.230 回答