H5实现移动端语音录制功能

·  阅读 7626
H5实现移动端语音录制功能

前言:

年前做了一期语音口令的年度活动,从语音录制、上传到智能检测,以及后续的语音播放组件,语音录制的实现的方式是基于微信的JSSDK,本篇主要把语音录制板块整理了一下,供大家参考;

下一篇-音频组件

各位看官先上眼:

功能梳理:

视图层
  • 定义动态效果状态标识
    区分几种录制状态
    isAudioState: 0 // 0 未录制or检测完毕 1 录制中 2 检测中
    :未录制or检测完毕     / 0 无动效
    :录制中               / 1 波浪纹
    :检测中               / 2 环形检测
    复制代码
  • 设置动态效果切换状态
    通过监听touchstart/touchend事件去切换动态效果
    复制代码
逻辑层
 录制、上传、检测功能
 时间限制(时间区间)、内容为空的兼容处理
复制代码
  • touchstart事件中做了哪些事
    1.开启录音
    2.切换录音中状态
    3.开始计时,用于记录音频时长
    复制代码
  • touchend事件中做了哪些事
    1.停止录音
    2.智能检测
    3.控制录音时长
    4.上传音频
    5.切换录音结束状态(是否开始检测)
    复制代码
USE API(JS-SDK)
 开始录制 startRecord
 停止录制 stopRecord
 智能检测 translateVoice (将语音转文字)
 上传音频 uploadVoice (将录制的临时音频文件上传至微信服务,供服务)
复制代码

这里就不赘述了,可以去猫一眼微信的官方文档 点击前往

代码层:

 注:所有逻辑均在此组件,父级组件引入即可
 
