博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
微信小程序--------语音识别(前端自己也能玩)
阅读量:7104 次
发布时间:2019-06-28

本文共 2700 字,大约阅读时间需要 9 分钟。

一、背景

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

二、小程序语音识别

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

由小程序文档可知:只支持 mp3格式和 aac格式

clipboard.png

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

clipboard.png

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. 录音一定在真机上测试,模拟器不行

转载地址:http://kzuhl.baihongyu.com/

你可能感兴趣的文章
7 行代码优雅地实现 Excel 文件导出功能?
查看>>
thinkphp3.2.3 无法调用带下划线的模型
查看>>
RK 3299 Ubuntu 配置密钥
查看>>
react-hooks: CSSProperties
查看>>
abp 关闭审计日志
查看>>
迭代器模式 Iterator 行为型 设计模式(二十)
查看>>
解决walle报错:宿主机代码检出检测出错,请确认svn用户名密码无误
查看>>
svn使用openldap验证apache访问方式
查看>>
Linux下安装emacs-24.3
查看>>
二分搜索找到所在区间
查看>>
拓扑排序(topsort)
查看>>
倒要看看你有啥本事
查看>>
JavaScript高级程序设计(第三版)学习笔记22、24、25章
查看>>
【清北学堂2018-刷题冲刺】Contest 3
查看>>
CCF NOI1030 角谷猜想
查看>>
HDU1042 n!
查看>>
UVA1368 UVALive3602 ZOJ3132 DNA Consensus String【贪心】
查看>>
Linux下C编写基本的多线程socket服务器
查看>>
iOS实时监控网络状态的改变
查看>>
MFC中的CString类
查看>>