失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 微信小程序+.NET(四) 科大讯飞语音接口-iat语音听写

微信小程序+.NET(四) 科大讯飞语音接口-iat语音听写

时间:2018-11-14 22:01:35

相关推荐

微信小程序+.NET(四) 科大讯飞语音接口-iat语音听写

科大讯飞语音接口-iat语音听写

Demo下载链接:/download/jinglell/11566402

首先,来看科大讯飞给的msc.dll API文档,

可以看到给出了语音识别详细的接口说明,

/windows/api/iFlytekMSCReferenceManual/qisr_8h.html

然后我们看另一个文档

/windows/api/iFlytekMSCReferenceManual/msp__cmn_8h.html

这里面就包含了MSC的初始化登入与最后的登出。

接下来就是引入MSC里面的接口,使用DLLImport(),我将DLL的import单独放在了一个文件里。

之后就是编写audio2text.ashx请求页,

首先要对appID进行修改,修改你自己的,并将msc.dll替换为你自己从官网下载来的,以免版本不同出现问题(若是遇到找不到某个msc.dll里的方法,请先到上面的MSCAPI文档里找原因)

private static string audio_iat(string audio_path, string session_begin_params){if (audio_path == null || audio_path == "") return "";IntPtr session_id;StringBuilder result = new StringBuilder();//存储最终识别的结果var aud_stat = AudioStatus.MSP_AUDIO_SAMPLE_CONTINUE;//音频状态var ep_stat = EpStatus.MSP_EP_LOOKING_FOR_SPEECH;//端点状态var rec_stat = RecogStatus.MSP_REC_STATUS_SUCCESS;//识别状态int errcode = (int)Errors.MSP_SUCCESS;byte[] audio_content; //用来存储音频文件的二进制数据int totalLength = 0;//用来记录总的识别后的结果的长度,判断是否超过缓存最大值try{audio_content = File.ReadAllBytes(audio_path);//SoundPlayer player = new SoundPlayer(audio_path);//player.Play();}catch (Exception e){//Console.WriteLine(e);System.Diagnostics.Debug.WriteLine(e.Message);audio_content = null;}try{if (audio_content == null)throw new Exception("没有读取到任何内容");//Console.WriteLine("开始进行语音听写.......");session_id = mscDLL.QISRSessionBegin(null, session_begin_params, ref errcode);if (errcode != (int)Errors.MSP_SUCCESS)throw new Exception("开始一次语音识别失败!");int res = mscDLL.QISRAudioWrite(session_id, audio_content, (uint)audio_content.Length, aud_stat, ref ep_stat, ref rec_stat);if (res != (int)Errors.MSP_SUCCESS)throw new Exception("写入识别的音频失败!" + res);res = mscDLL.QISRAudioWrite(session_id, null, 0, AudioStatus.MSP_AUDIO_SAMPLE_LAST, ref ep_stat, ref rec_stat);if (res != (int)Errors.MSP_SUCCESS)new Exception("写入音频失败!" + res);while (RecogStatus.MSP_REC_STATUS_COMPLETE != rec_stat){IntPtr now_result = mscDLL.QISRGetResult(session_id, ref rec_stat, 0, ref errcode);if (errcode != (int)Errors.MSP_SUCCESS)throw new Exception("获取结果失败:" + errcode);if (now_result != null){int length = now_result.ToString().Length;totalLength += length;if (totalLength > 4096)throw new Exception("缓存空间不够" + totalLength);result.Append(Marshal.PtrToStringAnsi(now_result));}Thread.Sleep(150);//防止频繁占用cpu}}catch (Exception ex){System.Diagnostics.Debug.WriteLine(ex.Message);}finally{//Console.WriteLine("语音听写结束");//Console.WriteLine("听写结果: ");//Console.WriteLine(result);//int res = mscDLL.MSPLogout();//用户名,密码,登陆信息,前两个均为空}return result.ToString();}

而这时其实已经可以对官方提供的.wav测试样例进行识别了,但是我们平时若是有其他系统对接我们的接口时,比如我这次的微信小程序,会发现通过录音得来的是.mp3音频格式,科大讯飞要求的却是.wav/.pcm格式,该怎么转换音频格式?

见下一篇文章:

微信小程序+.NET(五) 音频格式转换:/jinglell/article/details/99677371

如果觉得《微信小程序+.NET(四) 科大讯飞语音接口-iat语音听写》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。