.net 中的语音识别问题

发布于 2024-10-26 11:20:56 字数 1267 浏览 3 评论 0原文

我是语音识别领域的新手,并且开发了一个文本编辑器,可以编写我所说的内容。我遇到一个问题,我可以通过代码启用语音识别,但无法禁用它。任何人都可以建议如何禁用语音识别。我的语音识别代码如下:

//function to start/stop speech recognition

private void enableSpeechRecognitionToolStripMenuItem_Click(object sender, EventArgs e)
{
   listener = new SpeechLib.SpSharedRecoContext();
   //crating a share recognition object
   listener.Recognition += new _ISpeechRecoContextEvents_RecognitionEventHandler(listener_Reco);
   //creating a recgnition event handler object
   grammar = listener.CreateGrammar(0);
   //create grammar interface with ID = 0
   grammar.DictationLoad("", SpeechLoadOption.SLOStatic);
   //setting grammar load type to static
   grammar.DictationSetState(SpeechRuleState.SGDSActive);
   //activating speech dictation
   enableSpeechRecognitionToolStripMenuItem.Checked = true;
   //checked
   toolStripStatusLabel1.Text = "[Speech Recognition Enabled]"; 
}

//function to append the listened text to the text box's text
public void listener_Reco(int StreamNumber, object StreamPosition, SpeechRecognitionType RecognitionType, ISpeechRecoResult Result)
{
   string heard = Result.PhraseInfo.GetText(0, -1, true);
   //setting heard text to a variable
   richTextBox1.Text += " " + heard;
   //appending heard text
}

I'm new to speech recognition and have developed a text editor which writes what I speak to it. I'm into a problem that I can enable the speech recognition through code, but cannot disable it. Can anyone please suggest how to disable speech recognition. My speech recognizing code is as follows:

//function to start/stop speech recognition

private void enableSpeechRecognitionToolStripMenuItem_Click(object sender, EventArgs e)
{
   listener = new SpeechLib.SpSharedRecoContext();
   //crating a share recognition object
   listener.Recognition += new _ISpeechRecoContextEvents_RecognitionEventHandler(listener_Reco);
   //creating a recgnition event handler object
   grammar = listener.CreateGrammar(0);
   //create grammar interface with ID = 0
   grammar.DictationLoad("", SpeechLoadOption.SLOStatic);
   //setting grammar load type to static
   grammar.DictationSetState(SpeechRuleState.SGDSActive);
   //activating speech dictation
   enableSpeechRecognitionToolStripMenuItem.Checked = true;
   //checked
   toolStripStatusLabel1.Text = "[Speech Recognition Enabled]"; 
}

//function to append the listened text to the text box's text
public void listener_Reco(int StreamNumber, object StreamPosition, SpeechRecognitionType RecognitionType, ISpeechRecoResult Result)
{
   string heard = Result.PhraseInfo.GetText(0, -1, true);
   //setting heard text to a variable
   richTextBox1.Text += " " + heard;
   //appending heard text
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

鸠魁 2024-11-02 11:20:56

如果我没记错的话,SpeechLib 是一个围绕 SAPI 的 COM 互操作包装器API。您可能最好使用 System.Speech< 中的本机 .NET 托管语音类/a>. MSDN 文章中提到stackoverflow.com/questions/5101119/looking-for-a-book-on-net-speech-recognition/5118157#5118157">https://stackoverflow.com/questions/5101119/looking-for-a-book-on -net-speech-recognition/5118157#5118157 是一个很好的起点。我发布了一个很好的简单示例来帮助开始 在 ASP.NET Web 应用程序中将语音转录为文本的最佳选项是什么?

我认为您也在使用共享识别器。如果您使用自己的 inproc SpeechRecognitionEngine 实例,您将可以更好地控制识别。共享识别器用于可以控制 Windows 桌面或多个应用程序的应用程序。

If I'm not mistaken, SpeechLib is a COM interop wrapper around the SAPI API. You might be better off using the native .NET Managed Speech classes in System.Speech. The MSDN article mentioned in https://stackoverflow.com/questions/5101119/looking-for-a-book-on-net-speech-recognition/5118157#5118157 is a good place to start. I posted a good simple example to help get started in What is the best option for transcribing speech-to-text in a asp.net web app?.

I think you are also using a shared recognizer. If you use you own instance of an inproc SpeechRecognitionEngine, you'll have more control over the recognition. The shared recognizer is used for applications that can control the windows desktop or multiple applications.

冰雪梦之恋 2024-11-02 11:20:56

当您想要禁用语音识别时,您是否尝试过删除识别处理程序?

有关如何删除事件处理程序的示例,请参阅此问题

Have you tried removing the Recognition handler when you want to disable Speech Recognition?

See this question for an example of how to remove an event handler.

帅冕 2024-11-02 11:20:56

您是否尝试过更改规则状态或识别器状态?例如,尝试

grammar.DictationSetState(SpeechRuleState.SGDSInactive);

我也同意迈克尔的观点,您可能需要一个进程内识别引擎,而不是共享引擎。

Have you tried changing the rule state or recognizer state? E.g., try

grammar.DictationSetState(SpeechRuleState.SGDSInactive);

I also concur with Michael that you probably want an inproc recognition engine, rather than the shared engine.

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