H5實現移動端語音錄製功能

依舊優雅發表於2019-02-19

前言:

年前做了一期語音口令的年度活動,從語音錄製、上傳到智慧檢測,以及後續的語音播放元件,語音錄製的實現的方式是基於微信的JSSDK,本篇主要把語音錄製板塊整理了一下,供大家參考;

下一篇-音訊元件

各位看官先上眼:

H5實現移動端語音錄製功能

功能梳理:

檢視層
  • 定義動態效果狀態標識
    區分幾種錄製狀態
    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/IkZOBiZ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DLyKHwyHJJ2I22WEdHWD+H9szvTCWDraFAAAAAElFTkSuQmCC" alt="">
      <!-- 錄製中 -->
       <div class="wave-wrap" v-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+AWJ17kH9q1j89hGeAfwEeQyIM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jEUlVyL3my6Qx6cIG+QMfRuLSe5DoiT0sX7ZTGULSlyIrXJb3PPt8d+Qvz1B36D0MS0ik4ygzEi2aBmEbTCXOI0jyzk1IpwryuRtFxGjvyxJY6BMh7/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方式元件的實現;感謝,不當之處歡迎指正交流~

相關文章