2010-05-16 75 views
0

我在我的C#項目的代碼:如何解決這個語音識別邪惡的錯誤?

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。

現在,邪惡的bug: - 當我在其他應用程序的執行開始時運行startRecognition方法時,這些代碼運行良好。但是,當我在開始後的一段時間運行它時,這些代碼有效,但handleRecognition方法從未被調用過。我看到這些單詞被識別,因爲它們出現在Microsoft語音識別應用程序中,但處理程序方法從未被調用過。

你知道這段代碼有什麼問題嗎?

注意:這個項目有一些代碼正在執行。這可能是問題嗎?因爲其他代碼正在運行,它不允許它運行?

+1

這可能是所有使編譯器憤怒和你玩花招均值的評論:P – 2010-05-16 21:10:27

+0

只是想知道,如果你已經嘗試做了處理虛擬,而不是靜態的? – code4life 2010-05-17 01:31:18

+0

我會稍後嘗試虛擬的東西:) – 2010-05-17 08:15:38

回答

0

我在代碼的另一部分有另一個處理程序。 識別處理程序必須在另一個之前被調用。

我做的方式,它的工作:)

0

在第二次調用startRecognition()時,可能會在將句柄添加到recContext.Recognition之前拋出異常。圍繞startRecognition()中的所有內容進行嘗試/捕獲,並回應所有拋出的異常。

我也將WaveNative.waveInGetNumDevs()的值輸出到日誌或跟蹤文件。如果不是> 0那麼startRecognition()方法將不會執行任何操作。

+0

我知道corde運行,並且不會給出任何異常,因爲我已經測試過將msgBox放在「recContext.Recognition」行之後。如果拋出異常,代碼將爆炸:P 還有一點需要注意,我不會對這個方法進行兩次調用,只有一次。但是,當調用在另一個API的執行開始時並且在結束時不起作用時,它會起作用。 – 2010-05-17 08:18:12

+0

注意:這個項目有一些正在執行的代碼。這可能是問題嗎?因爲其他代碼正在運行,它不允許它運行? – 2010-05-17 08:22:52