如何将微信小程序的WebSocket实时语音识别功能改写为长尾?
- 内容介绍
- 文章标签
- 相关推荐
本文共计888个文字,预计阅读时间需要4分钟。
在研究百度的实时语音识别技术后,我将其应用于微信小程序中,并撰写了一篇分享文章。以下是文章的简要内容:
成果展示:- 微信小程序成功集成百度实时语音识别功能。- 用户可通过小程序实现语音输入,提高操作便捷性。
前置条件:- 确保微信开发者工具已安装。- 获取百度AI开放平台实时语音识别key。
操作步骤:
1.申请百度AI接入:
- 访问百度AI开放平台,注册账号并申请实时语音识别服务。 - 获取API key和Secret key。2. 集成百度AI接口: - 在小程序项目中,引入百度AI SDK。 - 配置API key和Secret key。
3. 创建小程序: - 使用微信开发者工具创建一个新的小程序项目。 - 设计小程序界面和功能。
4. 设置小程序录音功能: - 在小程序中添加录音组件。 - 调用百度AI SDK进行语音识别。
5. 测试与优化: - 运行小程序,测试语音识别功能。 - 根据测试结果调整参数,优化识别效果。
总结:通过集成百度实时语音识别技术,微信小程序实现了便捷的语音输入功能,提升了用户体验。
之前在研究百度的实时语音识别,并应用到了微信小程序中,写篇文章分享一下。
先看看完成的效果吧
前置条件
申请百度实时语音识别key 百度AI接入指南
创建小程序
设置小程序录音参数
在index.js中输入
const recorderManager = wx.getRecorderManager() const recorderConfig = { duration: 600000, frameSize: 5, //指定当录音大小达到5KB时触发onFrameRecorded format: 'PCM', //文档中没写这个参数也可以触发onFrameRecorded的回调,不过楼主亲测可以使用 sampleRate: 16000, encodeBitRate: 96000, numberOfChannels: 1 }
使用websocket连接
linkSocket() { let _this = this //这里的sn是百度实时语音用于排查日志,这里我图方便就用时间戳了 let sn = new Date().getTime() wx.showLoading({ title: '识别中...' }) recorderManager.start(recorderConfig) //开启链接 wx.connectSocket({ url: 'wss://vop.baidu.com/realtime_asr?sn=' + sn, protocols: ['websocket'], success() { console.log('连接成功') _this.initEventHandle() } }) }, //监听websocket返回的数据 initEventHandle() { let _this = this wx.onSocketMessage((res) => { let result = JSON.parse(res.data.replace('\n','')) if(result.type == 'MID_TEXT'){ _this.tran(result.result, 'value') _this.setData({ textDis: 'none', value: result.result, }) } if(result.type == 'FIN_TEXT'){ let value = _this.data.text let tranStr = value + result.result _this.tran(tranStr, 'text') _this.setData({ value: '', valueEn: '', textDis: 'block', text: tranStr, }) } }) wx.onSocketOpen(() => //发送数据帧 _this.wsStart() console.log('WebSocket连接打开') }) wx.onSocketError(function (res) { console.log('WebSocket连接打开失败') }) wx.onSocketClose(function (res) { console.log('WebSocket 已关闭!') }) },
发送开始、音频数据、结束帧
wsStart() { let config = { type: "START", data: { appid: XXXXXXXXX,//百度实时语音识别appid appkey: "XXXXXXXXXXXXXXXXXX",//百度实时语音识别key dev_pid: 15372, cuid: "cuid-1", format: "pcm", sample: 16000 } } wx.sendSocketMessage({ data:JSON.stringify(config), success(res){ console.log('发送开始帧成功') } }) }, wsSend(data){ wx.sendSocketMessage({ data:data, success(res){ console.log('发送数据帧成功') } }) }, wsStop(){ let _this = this this.setData({ click: true, }) _this.stop() let config = { type: "FINISH" } wx.hideLoading() recorderManager.stop() wx.sendSocketMessage({ data:JSON.stringify(config), success(res){ console.log('发送结束帧成功') } }) },
小程序录音回调
onShow: function () { let _this = this recorderManager.onFrameRecorded(function (res){ let data = res.frameBuffer _this.wsSend(data) }) recorderManager.onInterruptionBegin(function (res){ console.log('录音中断') _this.wsStopForAcc() }) recorderManager.onStop(function (res){ console.log('录音停止') }) },
到此这篇关于微信小程序通过websocket实时语音识别的实现代码的文章就介绍到这了,更多相关微信小程序websocket实时语音识别内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!
本文共计888个文字,预计阅读时间需要4分钟。
在研究百度的实时语音识别技术后,我将其应用于微信小程序中,并撰写了一篇分享文章。以下是文章的简要内容:
成果展示:- 微信小程序成功集成百度实时语音识别功能。- 用户可通过小程序实现语音输入,提高操作便捷性。
前置条件:- 确保微信开发者工具已安装。- 获取百度AI开放平台实时语音识别key。
操作步骤:
1.申请百度AI接入:
- 访问百度AI开放平台,注册账号并申请实时语音识别服务。 - 获取API key和Secret key。2. 集成百度AI接口: - 在小程序项目中,引入百度AI SDK。 - 配置API key和Secret key。
3. 创建小程序: - 使用微信开发者工具创建一个新的小程序项目。 - 设计小程序界面和功能。
4. 设置小程序录音功能: - 在小程序中添加录音组件。 - 调用百度AI SDK进行语音识别。
5. 测试与优化: - 运行小程序,测试语音识别功能。 - 根据测试结果调整参数,优化识别效果。
总结:通过集成百度实时语音识别技术,微信小程序实现了便捷的语音输入功能,提升了用户体验。
之前在研究百度的实时语音识别,并应用到了微信小程序中,写篇文章分享一下。
先看看完成的效果吧
前置条件
申请百度实时语音识别key 百度AI接入指南
创建小程序
设置小程序录音参数
在index.js中输入
const recorderManager = wx.getRecorderManager() const recorderConfig = { duration: 600000, frameSize: 5, //指定当录音大小达到5KB时触发onFrameRecorded format: 'PCM', //文档中没写这个参数也可以触发onFrameRecorded的回调,不过楼主亲测可以使用 sampleRate: 16000, encodeBitRate: 96000, numberOfChannels: 1 }
使用websocket连接
linkSocket() { let _this = this //这里的sn是百度实时语音用于排查日志,这里我图方便就用时间戳了 let sn = new Date().getTime() wx.showLoading({ title: '识别中...' }) recorderManager.start(recorderConfig) //开启链接 wx.connectSocket({ url: 'wss://vop.baidu.com/realtime_asr?sn=' + sn, protocols: ['websocket'], success() { console.log('连接成功') _this.initEventHandle() } }) }, //监听websocket返回的数据 initEventHandle() { let _this = this wx.onSocketMessage((res) => { let result = JSON.parse(res.data.replace('\n','')) if(result.type == 'MID_TEXT'){ _this.tran(result.result, 'value') _this.setData({ textDis: 'none', value: result.result, }) } if(result.type == 'FIN_TEXT'){ let value = _this.data.text let tranStr = value + result.result _this.tran(tranStr, 'text') _this.setData({ value: '', valueEn: '', textDis: 'block', text: tranStr, }) } }) wx.onSocketOpen(() => //发送数据帧 _this.wsStart() console.log('WebSocket连接打开') }) wx.onSocketError(function (res) { console.log('WebSocket连接打开失败') }) wx.onSocketClose(function (res) { console.log('WebSocket 已关闭!') }) },
发送开始、音频数据、结束帧
wsStart() { let config = { type: "START", data: { appid: XXXXXXXXX,//百度实时语音识别appid appkey: "XXXXXXXXXXXXXXXXXX",//百度实时语音识别key dev_pid: 15372, cuid: "cuid-1", format: "pcm", sample: 16000 } } wx.sendSocketMessage({ data:JSON.stringify(config), success(res){ console.log('发送开始帧成功') } }) }, wsSend(data){ wx.sendSocketMessage({ data:data, success(res){ console.log('发送数据帧成功') } }) }, wsStop(){ let _this = this this.setData({ click: true, }) _this.stop() let config = { type: "FINISH" } wx.hideLoading() recorderManager.stop() wx.sendSocketMessage({ data:JSON.stringify(config), success(res){ console.log('发送结束帧成功') } }) },
小程序录音回调
onShow: function () { let _this = this recorderManager.onFrameRecorded(function (res){ let data = res.frameBuffer _this.wsSend(data) }) recorderManager.onInterruptionBegin(function (res){ console.log('录音中断') _this.wsStopForAcc() }) recorderManager.onStop(function (res){ console.log('录音停止') }) },
到此这篇关于微信小程序通过websocket实时语音识别的实现代码的文章就介绍到这了,更多相关微信小程序websocket实时语音识别内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!

