“值不在预期范围内”是什么意思在运行时错误?

时间:2010-04-14 02:59:39

标签: .net dll c++-cli speech-recognition runtime-error

我正在使用/ 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);
          }
       }
    };

3 个答案:

答案 0 :(得分:0)

我想我知道造成这个错误的原因。

错误发生在

SpeechRecognitionEngine.SetInputToDefaultAudioDevice();

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

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

SpeechRecognitionEngine.SetInputToWaveFile("input.wav");

我希望这能为您解决问题。

答案 1 :(得分:0)

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

您获得的“Interop”事项与本机代码和.net托管代码库之间的编组有关...

您可以在库中找到问题,第299至325行:Source code for the .NET framework in C#, RecognizerBase.cs source code in C# .NET

答案 2 :(得分:0)

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

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,并且在运行程序两天后我没有例外。希望这有助于某人。

不可否认,我并不完全理解为什么抛出此异常。但是,为MaxAlternates对象设置1SpeechRecognitionEngine会阻止它。

由于原问题的作者从未说出实际问题是什么,这可能是一种解决方案。