“价值未落在预期范围内”是什么意思?意味着运行时错误?

发布于 2024-08-28 18:25:23 字数 2287 浏览 4 评论 0原文

我正在使用 /clr 编写一个插件(dll 文件)并尝试使用 .NET 实现语音识别。 但是当我运行它时,我收到运行时错误,提示“值不在预期范围内”,该消息是什么意思?

    public ref class Dialog : public System::Windows::Forms::Form
    {
       public: SpeechRecognitionEngine^ sre;

       private: System::Void btnSpeak_Click(System::Object^  sender, System::EventArgs^  e) 
       {
         Initialize();
       }

       protected: void Initialize()
       {  
          //create the recognition engine
          sre = gcnew SpeechRecognitionEngine();

          //set our recognition engine to use the default audio device
          sre->SetInputToDefaultAudioDevice();

          //create a new GrammarBuilder to specify which commands we want to use
          GrammarBuilder^ grammarBuilder = gcnew GrammarBuilder();

          //append all the choices we want for commands.
          //we want to be able to move, stop, quit the game, and check for the cake.
          grammarBuilder->Append(gcnew Choices("play", "stop"));

          //create the Grammar from th GrammarBuilder
          Grammar^ customGrammar = gcnew Grammar(grammarBuilder);

          //unload any grammars from the recognition engine
          sre->UnloadAllGrammars();

          //load our new Grammar
          sre->LoadGrammar(customGrammar);

          //add an event handler so we get events whenever the engine recognizes spoken commands
          sre->SpeechRecognized += gcnew EventHandler<SpeechRecognizedEventArgs^> (this, &Dialog::sre_SpeechRecognized);

          //set the recognition engine to keep running after recognizing a command.
              //if we had used RecognizeMode.Single, the engine would quite listening after
          //the first recognized command.
          sre->RecognizeAsync(RecognizeMode::Multiple);

          //this->init();
       }  

       void sre_SpeechRecognized(Object^ sender, SpeechRecognizedEventArgs^ e)
       {
          //simple check to see what the result of the recognition was
          if (e->Result->Text == "play")
          {
             MessageBox(plugin.hwndParent, L"play", 0, 0);
          }

                  if (e->Result->Text == "stop")
          {
             MessageBox(plugin.hwndParent, L"stop", 0, 0);
          }
       }
    };

I'm writing a plugin (dll file) using /clr and trying to implement speech recognition using .NET.
But when I run it, I got a runtime error saying "Value does not fall within expected range", what does the message mean?

    public ref class Dialog : public System::Windows::Forms::Form
    {
       public: SpeechRecognitionEngine^ sre;

       private: System::Void btnSpeak_Click(System::Object^  sender, System::EventArgs^  e) 
       {
         Initialize();
       }

       protected: void Initialize()
       {  
          //create the recognition engine
          sre = gcnew SpeechRecognitionEngine();

          //set our recognition engine to use the default audio device
          sre->SetInputToDefaultAudioDevice();

          //create a new GrammarBuilder to specify which commands we want to use
          GrammarBuilder^ grammarBuilder = gcnew GrammarBuilder();

          //append all the choices we want for commands.
          //we want to be able to move, stop, quit the game, and check for the cake.
          grammarBuilder->Append(gcnew Choices("play", "stop"));

          //create the Grammar from th GrammarBuilder
          Grammar^ customGrammar = gcnew Grammar(grammarBuilder);

          //unload any grammars from the recognition engine
          sre->UnloadAllGrammars();

          //load our new Grammar
          sre->LoadGrammar(customGrammar);

          //add an event handler so we get events whenever the engine recognizes spoken commands
          sre->SpeechRecognized += gcnew EventHandler<SpeechRecognizedEventArgs^> (this, &Dialog::sre_SpeechRecognized);

          //set the recognition engine to keep running after recognizing a command.
              //if we had used RecognizeMode.Single, the engine would quite listening after
          //the first recognized command.
          sre->RecognizeAsync(RecognizeMode::Multiple);

          //this->init();
       }  

       void sre_SpeechRecognized(Object^ sender, SpeechRecognizedEventArgs^ e)
       {
          //simple check to see what the result of the recognition was
          if (e->Result->Text == "play")
          {
             MessageBox(plugin.hwndParent, L"play", 0, 0);
          }

                  if (e->Result->Text == "stop")
          {
             MessageBox(plugin.hwndParent, L"stop", 0, 0);
          }
       }
    };

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

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

