小程序 语音识别(一)

来源:互联网 发布:传感器网络的安全问题 编辑:程序博客网 时间: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格式转换,后端带面,见小程序 语音识别(二)