微信小程序--------语音识别(前端自己也能玩)

发布时间:2019-06-10 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了微信小程序--------语音识别(前端自己也能玩)脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

一、背景

作为一名前端同学有时候感觉挺可怜的,复杂的操作都依赖后端同学在服务器端完成。那么,有一天我们自己想玩一个新技或者后端同学不搭理我们,怎么办?绝望中.....

二、小程序语音识别

 接到这个需求,我们明确两个问题:
  1. 小程序录音支持什么格式

由小程序文档可知:只支持 mP3格式和 aac格式
微信小程序录音文档

微信小程序--------语音识别(前端自己也能玩)

  1. 科大讯飞平台需要什么格式的音频
    支持的格式 pacm或者wav, sPEex和 speex-web 格式
    科大讯飞语音听写api

微信小程序--------语音识别(前端自己也能玩)

3. 目标 将小程序的录音转为 科大讯飞能识别的音频格式

  import Mp3 from '@/utils/js-mp3/decode'
  import { md5 } from '@/utils/md5.js'
  import pcm from 'pcm-util'
录音
    // 获取录音权限
    this.getRecordAuth()
    // 获取录音对象
    const that = this;
    this.recorderManager = wx.getRecorderManager()
    this.recorderManager.onStart(() => {
      console.log('recorder start')
    })
    // 录音的格式参数
     const options = {
      duration: 11000,
      sampleRate: 32000,
      numberOfChannels: 1,
      encodeBitRate: 64000,
      format: 'mp3',
      frameSize: 6
    }
    this.recorderManager.start(options)
    this.recorderManager.onStop(res => {
      const tempFilePath = res.tempFilePath
      that.duration = res.duration
      const fs = wx.getFileSystemManager()
      console.log('record stop')
      console.log(res)
      // 从临时文件中读取音频
      fs.readFile({
        filePath: tempFilePath,
        success (res) {
          console.log('read success')
          that.mp3ToPcm(res.data)
        },
        fail (e) {
          console.log('read fail')
          console.log(e)
        }
      })
    })


转格式
mp3ToPcm (mp3AB) {
    var that = this
    var decoder = Mp3.newDecoder(mp3AB)
    var pcmArrayBuffer = decoder.decode()
    // 和录音的格式一样
    const fromFormat = {
      channels: 1,
      sampleRate: 32000,
      interleaved: true,
      float: false,
      samplesPerFrame: 1152,
      signed: true
    }
    // 目标音频的格式
    const toFormat = {
      channels: 1,
      sampleRate: 16000,
      bitDepth: 8,
      interleaved: true,
      float: false,
      samplesPerFrame: 576,
      signed: true
    }
    var pcmAB = pcm.convert(pcmArrayBuffer, fromFormat, toFormat)
    const base64 = wx.arrayBufferToBase64(pcmAB)
    var millTime = (new Date().setMilliseconds(0) / 1000) + ''
    /** 调用科大讯飞平台的语音识别
        请求参数都是自己申请应用的参数
    */
    wx.request({
      url: 'http://api.xfyun.cn/v1/service/v1/iat',
      method: 'POST',
      data: {
        audio: base64
      },
      header: {
        'X-Appid': '5be4162d',    
        'X-CurTime': millTime,
        'X-Param': 'eyJlbmdpbmVfdHlwZSI6ICJzbXMxNmsiLCJhdWUiOiAicmF3In0=',
        'X-CheckSum': md5('b243cb9e1ea9d9eb40847967a8ebeef2' + millTime + 'eyJlbmdpbmVfdHlwZSI6ICJzbXMxNmsiLCJhdWUiOiAicmF3In0='),
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success (res) {
        console.log('turn success')
        console.log(res)
        console.log(res.data)
      },
      fail: function (res) {
        console.log('turn fail')
        console.log(res)
      }
    })
  }
},

注意:

  1. 首先在科大讯飞平台申应用申请应用
  2. 请求参数的文档语音识别的接口文档
  3. 录音一定在真机上测试,模拟器不行

js-mp3
pcm工具包

脚本宝典总结

以上是脚本宝典为你收集整理的微信小程序--------语音识别(前端自己也能玩)全部内容,希望文章能够帮你解决微信小程序--------语音识别(前端自己也能玩)所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。