微信h5录音,长按录音(wx.startRecord)

本文介绍了如何在Vue2项目中实现一个H5录音功能,利用微信JSSDK进行长按录音、播放及监听播放结束的处理。在iOS和Android设备上需要注意不同平台的兼容性问题,如安卓可能出现签名失效的情况,需要避免使用vue路由跳转,改用location.href。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

嗨嗨,好久不见,自从chatgpt爆火之后,愈发不想更新文章了,因为大部分问题都可以得到解答,但是今日无事,还是记录下自己最近做的一个事吧,vue2中的录音h5,一个小活动,长按录音,播放录音,监听录音播放完毕做一些处理,接下来看代码吧

效果图

在这里插入图片描述在这里插入图片描述

前置条件

老生常谈的去注册你要使用的api接口,注册所需参数后端提供,initWechat()可以在入口处调用

import wx from 'weixin-jsapi'
import { wechatConfig } from '@/api/index'

const jsApiList = [
  'startRecord',
  'stopRecord',
  'onVoiceRecordEnd',
  'playVoice',
  'pauseVoice',
  'stopVoice',
  'onVoicePlayEnd',
  'uploadVoice',
  'downloadVoice',
  'onMenuShareAppMessage',
  'onMenuShareTimeline'
]

export function initWechat() {
  wechatConfig({
    url: encodeURIComponent(location.href)
  }).then((res) => {
    if (res.code === 200) {
      // <!--通过config接口注入权限验证配置-->
      wx.config({
        debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
        appId: res.data.appId, // 必填,公众号的唯一标识
        timestamp: res.data.timestamp, // 必填,生成签名的时间戳
        nonceStr: res.data.nonceStr, // 必填,生成签名的随机串
        signature: res.data.signature, // 必填,签名
        jsApiList: jsApiList, // 必填,需要使用的JS接口列表
        success: () => {}
      })
    }
  })
}

录音代码

长按录音,松开添加到录音列表,每段录音微信api最长支持60s

<div class="long-tap-btn" @touchstart="onStartRecording" @touchend="stopRecording">
   <img class="icon" @click.prevent>
   <div v-show="isRecording" class="record">
     <div class="item item1" />
     <div class="item item2" />
     <div class="item item3" />
     <div class="item item4" />
     <div class="item item3" />
     <div class="item item2" />
     <div class="item item1" />
     <div class="time">{{ btnDuration }}s</div>
   </div>
   <span v-show="!isRecording" class="btn-txt">长按录制语音</span>
 </div>
// 长按录音
onStartRecording(event) {
 event.preventDefault()
 clearTimeout(this.timeOutEvent)
 clearInterval(this.btnDurationTimer)
 this.btnDuration = 1
 
 // 手指按住500ms才算长按
 this.timeOutEvent = setTimeout(() => {
   this.timeOutEvent = 0
   this.isRecording = true
   this.recordStartTime = new Date().getTime()
   wx.startRecord()
   // 监听录音结束事件 超过60s自动执行
   wx.onVoiceRecordEnd({
     complete: res => {
       clearTimeout(this.timeOutEvent)
       this.onRecorded(res.localId)
       this.timeOutEvent = 1
     }
   })
   // 为了添加具体录制多长时间,微信未提供根据localId获取时长,所以用定时器实现
   this.btnDurationTimer = setInterval(() => {
     this.btnDuration++
     if (this.btnDuration >= 60) clearInterval(this.btnDurationTimer)
   }, 1000)
 }, 500)
 return false
},
//停止录音
stopRecording() {
 clearTimeout(this.timeOutEvent)
 const { timeOutEvent } = this
 this.isRecording = false
 clearInterval(this.btnDurationTimer)

 if (timeOutEvent === 0) {
   wx.stopRecord({
     success: res => {
       this.onRecorded(res.localId)
     }
   })
 }
 return false
},
// 录制结束逻辑处理
onRecorded(localId) {
  const { recordStartTime } = this
  const endTime = new Date().getTime()
  const arrItem = {
    localId,
    duration: (endTime - recordStartTime) / 1000
  }
  if (arrItem.duration < 1) {
    Toast('录制时间太短,请重新录制')
    return
  }
  if (arrItem.duration >= 60) {
    arrItem.duration = 60
  } else {
    arrItem.duration = Math.ceil(arrItem.duration)
  }
  // 后端需要的的是serverId,localId仅作为当前页面即时播放和转换serverId
  wx.uploadVoice({
    localId,
    isShowProgressTips: 0,
    success: res => {
      arrItem.url = res.serverId
      this.voiceList.push(arrItem)
    }
  })
  this.isRecording = false
},

