在 C# 中按下 keydown 时播放键值

发布于 2024-10-16 06:52:40 字数 410 浏览 6 评论 0原文


我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你又不是我 2024-10-23 06:52:40

首先,我不在事件处理程序中创建和处置 SpeechSynthesizer 对象。

程序运行时创建一次对象,并

char mappedChar = (char)e.KeyValue;       
synth.Speak(mappedChar.ToString());

在事件处理程序中包含:。

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.

纵性 2024-10-23 06:52:40

我怀疑语音合成器的构造函数占用了大部分时间。尝试创建一次合成对象并缓存它,而不是在每次调用时创建它。

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.

羁客 2024-10-23 06:52:40

创建新的 SpeechSynthesizer 实例的成本很高。

将合成对象定义为表单的成员,在该范围内实例化它,然后在事件代码中引用它。例如在伪代码中...

class MyForm
{
  SpeechSynthesizer synth = new SpeechSynthesizer(); 
  ...

  void On_Click(<params>)
  {
     this.synth.Speak(<text>);
  }

}

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>);
  }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文