2

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

3 回答 3

1

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

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

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

于 2013-11-14T23:04:53.160 回答
1

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

错误发生在该行

SpeechRecognitionEngine.SetInputToDefaultAudioDevice();

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

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

SpeechRecognitionEngine.SetInputToWaveFile("input.wav");

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

于 2013-07-12T13:31:17.083 回答
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

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

于 2018-04-04T08:45:32.020 回答