在 C# 中按下 keydown 时播放键值
我对 C# 很陌生,正在致力于为盲人和视障人士开发软件。
在软件中,我想要一个文本框,当按下某个键时,它会生成一个
例如,当按 j 时,它会发出声音 j... 我找到了很棒的文本到语音引擎,并尝试将其与 keydown 事件一起使用,如下所示:
SpeechSynthesizer synth = new SpeechSynthesizer();
char mappedChar = (char)e.KeyValue;
synth.Speak(Convert.ToString(mappedChar));
synth.Dispose();
不幸的是,它太慢了,大约需要 1 分钟。每次击键之间间隔 1 秒。
如有任何建议,将不胜感激。
I'm quite new to C# and am working on developing a software for blind and visually impaired people.
in the software I want to have a text box that when a key is pressed it will make a
sound for instance when pressing j it will say j...
I found the wonderful text to speech engine and tried using it with the keydown event as follows:
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,我不在事件处理程序中创建和处置
SpeechSynthesizer
对象。程序运行时创建一次对象,并
在事件处理程序中包含:。
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:
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.
创建新的 SpeechSynthesizer 实例的成本很高。
将合成对象定义为表单的成员,在该范围内实例化它,然后在事件代码中引用它。例如在伪代码中...
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...