微信公众号h5网页-调用录音评测

一、全局引入jweixin-1.6.0.js微信能力插件;

二、初始化微信配置,开启音频相关能力;

initWxConfig() {
      let url = window.location.href.split('#')[0]
      let data = { url: url }
      this.$http.get(this.$api.getTicket, { params: data }).then((res) => {
        let data = res.data || {}
        if (data.flag) {
          this.$nextTick(() => {
            wx.config({
              debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
              appId: data.appId,
              timestamp: data.timestamp,
              nonceStr: data.noncestr,
              signature: data.signature,
              jsApiList: ['startRecord', 'stopRecord', 'playVoice', 'stopVoice', 'uploadVoice', 'translateVoice']
            })
          })
        }
      })
    }, 

三、录音上传评测;

wx.startRecord();
let _this = this
        wx.stopRecord({
          success: function (res) {
            _this.localId = res.localId
            wx.uploadVoice({
              localId: res.localId, // 需要上传的音频的本地ID,由stopRecord接口获得
              isShowProgressTips: 1, // 默认为1,显示进度提示
              success: function (res) { 
                _this.serverId = res.serverId // 返回音频的服务器端ID
                let params = {
                  questionId: _this.paper.id,
                  mediaId: res.serverId,
                  paperId: _this.paper.paperId
                }
                // loading
                _this.$http
                  .post(_this.$api.audioCheck, params)
                  .then((res) => { 
                    _this.nextFlag = true //通过
                  })
                  .finally(() => {
                    Toast.clear()
                  })
              }
            })
          }
        })

四、应用置于后台,定时器、延时器由于浏览器性能机制有所沉睡;采用visibilitychange及时处理;

document.removeEventListener("visibilitychange", this.checkViChange);
checkViChange(){ 
      if (document.hidden) { // 隐藏
        this.cacheHiddenTime = moment().unix();
        if(this.setInterval){
          clearInterval(this.setInterval); 
          this.setInterval = null;
        };
      }else{
        let cur = moment().unix() - this.cacheHiddenTime;
        this.countdown = this.countdown>cur ? (this.countdown-cur) : 0; 
        this.countDownUtil();
      }
    },

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值