发布评论

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

评论(3

回忆躺在深渊里 2024-09-04 18:25:23

我想我知道是什么导致了这个错误。

错误发生在

SpeechRecognitionEngine.SetInputToDefaultAudioDevice();

行。该错误意味着输入设备的通道超出了可接受的通道范围。这是因为有时在 Windows XP 上输入设备有 0 个通道。这是调用时的错误返回,从而导致错误。这并不意味着麦克风不起作用。

您可以做的就是首先将输入记录到 wav 文件,然后识别该 wav 文件中的语音,如下所示:

SpeechRecognitionEngine.SetInputToWaveFile("input.wav");

我希望这可以为您解决问题。

I think I know what is causing this error.

The error happens at the line

SpeechRecognitionEngine.SetInputToDefaultAudioDevice();

The error means that the input device's channels are out of the range of accepted channels. This is because sometimes on Windows XP an input device has 0 channels. This is a faulty return when called upon, thus leading to an error. This does not mean that the Microphone does not work.

What you can do is first record the input to a wav file, and then recognize the speech from that wav file, like so:

SpeechRecognitionEngine.SetInputToWaveFile("input.wav");

I hope this resolves the problem for you.

巡山小妖精 2024-09-04 18:25:23

您可能使用的是 Windows Pre-Vista (NT5)...
该错误是由于 SAPI 版本不是 5.3 或更高版本造成的...
在 Windows 7 中测试代码,一切都应该运行正常...

您得到的“互操作”内容与本机代码和 .net 托管代码库之间的编组有关...

您可以在库中看到问题,第 299 行到325: C# 中的 .NET 框架源代码,C# .NET 中的 RecognizerBase.cs 源代码

You are probably using a Windows Pre-Vista (NT5)...
That error is due to SAPI version is not 5.3 or higher...
Test the code in Windows 7, everything should run OK...

The "Interop" things you are getting relates to the marshalling between native code and .net managed code libraries...

You can see the problem in the library, lines 299 to 325: Source code for the .NET framework in C#, RecognizerBase.cs source code in C# .NET.

·深蓝 2024-09-04 18:25:23

几天前我在工作中遇到了这个错误。经过大量研究和调试后,我发现以下内容:

https://social.msdn.microsoft.com/Forums/sqlserver/en-US/fc9ba226-7170-49d8-9fb3-c8de05d5b542/systemspeechrecognition- exception-after-prolonged-use?forum=windowsgeneraldevelopmentissues

具体来说,扎克·巴纳德 (Zach Barnard) 的回答非常有帮助:

我可能已经找到了解决这个问题的方法,尽管以牺牲 Alternates 为代价,我将 MaxAlternates 设置为 1,并且在运行程序两天后我没有遇到异常。希望这对某人有帮助。

诚然,我不完全理解为什么会抛出这个异常。但是,将 SpeechRecognitionEngine 对象的 MaxAlternates 设置为 1 会阻止这种情况发生。

由于原始问题的作者从未说过实际问题是什么,因此这可能是一个解决方案,也可能不是。

I came across this error a couple of days ago at work. After looking into and debugging a lot I found the following:

https://social.msdn.microsoft.com/Forums/sqlserver/en-US/fc9ba226-7170-49d8-9fb3-c8de05d5b542/systemspeechrecognition-exception-after-prolonged-use?forum=windowsgeneraldevelopmentissues

Specifically the answer from Zach Barnard is very helpful:

I may have found a solution to this, though at the sacrifice of Alternates, I set my MaxAlternates to 1, and I haven't had the exception, after running the program for two days. Hopefully this helps someone.

Admittedly I don't fully understand why this exception is thrown. However setting MaxAlternates to 1 for your SpeechRecognitionEngine object prevents it.

Since the author of the original question never said what the actual problem was, this could be a solution or not.

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