2016-09-25 11 views
1

안녕하세요. 저는 현재 내 목소리를 인식하고 듣는 것을 기록해야하는 프로그램을 개발하고 있습니다. 코드를 실행하고 buttun을 클릭하여 recEngine을 시작합니다. "처리되지 않은 'System.NullReferenceException'형식의 예외가 System.Speech.dll에서 발생했습니다. 팁에 var이 null 인 가능성이 있습니다. 하지만 전에 변수를 설정했거나 잘못하고 있습니다.RecognizeAsync로 인식기를 시작할 때 NullReferenceException이 발생했습니다.

다음은이 라인 오류 shos 최대 인 코드

using System; 
using System.Windows.Forms; 
using System.Speech.Recognition; 

namespace Voice_Recognition 
{ 
    public partial class Form1 : Form 
    { 
     SpeechRecognitionEngine recEngine = new SpeechRecognitionEngine(); 

     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void BtnEnable_Click(object sender, EventArgs e) 
     { 

입니다. 도움말 메뉴에서

  recEngine.RecognizeAsync(RecognizeMode.Multiple); 
      // BtnEnable.Enabled = false; 
      BtnDisable.Enabled = true; 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      Choices commands = new Choices(); 
      commands.Add(new string[] { "say hello", "print my name" }); 
      GrammarBuilder Gbuilder = new GrammarBuilder(); 
      Gbuilder.Append(commands); 
      Grammar grammar = new Grammar(Gbuilder); 
      recEngine.LoadGrammarAsync(grammar); 
      recEngine.SetInputToDefaultAudioDevice(); 
      recEngine.SpeechRecognized += recEngine_SpeechRecognized; 
     } 

     private void recEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e) 
     { 
      switch (e.Result.Text) 
      { 
       case "say hello": 
        Log.Text += "\nHello"; 
        break; 
       case "print my name": 
        Log.Text += "\nMyname"; 
        break; 

      } 
     } 

     private void BtnDisable_Click(object sender, EventArgs e) 
     { 
      BtnEnable.Enabled = true; 
      BtnDisable.Enabled = false; 
      recEngine.RecognizeAsyncStop(); 
     } 
    } 
} 

는 말한다 :

- 체크 객체의 인스턴스를 생성하기 위해 "새로운"키워드 -USE 방법

를 호출하기 전에 객체가 null의 여부를 결정하기 위해

미리 감사드립니다.

답변

1

이상한 모양의 몇 가지 :

  • 사용 recEngine.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(recEngine_SpeechRecognized); 대신 그것은 문법은 당신이 인식을 시작하는 시간에 의해로드가 완료되지 않은 가능성이 recEngine.SpeechRecognized += recEngine_SpeechRecognized;

  • 의; LoadGrammarAsync 대신 LoadGrammar을 사용하거나 LoadGrammarCompleted에 대한 처리기를 추가하십시오.

  • 저는 인식기 (및 문법)에 대한 cultureinfo를 명시 적으로 지정하면 어떤 언어가 사용되고 있는지에 대한 오해를 막을 수 있으므로 항상 편리하다는 것을 알았습니다.