首页 新闻 搜索 专区 学院

使用MS的Speech写的语音识别,资源释放问题

0
悬赏园豆:20 [已关闭问题] 关闭于 2012-11-06 11:14
 private SpInProcRecoContext m_wavRecoContext;
        private ISpeechRecoGrammar m_Grammar;
        private SpFileStream m_infile;
        public CtiLine ctiLine;
        private string result = string.Empty;
        SpInprocRecognizer recognizer;

        public SpRecognizer()
        {
            try
            {
                recognizer = new SpInprocRecognizer();
                m_wavRecoContext = (SpInProcRecoContext)recognizer.CreateRecoContext();
                m_wavRecoContext.RetainedAudio = SpeechRetainedAudioOptions.SRAORetainAudio;
                m_infile = new SpFileStreamClass();
                m_infile.Format.GetWaveFormatEx();
            }
            catch (Exception ex)
            {
                SearchLog.Log.AppendLog("RecognizeWav", ex.ToString());
            }
        }

        public void OpenWAV()
        {
            try
            {
                m_Grammar = m_wavRecoContext.CreateGrammar(0);
                m_Grammar.DictationLoad("", SpeechLoadOption.SLOStatic);
                m_wavRecoContext.Recognition += new _ISpeechRecoContextEvents_RecognitionEventHandler(RecoContext_Recognition);
                m_wavRecoContext.EndStream += new _ISpeechRecoContextEvents_EndStreamEventHandler(RecoContext_EndRecognition);
                m_infile.Open(ctiLine.MStation.RecorderFilePath, SpeechStreamFileMode.SSFMOpenForRead, false);
                m_wavRecoContext.Recognizer.AudioInputStream = m_infile;
                m_Grammar.DictationSetState(SpeechRuleState.SGDSActive);
            }
            catch (Exception ex)
            {
                SearchLog.Log.AppendLog("OpenWAV", ex.ToString());
            }
        }

        void RecoContext_Recognition(int StreamNumber, object StreamPosition, SpeechRecognitionType RecognitionType, ISpeechRecoResult Result)
        {
            if (m_infile != null)
            {
                result += Result.PhraseInfo.GetText(0, -1, true);
            }
        }

        void RecoContext_EndRecognition(int StreamNumber, object StreamPosition, bool f)
        {
            ctiLine.MStation.MobileIsStation = IsMobileStation();
            SearchLog.Log.AppendLog("(" + ctiLine.MStation.MobileNo + ")" + "Recognize Result", result);
            m_Grammar.DictationSetState(SpeechRuleState.SGDSInactive);
            m_wavRecoContext.Recognition -= RecoContext_Recognition;
            m_wavRecoContext.EndStream -= RecoContext_EndRecognition;
            ctiLine.IsRecognized = true;
            m_infile.Close();
            m_infile = null;
            m_wavRecoContext = null;
            m_Grammar = null;
            recognizer = null;          
        }

上面是主要代码,资源释放有问题,运行一段时间后  大概识别3700个左右的时候就会报System.OutOfMemoryException: Insufficient memory to continue the execution of the program.错误,内存不足,

请各位支招,怎么修改下,急

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