小程序 语音识别(一)
来源:互联网 发布:传感器网络的安全问题 编辑:程序博客网 时间:2024/04/29 21:14
由于最近项目中需要实现小程序端的语音识别,所以写了一个例子,供需要的同学参考。下面贴下代码
小程序端代码:
<!--index.wxml--><view class="container"> <view class="userinfo"> <button wx:if="{{!hasUserInfo && canIUse}}" open-type="getUserInfo" bindgetuserinfo="getUserInfo"> 获取头像昵称 </button> <block wx:else> <image bindtap="bindViewTap" class="userinfo-avatar" src="{{userInfo.avatarUrl}}" background-size="cover"></image> <text class="userinfo-nickname">{{userInfo.nickName}}</text> </block> </view> <!-- <view class="usermotto"> --> <button type="default" bindtap='startLy' style='margin-top:10rpx;color:green;' disabled='{{disabled1}}' loading='{{loading1}}'>录音</button> <button type="default" bindtap='endLy' style='margin-top:10rpx;color:red;'>结束</button> <button type="default" bindtap='upload' style='margin-top:10rpx;color:blue;' disabled='{{disabled}}' loading='{{loading}}'>识别</button> <view>识别结果:{{res}}</view> <!-- </view> --></view>
//index.js//获取应用实例const app = getApp()const recorderManager = wx.getRecorderManager()recorderManager.onStart(() => { console.log('recorder start')})recorderManager.onResume(() => { console.log('recorder resume')})recorderManager.onPause(() => { console.log('recorder pause')})recorderManager.onStop((res) => { console.log('recorder stop', res) const { tempFilePath } = res})recorderManager.onFrameRecorded((res) => { const { frameBuffer } = res console.log('frameBuffer.byteLength', frameBuffer.byteLength)})const options = { //duration: 10000, sampleRate: 16000,//采样率 numberOfChannels: 1, encodeBitRate: 96000,//编码码率 format: 'mp3', frameSize: 50}const innerAudioContext = wx.createInnerAudioContext()var tempPath = "";Page({ data: { motto: 'Hello World', userInfo: {}, hasUserInfo: false, canIUse: wx.canIUse('button.open-type.getUserInfo'), res:'', loading: false, disabled: false, loading1: false, disabled1: false }, //事件处理函数 bindViewTap: function() { wx.navigateTo({ url: '../logs/logs' }) }, onLoad: function () { if (app.globalData.userInfo) { this.setData({ userInfo: app.globalData.userInfo, hasUserInfo: true }) } else if (this.data.canIUse){ // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回 // 所以此处加入 callback 以防止这种情况 app.userInfoReadyCallback = res => { this.setData({ userInfo: res.userInfo, hasUserInfo: true }) } } else { // 在没有 open-type=getUserInfo 版本的兼容处理 wx.getUserInfo({ success: res => { app.globalData.userInfo = res.userInfo this.setData({ userInfo: res.userInfo, hasUserInfo: true }) } }) } }, getUserInfo: function(e) { console.log(e) app.globalData.userInfo = e.detail.userInfo this.setData({ userInfo: e.detail.userInfo, hasUserInfo: true }) }, startLy: function(e){ var that = this that.setData({ loading1: true }); that.setData({ disabled1: true }); recorderManager.start(options) }, endLy: function(e){ var that = this that.setData({ loading1: false }); that.setData({ disabled1: false }); recorderManager.stop(); recorderManager.onStop((res) => { console.log('recorder stop', res) tempPath = res.tempFilePath; console.log('录音地址' + tempPath) }) }, upload: function(){ var that = this that.setData({ loading: true}); that.setData({ disabled: true}); wx.uploadFile({ //url: 'http://192.168.30.3:8080/gt_store_checking_in/voice/upload.action', //仅为示例,非真实的接口地址 url: 'http://192.168.30.25:8080/gt_store_checking_in/voice/upload.action', //仅为示例,非真实的接口地址 filePath: tempPath, name: 'file', formData: { 'user': 'test' }, success: function (res) { var data = res.data that.setData({res: data}) //do something console.log('succ',data) that.setData({ loading: false }); that.setData({ disabled: false }); }, fail: function(res){ that.setData({ loading: false }); that.setData({ disabled: false }); console.log('fail',res.errMsg) } }) } })
后端使用java+百度语音识别+ffmpeg格式转换,后端带面,见小程序 语音识别(二)
阅读全文
0 0
- 小程序 语音识别(一)
- 小程序 语音识别(二)
- 小程序 语音识别(三)
- 语音识别-小娜
- Android 开发基于百度语音识别技术的小程序
- 小程序 wx.getRecorderManager 录音 to 语音识别
- android语音识别方法一:使用intent调用语音识别程序
- android语音识别方法一:使用intent调用语音识别程序
- android语音识别方法一:使用intent调用语音识别程序
- android语音识别方法一:使用intent调用语音识别程序
- android 语音识别程序
- Unity 中使用百度语音识别发布Android程序(一)
- 语音合成与语音识别小例子
- 小词汇量的语音识别
- android 语音识别 语音手电(一)
- 微信小程序语音识别探索
- 微信小程序 语音识别开发
- 语音识别原理--理论基础(一)
- echarts基本地图
- jquery.tmpl详解
- 第6章 Unity 中的基础光照
- 5-3 多级派生类的构造函数
- 生产者消费者实例!!!
- 小程序 语音识别(一)
- 枚举 编程题#1: 画家问题(Coursera 程序设计与算法 专项课程4 算法基础 郭炜、刘家瑛;二进制实现)
- 龙与地下城游戏问题
- 使用jquery ajax jsop gpsspg api转换坐标
- jquery validation addMethod 自定义方法
- MySQL 存储过程和函数
- Day014
- 普及练习场 贪心 凌乱的yyy
- Java中的static关键字解析