2010-05-16 2 views
0

나는 내 C# 프로젝트에서이 코드를 가지고 :이 음성 인식 wicked 버그를 고치는 방법?

public void startRecognition(string pName) 
{ 
    presentationName = pName; 

    if (WaveNative.waveInGetNumDevs() > 0) 
    { 
     string grammar = System.Environment.GetEnvironmentVariable("PUBLIC") + "\\SoundLog\\Presentations\\" + presentationName + "\\SpeechRecognition\\soundlog.cfg"; 

     if (File.Exists(grammar)) 
     { 
      File.Delete(grammar); 
     } 
     executeCommand(); 

     /// Create an instance of SpSharedRecoContextClass which will be used 
     /// to interface with the incoming audio stream 
     recContext = new SpSharedRecoContextClass(); 

     // Create the grammar object   
     recContext.CreateGrammar(1, out recGrammar); 
     //recContext.CreateGrammar(2, out recGrammar2); 
     // Set up dictation mode 
     //recGrammar2.SetDictationState(SpeechLib.SPRULESTATE.SPRS_ACTIVE); 
     //recGrammar2.SetGrammarState(SPGRAMMARSTATE.SPGS_ENABLED); 

     // Set appropriate grammar mode 
     if (File.Exists(grammar)) 
     { 
      recGrammar.LoadCmdFromFile(grammar, SPLOADOPTIONS.SPLO_STATIC); 
      //recGrammar.SetDictationState(SpeechLib.SPRULESTATE.SPRS_INACTIVE); 
      recGrammar.SetGrammarState(SPGRAMMARSTATE.SPGS_ENABLED); 
      recGrammar.SetRuleIdState(0, SPRULESTATE.SPRS_ACTIVE); 
     } 

     /// Bind a callback to the recognition event which will be invoked 
     /// When a dictated phrase has been recognised. 
     recContext.Recognition += new _ISpeechRecoContextEvents_RecognitionEventHandler(handleRecognition); 
     //    System.Windows.Forms.MessageBox.Show(recContext.ToString()); 
     // gramática compilada 
    } 
} 

private static void handleRecognition(int StreamNumber, 
    object StreamPosition, 
    SpeechLib.SpeechRecognitionType RecognitionType, 
    SpeechLib.ISpeechRecoResult Result) 
{ 
    string temp = Result.PhraseInfo.GetText(0, -1, true); 
    _recognizedText = ""; 
    //   System.Windows.Forms.MessageBox.Show(temp); 
    //   System.Windows.Forms.MessageBox.Show(recognizedWords.Count.ToString()); 
    foreach (string word in recognizedWords) 
    { 
     if (temp.Contains(word)) 
     { 
      //     System.Windows.Forms.MessageBox.Show("yes"); 
      _recognizedText = word; 
     } 
    } 
} 

이 코드는 내가 다른 응용 프로그램에서 사용하는 DLL을 생성합니다.

악의적 인 버그 : - 다른 응용 프로그램의 실행 시작 부분에서 startRecognition 메서드를 실행할 때이 코드는 매우 잘 작동합니다. 하지만 처음부터 약간의 시간이 지나면이 코드는 작동하지만 handleRecognition 메서드는 호출되지 않습니다. 단어가 Microsoft 음성 인식 응용 프로그램에 나타나기 때문에 인식되지만 핸들러 메서드는 호출되지 않습니다.

이 코드의 문제점을 알고 계십니까?

참고 :이 프로젝트에는 항상 실행중인 일부 코드가 있습니다. 그게 문제일까요? 다른 코드가 실행되고 있기 때문에이 코드를 실행할 수 없습니다.

+1

그것은 컴파일러가 분노와 당신의 평균 트릭을 재생하고 모든 의견 수를했다 : 당신이 핸들러는 가상이 아닌 정적 만드는 시도하면 그냥 궁금 P –

+0

를? – code4life

+0

나중에 가상의 것을 시도 할 것입니다. –

답변

0

코드의 다른 부분에 다른 처리기가 있습니다. 인식 핸들러는 다른 핸들러보다 먼저 호출되어야했습니다.

나는 그런 식으로 만든이 :

0

startRecognition()에 대한 두 번째 호출에서 핸들러를 recContext.Recognition에 추가하기 전에 예외가 throw 될 수 있습니다. try/catch를 모두 startRecognition()에두고 throw 된 예외를 표시합니다.

WaveNative.waveInGetNumDevs() 값을 로그 또는 추적 파일에 출력합니다. > 0이 아닌 경우 startRecognition() 메서드는 아무 것도 수행하지 않습니다.

+0

코덱이 실행되고 "recContext.Recognition"라인 다음에 MsgBox를 올리면서 테스트했기 때문에 예외가 발생하지 않는다는 것을 알고 있습니다. 예외가 발생하면 코드가 폭발 할 것입니다. P 그리고 다른 하나의 참고 사항, 나는이 메서드를 두 번 호출하지 않고 하나만 호출합니다. 하지만 호출이 다른 API의 실행 시작에 있고 끝나면 작동하지 않습니다. –

+0

참고 :이 프로젝트에는 항상 실행중인 일부 코드가 있습니다. 그게 문제일까요? 다른 코드가 실행되고 있기 때문에이 코드를 실행할 수 없습니다. –