代码之家  ›  专栏  ›  技术社区  ›  manuel

“值不在预期范围内”是什么意思?

  •  1
  • manuel  · 技术社区  · 14 年前

    尝试使用clr/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 回复  |  直到 14 年前
        1
  •  1
  •   Stephen Kennedy annamataws    6 年前

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

    错误发生在线路上

    SpeechRecognitionEngine.SetInputToDefaultAudioDevice();

    错误意味着输入设备的通道超出了可接受通道的范围。这是因为有时在WindowsXP上输入设备有0个通道。当被调用时,这是一个错误的返回,因此会导致错误。这并不意味着麦克风不工作。

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

    SpeechRecognitionEngine.SetInputToWaveFile("input.wav");
    

        2
  •  1
  •   Stephen Kennedy annamataws    6 年前

    您可能正在使用Windows Pre-Vista(NT5)。。。 该错误是由于SAPI版本不是5.3或更高版本。。。

    您得到的“互操作”与本机代码和.net托管代码库之间的封送有关。。。

    Source code for the .NET framework in C#, RecognizerBase.cs source code in C# .NET .

        3
  •  0
  •   Stephen Kennedy annamataws    6 年前

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

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

    特别是扎克·巴纳德的回答非常有帮助:

    诚然,我不完全理解为什么抛出这个异常。但是设置 MaxAlternates 1 SpeechRecognitionEngine 对象阻止它。