Playing the key value on keydown in C#
SpeechSynthesizer synth = new SpeechSynthesizer();
char mappedChar = (char)e.KeyValue;
synth.Speak(Convert.ToString(mappedChar));
synth.Dispose();
unfortunately it is way too slow and takes approx. 1 second between each key stroke.
Would appreciate any suggestion.
I'd start by not creating and disposing of your SpeechSynthesizer
object in the event handler.
Create the object once when the program runs and just have:
char mappedChar = (char)e.KeyValue;
synth.Speak(mappedChar.ToString());
in your event handler.
I suspect that the constructor of speechsynthesizer is taking most of the time. Try creating the synth object once and caching it, rather than creating it on each call.
Creating a new SpeechSynthesizer instance is expensive.
Define your synth object as a member of the form, instantiate it that scope, then just refer to it in your event code. e.g. in pseudo code...
class MyForm
{
SpeechSynthesizer synth = new SpeechSynthesizer();
...
void On_Click(<params>)
{
this.synth.Speak(<text>);
}
}
精彩评论