播放录音,暂停录音

// tips 离开页面或者删除时记得判断当前录音是否还在播放中 播放的话需要停止播放
// 播放录音
 onPlayVoice(item, index) {
   this.currentPlayLocalId = item.localId
   if (index === this.currentPlay) {
     wx.pauseVoice({
       localId: item.localId
     })
     this.currentPlay = -1
   } else {
     wx.playVoice({
       localId: item.localId
     })
     this.currentPlay = index
     wx.onVoicePlayEnd({
       success: () => {
         this.currentPlay = -1
       }
     })
   }
 },
 // 删除录音
 onDelVoice(index) {
   const { stepData, currentStep } = this
   stepData[currentStep].voiceList.splice(index, 1)
   this.stopPlayHandle()
 },
 stopPlayHandle() {
   this.currentPlay = -1
   wx.stopVoice({
     localId: this.currentPlayLocalId
   })
 }

到这差不多就结束啦,在vue中使用微信jssdk还是有点坑的,不过按部就班的还是很简单的…

彩蛋

对接的时候遇到了在ios正常,但是在安卓会出现调用录音api时,签名失效,wx.config和其他的都是正常的,排查半天想起来之前做支付遇到过类似问题,在录音或者支付的时候,前置页面不能使用vue路由进行跳转,需要使用location.href进行跳转,下图是gpt的解释:
在这里插入图片描述

用到的动画 wifi播放和波浪录制动效

播放(wifi)
在这里插入图片描述
在这里插入图片描述
波浪

 .item {
   width: 8px;
   background-color: #fff;
   margin: 0 5px;
   border-radius: 10px;
   animation: volume 1s linear infinite;
   &.item1 {
     animation-delay: 0s;
   }
   &.item2 {
     animation-delay: 0.1s;
   }
   &.item3 {
     animation-delay: 0.2s;
   }
   &.item4 {
       animation-delay: 0.1s;
   }
 }
 @keyframes volume {
  0% {
    height: 20%;
  }
  25% {
    height: 45%;
  }
  50% {
    height: 65%;
  }
  75% {
    height: 45%;
  }
  100% {
    height: 20%;
  }
}

完结撒花🎉

其实作为H5想要做语音识别,自认为还说有各种弊端得,同时还是微信公众号里面,如果小程序得话,或许会简单一点,但是这里是在公众号里面开发,在这个过程中查阅个各种资料,其实里面得东西都大同小异,但是大多数并不全, 首先微信公众号里面得H5开发语音录入,试过各种方法,由于是java渣渣,页面技术只是了解简单得,因此在使用标签得时候,本身并不能适用于当前得需求,因此最后只能选择了微信自带得录音功能,使用微信提供的接口,(具体得接口使用还是去看微信接口得使用) 由于使用微信得接口得话,首先要上传到它得服务器上面去(虽然也有本地得文件储存id,但是好像并没有用),这里就涉及到了一个serverid,这个值是存在于微信临时素材库得id,后期要用它来获取录音得文件。 通过微信提供得获取临时素材文件得接口,得到了文件(具体方法网上一搜一大堆,后期我也会将完整得代码放进资源里面),但是这里有一个坑得问题,他的格式为.amr得格式,但是最后的目的是转换为文字,因此这里涉及到了格式转换 主要用了现成得技术,也是通过下载资源获取得jar以及实现得方法,将.amr转换为了MP3格式 然后就就是最后一部了,将mp3得音频文件转换为文字,我这里用的是讯飞得技术,当然百度得也可以, 其实整个需求得这个流程已经完成了,但是不得不说里面遇到得坑,由于是渣渣,除了人们总说的,转换为mp3得时候会报N/A得错误,我还遇到了unkowFormat这个错误。 这里只是记录一下自己在查询资料时没有一个符合自己需求得资料,同时也希望以后有这样需求得人,可以将思路捋的更加清楚,也可以进行交流.
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值