<template>
 <div>
   <div class="audio-img" @touchstart="audioStart" @touchend="audioEnd">
       <!-- 未录制 -->
       <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIwAAACMCAYAAACuwEE+AAAZ+ElEQVR4nO2deZRcVZ3HP+9V9Zp00p2lkiCRTshC0AAhCwRUHMFR5hxQwowDZ3BBOSKyKKIMq6i4I3DcWAbCnBE5o0TEwREVwWVYDEmgMYSwphNZsnTSnU466U66u96dP269V2+5971X1dXV9Sr1O6eq3nv3d393+9b3/u7y3jOEEKika/Gi/IkZOBiZGM5XOWU8cDQwC2jPfaYDk3OfKUAdkAZacnH6gGFgCOgGduV+twNbcp/NwEZgXxnKUDbJrFmnvJ6OFdty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-show="isAudioState === 1">
         <div class="wave wave-1"></div>
         <div class="wave wave-2"></div>
         <div class="wave wave-3"></div>
       </div>
       <!-- 校验中 -->
       <div class="roate-wrap" v-show="isAudioState === 2">
         <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAALYAAAC1CAYAAADyZAWqAAAUFUlEQVR4nO2da6xc11XHfzN3bF/n+pX4EduxncSJE4LbxE2apm1oS4tooKAALc8IoSIeFQIB4SU+gBBPlQ/AB8SrFCiqQKWAII1AvNoqjVqJtFAalDQ4aZ3YjuNn7MTx6947M3xYZ/vs2bPPOfu8Z+aun3Q0c885c86+9/5nnbXXXnvtzom7Xo8SxDxwE7A3et0DbAO2AJujbR1wTXT+eqAXvb8EXAYuRtsZ4ATwInASeCnaDgHPRec2zrb/ebKN29ZCL/uUFcle4I3A66PtTkTMnYLXWxtt10Y/74teh55zh8Bh4CngyWh7AvhqwXuvSFTY0AXuBr4OeDvwFmB7i+3pADdG23us/aeBzwGfibb/BgaNt25KWKnC3gI8AHwz8G5ga7vNCWIL8GC0gbgw/xxt/w680lK7JpKVJOzNwHuB7wLeBcy125zSbAPeH21XgM8CfwP8PfBya62aELptN6Bm1iBC/iekc/Yh4BuZflG7rAbeCfwxcBT4W8Syr2qzUW0yqxb7NuBHEWt2HcU7fdNCh7gjugZ5Mr0Xsdx/Dvwh8EI7TWuHWbPYbwP+EXga+FnE/Zh1UYM/ugLypf454Fng40gHeUUwCxa7g1innwfeVPO9TiBht+ej7Wi070y0nQUuROe+BixF79cg8e110bYZ6QzuAHYiMfGvAW4HNtTQ7jngfdH2X8BvAY+Q/IWYeqZZ2B3gW4FfR+LMLkPrvCIcRMJrXwL+N3o9XfBaV6LtbMC5NwB3APuRMOS9iODTfg/bFcniHuDvkHDhryL9j5mjM6Ujj9+AWJ17kH9q1j89hGeAfwEeQyIMp8o0sGI2A/cDXw98E2LdXfJYX/vc/0Sedp/Tkcf2uAX4beDbK7jWEhL/fRQR9PMVXLMuzgCfiLafAW5FBP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bTCXOI0jyzk1IpwryuRtFxGjvyxJY6BMh7/3zfDlCz11E/p5HmLEY/zQJ27CMdDAPIUPFe0lfvrhqQpOFqvySpX0u67O+L+gF5Cn4EjNioV2mUdiGAWJpjiKdzBuRuG7e+KxNUXFl5S/nEXnReh1JSUtD6/Vl5G92puB9poZpFrZhiIwonkSiCruRBKt5mvnnVS3aon2GpNDfItI/OcYUhu2KMgvCtrmEhKmeRaz3DiR3IvT3LOIa5Om8hYq2zJMExF8+RZw+MNPW2cesCdswRP6xp5AY8jZE4FvxL1Dvo2y0pWwkJe9nlhARn4heZ6ozmJdZFbbNAMlDOY4IZCMi9K3IyF7IdDUo7n+HRFLy3MP+wphUgNPISOmKs8xJrARh2wyRXJBziMuyCkkiug4R/EaSE4rKWPCiIT773AEi3nPWppMpElhpwnZZQh7dJlm+g1jxjdHrOiSUOG8dh/ot4xUkP+XV6NXkq6hFDmSlC9vFPN7dYuRdJOJyTfQ6T5zrYV67xP57lzg7sU8cK16K3i8S55dcid7beSgr2j+ugv8HWZJZa/fw8nsAAAAASUVORK5CYII=" alt="">
       </div>
      </div>
      <!-- 状态 -->
     <div class="audio-text">{{isAudioState === 0 ? '长按开始' : isAudioState === 1 ? '录制中...' : '识别中...'}}</div>
 </div>
</template>

<script>
let vm = null
import _wx from '../../../static/wx.js' // JS-SDK
require('es6-promise').polyfill()
export default {
  data () {
    return {
      isAudioState: 0, // 0 未录制or检测完毕 1 录制中 2 检测中
      secondNum: 0 // 音频时长
    }
  },
  filters: {
  },
  created: function () {
    vm = this
  },
  methods: {
    audioStart (e) {
      // 开始录制
      e.preventDefault() // 解决touch时触发下拉 松开后无法touchend的情况
      _wx.startRecord()
      this.count_time()
      this.isAudioState = 1
    },
    audioEnd () {
      // 结束录制
      vm.isAudioState = 0
      clearInterval(this.setIvWrap)
      vm.isAudioState = 2 // 方便本地观测 动态 测试通过后注释即可
      var params = {
        async success (res) {
          this.limit_time() // 时间限制
          vm.isAudioState = 2 // 实际检测动效
          const testdata = await vm.check_text(res)
          if (!testdata.translateResult) { // 未检测语音
            console.log('人家没听明白你说啥,再读一下')
            return
          }
          _wx.uploadVoice({
            localId: res.localId,
            isShowProgressTips: 0,
            success (updata) {
              console.log('上传成功')
            }
          })
        }
      }
      _wx.stopRecord(params)
    },
    check_text (res) {
      // 智能校验
      return new Promise((resolve, reject) => {
        _wx.translateVoice({
          localId: res.localId,
          isShowProgressTips: 0,
          success (testdata) {
            resolve(testdata)
          }
        })
      })
    },
    count_time () {
      // 计算音频时长
      clearInterval(this.setIvWrap)
      this.secondNum = 0
      this.setIvWrap = setInterval(() => {
        this.secondNum ++
        if (this.secondNum >= 15) {
          this.audioEnd()
        }
      }, 1000)
    },
    limit_time () {
      // 音频时长限制
      if (vm.secondNum >= 15 || vm.secondNum <= 3) {
        console.log(vm.secondNum ? '最多录制15s' : '录制时长不得小于3s')
        return
      }
    }
  },
  mounted () {
  },
  components: {
  }
}
</script>

