protected override string RecognizeInternal(BackgroundWorker bw, string uriString) { bool finished = false; SpeechRecognitionSession session = null; EventHandler <WaveInEventArgs> handler = delegate(object sender, WaveInEventArgs e) { try { AudioStatus status = AudioStatus.ISR_AUDIO_SAMPLE_CONTINUE; EndPointStatusEnums ep_status = EndPointStatusEnums.ISR_EP_NULL; ///开始向服务器发送音频数据 string partResult = session.AppendAudio(e.Buffer, 0, e.BytesRecorded, status, out ep_status); if (!string.IsNullOrEmpty(partResult)) { this.OnResultArrived(new ResultArrivedEventArgs(partResult)); } if (ep_status == EndPointStatusEnums.ISR_EP_AFTER_SPEECH) {//检测到音频后端点,结束音频发送 finished = true; } } catch (Exception ex) { } finally { } }; try { session = client.CreateSession(); wis.DataAvailable += handler; wis.StartRecording(); while (true) { if (bw != null && bw.CancellationPending) { break; } if (finished) { break; } Thread.Sleep(200); } wis.DataAvailable -= handler; wis.StopRecording(); string lastResult = session.Finish(); if (!string.IsNullOrEmpty(lastResult)) { this.OnResultArrived(new ResultArrivedEventArgs(lastResult)); } string result = session.Result; return(result); } catch (Exception ex) { return(session.Result); } finally { if (session != null) { session.Dispose(); session = null; } } }
protected override string RecognizeInternal(BackgroundWorker bw, string fileName) { if (!File.Exists(fileName)) { throw new FileNotFoundException("文件不存在。", fileName); } string ext = Path.GetExtension(fileName).ToLower(); if (ext != ".wav" && ext != ".pcm") { throw new FormatException("音频文件格式不受支持,仅支持wav或pcm。"); } SpeechRecognitionSession session = null; FileStream stream = null; try { //打开文件 stream = new FileStream(fileName, FileMode.Open); if (Path.GetExtension(fileName).ToLower() == ".wav") { stream.Position = 44; } session = client.CreateSession(); byte[] buffer = new byte[session.BlockMaxLength]; AudioStatus audio_status = AudioStatus.ISR_AUDIO_SAMPLE_INIT; // 发送音频数据,获取语音听写结果 while (audio_status != AudioStatus.ISR_AUDIO_SAMPLE_LAST) { if (bw != null && bw.CancellationPending) { break; } int length = stream.Read(buffer, 0, buffer.Length); audio_status = (length == buffer.Length) ? AudioStatus.ISR_AUDIO_SAMPLE_CONTINUE : AudioStatus.ISR_AUDIO_SAMPLE_LAST; //端点检测器所处的状态 EndPointStatusEnums ep_status; string partResult = session.AppendAudio(buffer, 0, length, audio_status, out ep_status); if (!string.IsNullOrEmpty(partResult) && bw != null) { bw.ReportProgress(0, partResult); } if (ep_status == EndPointStatusEnums.ISR_EP_AFTER_SPEECH) {//检测到音频后端点,结束音频发送 break; } //如果是实时采集音频,可以省略此操作。5KB大小的16KPCM持续的时间是160毫秒 System.Threading.Thread.Sleep(160); } string lastResult = session.Finish(); if (!string.IsNullOrEmpty(lastResult) && bw != null) { bw.ReportProgress(0, lastResult); } return(session.Result); } catch (Exception) { return(session.Result); } finally { session.Dispose(); session = null; } }
protected override string RecognizeInternal(BackgroundWorker bw, string httpAudioUrl) { SpeechRecognitionSession session = null; HttpWebRequest webRequest = null; HttpWebResponse resp = null; Stream stream = null; try { //连接音频源 webRequest = WebRequest.Create(httpAudioUrl) as HttpWebRequest; resp = (HttpWebResponse)webRequest.GetResponse(); stream = resp.GetResponseStream(); session = client.CreateSession(); byte[] buffer = new byte[session.BlockMaxLength]; AudioStatus audio_status = AudioStatus.ISR_AUDIO_SAMPLE_INIT; // 发送音频数据,获取语音听写结果 while (audio_status != AudioStatus.ISR_AUDIO_SAMPLE_LAST) { if (bw != null && bw.CancellationPending) { break; } int length = stream.Read(buffer, 0, buffer.Length); //audio_status = (length == buffer.Length) ? AudioStatus.ISR_AUDIO_SAMPLE_CONTINUE : AudioStatus.ISR_AUDIO_SAMPLE_LAST; audio_status = AudioStatus.ISR_AUDIO_SAMPLE_CONTINUE; //端点检测器所处的状态 EndPointStatusEnums ep_status; string partResult = session.AppendAudio(buffer, 0, length, audio_status, out ep_status); if (!string.IsNullOrEmpty(partResult) && bw != null) { bw.ReportProgress(0, partResult); } if (ep_status == EndPointStatusEnums.ISR_EP_AFTER_SPEECH) {//检测到音频后端点,结束音频发送 break; } //如果是实时采集音频,可以省略此操作。5KB大小的16KPCM持续的时间是160毫秒 System.Threading.Thread.Sleep(160); } string lastResult = session.Finish(); if (!string.IsNullOrEmpty(lastResult) && bw != null) { bw.ReportProgress(0, lastResult); } return(session.Result); } catch (WebException ex) { if (ex.Status != WebExceptionStatus.RequestCanceled) {//不是视频流结束 throw ex; } //正常结束 return(session.Result); } catch (Exception ex) { return(session.Result); } finally { session.Dispose(); session = null; if (stream != null) { stream.Close(); stream = null; } if (resp != null) { resp.Close(); resp = null; } if (webRequest != null) { webRequest = null; } } }