首页新闻找找看学习计划

救火!!!!语音识别,线程问题

0
悬赏园豆:20 [已关闭问题] 关闭于 2012-10-19 11:51

先上代码

 

 /// <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事件,不知道具体怎么回事,请各位大侠帮帮忙,先谢过了,比较穷,豆不多

冰封一夏的主页 冰封一夏 | 初学一级 | 园豆:13
提问于:2012-09-25 11:00
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册