<style lang="scss">
img{ pointer-events: none; } // 取消某些浏览器的默认事件
.audio-img{
  width: 1.4rem;
  height: 1.4rem;
  margin: 0 auto;
  position: relative;
  img{
    width: 100%;
    height: 100%;
    position: absolute;
    left: 0;
    top: 0;
    z-index: 1;
  }
  .mark-image{
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;height: 100%;
    z-index: 1;
  }
  .wave-wrap{
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;height: 100%;
    z-index: 0;
    .wave{
      position: absolute;
      left: 1%;
      top: 1%;
      width: 98%;
      height: 98%;
      border-radius: 50%;
      z-index: 1;
    }
    .wave-1{ 
      background: #d6382f;
      animation: scale-wave1 2s linear infinite;    
    }
    .wave-2{
      background: #e37f6a;
      animation: scale-wave2 2s linear infinite;
    }
    .wave-3{ 
      background: #e37f6a;
      animation: scale-wave2 2s 1s linear infinite;    
    }
  }
  .roate-wrap{
    position: absolute;
    left: -0.21rem;
    top: -0.21rem;
    width: 100%;
    height: 100%;
    padding: 0.25rem;
    z-index: 0;
    animation: roate-test 2s linear infinite;
    -webkit-animation: roate-test 2s linear infinite;
  }
}
.audio-text{
  margin-top: 0.3rem;
  color: #fff;
  text-align: center;
}
@keyframes scale-wave1 {
  0%{
   opacity: 1;
   transform: scale(1)
  }
  100%{
    transform: scale(1.35);
    opacity: 0;
  }
}
@keyframes scale-wave2 {
  0%{
    opacity: 1;
    transform: scale(1)
  }
  100%{
    opacity: 0;
    transform: scale(1.9)
  }
}

@keyframes roate-test {
  0%{
   transform: rotate(0);
  }
  100%{
   transform: rotate(360deg);
  }
}
</style>
复制代码

注意事项:

  • 上述逻辑中均为同步操作,以应对服务端异步获取当次录音的音频文件(目的在于需确报本次音频上传成功后再与服务端交互);
  • 微信音频格式为speex,需转格;
  • 部分浏览器存在touch img呈现预览模式的解决方案,img{ pointer-events: none; };
  • audioStart时,部分浏览器存在用户同时触发下拉动作后松手无法touchend的情况,需preventDefault操作;
  • 米8在触发上传、检测api时,会造成检测动画的卡顿,(其他机型没问题),初步怀疑是线程阻塞,有兴趣的可以一块研究一下~(方案:动画轨迹被阻塞/dom渲染被打断(gif),所以回调后,可以使用时间戳的方式重新绘制图片;其他类似场景也可以使用这种方案,当然针对性场景,具体情况还要重新定义~)

服务端针对微信speek格式音频优化

针对微信的speex高清音频格式,服务端的小可爱做了以下处理; 处理音频为前端可识别格式wav,并转格为mp3,并再次压缩;友好前端格式、提高的效率的同时又极大的节约了成本;有兴趣的同学可以去看一下, 作者:purewater2014,微信高清音频处理链接

结语:

本篇简要概述了H5语音的录制模块,下一篇说一下本次活动中,类微信语音列表的render方式组件的实现;感谢,不当之处欢迎指正交流~

分类:
前端
标签:
分类:
前端
标签:
收藏成功!
已添加到「」, 点击更改