先上代码
/// <summary> /// 識別Wav文件 /// </summary> /// <param name="mStation"></param> public void RecognizeWav(CtiLine ctiline) { //ctiline.Recognizings = true; this.ctiLine = ctiline; try { SpInprocRecognizer Recognizer = new SpInprocRecognizer(); InputFile = new SpFileStream(); SearchLog.Log.AppendLog("1", ""); //定义输入文件格式 SpAudioFormat AF = new SpAudioFormat(); AF.Type = SpeechAudioFormatType.SAFTCCITT_ALaw_8kHzStereo; InputFile.Format.SetWaveFormatEx(AF.GetWaveFormatEx()); //打开文件并将识别引擎的输入定位到文件 InputFile.Open(ctiLine.MStation.RecorderFilePath, SpeechStreamFileMode.SSFMOpenForRead, false); Recognizer.AudioInputStream = InputFile; //建立相关的上下文与语法 ISpeechRecoContext RecoContext = Recognizer.CreateRecoContext(); ((SpSharedRecoContext)(RecoContext)).Recognition += new _ISpeechRecoContextEvents_RecognitionEventHandler(RecoContext_Recognition); ISpeechRecoGrammar Grammar = RecoContext.CreateGrammar(10); Grammar.DictationLoad("", SpeechLoadOption.SLOStatic); Grammar.DictationSetState(SpeechRuleState.SGDSActive); } catch (Exception ex) { SearchLog.Log.AppendLog("RecognizeWav", ex.ToString()); if (InputFile != null) { InputFile.Close(); InputFile = null; } } }
#region 獲取識別結果的事件 private void RecoContext_Recognition(int StreamNumber, object StreamPosition, SpeechRecognitionType RecognitionType, ISpeechRecoResult Result) { try { SearchLog.Log.AppendLog("2", ""); if (InputFile != null) { this.ctiLine.IsRecognized = true; this.result = Result.PhraseInfo.GetText(0, -1, true); SearchLog.Log.AppendLog("(" + this.ctiLine.MStation.MobileNo + ")" + "Recognize Result", this.result); this.ctiLine.MStation.MobileIsStation = IsMobileStation(); InputFile.Close(); InputFile = null; } // Monitor.Exit(obj); } catch (Exception ex) { // Monitor.Exit(obj); SearchLog.Log.AppendLog("Recognize Error:", ex.Message); } }
代码上完,现在说问题:当同时有多个线程同时进入RecognizeWav方法的时候,只有一个线程会触发下面的RecoContext_Recognition事件,不知道具体怎么回事,请各位大侠帮帮忙,先谢过了,比较穷,豆不多