/// <summary> /// 结束会话。 /// </summary> public string Finish() { StringBuilder lastResult = new StringBuilder(); int loop_count = 0; //获取余下的识别结果 EndPointStatusEnums ep_status = EndPointStatusEnums.ISR_EP_NULL; RecogStatusEnums rec_status = RecogStatusEnums.ISR_REC_NULL; ///最后一块数据发完之后,循环从服务器端获取结果 IsrInterop.AudioWrite(this.sessionId, IntPtr.Zero, 0, AudioStatus.ISR_AUDIO_SAMPLE_LAST, out ep_status, out rec_status); ///考虑到网络环境不好的情况下,需要对循环次数作限定 while (this.rslt_status != RecogStatusEnums.ISR_REC_STATUS_SPEECH_COMPLETE && loop_count++ < 30) { string str = null; try { str = IsrInterop.GetResult(sessionId, out rslt_status, waitTime); } catch (EngineNoDataException) { break; } if (!string.IsNullOrEmpty(str)) { this.result.Append(str); lastResult.Append(str); } System.Threading.Thread.Sleep(200); } return(lastResult.ToString()); }
/// <summary> /// 添加音频文件。 /// </summary> /// <param name="buffer"></param> /// <param name="offset"></param> /// <param name="length"></param> /// <param name="audio_status"></param> /// <param name="ep_status">端点检测(End-point detected)器所处的状态</param> public string AppendAudio(byte[] buffer, int offset, int length, AudioStatus audio_status, out EndPointStatusEnums ep_status) { if (length > BLOCK_LEN) { throw new ArgumentException("length超过了最大发送长度。", "length"); } //返回部分结果 string partResult = null; RecogStatusEnums rec_status;//识别器所处的状态 Marshal.Copy(buffer, 0, ptrBuffer, length); ///开始向服务器发送音频数据 IsrInterop.AudioWrite(sessionId, ptrBuffer, (uint)length, audio_status, out ep_status, out rec_status); ///服务器返回部分结果 if (rec_status == RecogStatusEnums.ISR_REC_STATUS_SUCCESS) {//已经有结果缓存在MSC中了,可以获取了 partResult = IsrInterop.GetResult(sessionId, out this.rslt_status, waitTime); if (!string.IsNullOrEmpty(partResult)) { this.result.Append(partResult); } } return(partResult); }