小程序录音功能(包括授权,倒计时,暂停功能)

首先判断权限

getPermission: function() {
    var that = this;
        wx.getSetting({
          success(res) {
            console.log(res.authSetting)
            if (res.authSetting["scope.record"] === false) {
              wx.showModal({
                title: '是否录音',
                content: '是否录音',
                success: function (tip) {
                  if (tip.confirm) {
                    wx.openSetting({
                      success: function (data) {
                        if (data.authSetting["scope.record"] === true) {
                          wx.showToast({
                            title: '授权成功',
                            icon: 'success',
                            duration: 1000
                          })
                          that.startLuYin()
                          //授权成功之后,再调用chooseLocation选择地方
                        } else {
                          wx.showToast({
                            title: '授权失败',
                            icon: 'success',
                            duration: 1000
                          })
                        }
                      }
                    })
                  }
                }
              })
            }else{
              that.startLuYin()
            }
          }
        })
  },

授权成功后开始录音

startLuYin(){
    const options = {
      duration: 10000 * 6 * 10, //指定录音的时长,单位 ms
      sampleRate: 16000, //采样率
      numberOfChannels: 1, //录音通道数
      encodeBitRate: 96000, //编码码率
      format: 'mp3', //音频格式,有效值 aac/mp3
      frameSize: 50, //指定帧大小,单位 KB
    }
    //开始录音
    recorderManager.start(options);
    recorderManager.onStart(() => {
      console.log('recorder start');
      Countdown(this); //开始计时
    });
    //错误回调
    recorderManager.onError((res) => {
      console.log('recorder出错:' + res);
      console.log(res);
      clearTimeout(timer); //出错时停止计时
    })
  },

暂停录音

// 暂停录音
  pause: function() {
    var that = this;
    recorderManager.pause()
    recorderManager.onPause((res) => {
      console.log(res)
      console.log('暂停录音')
      clearTimeout(timer);
    })
  },

继续录音

//继续录音
  jixu: function() {
    var that = this;
    recorderManager.resume()
    Countdown(that); //开始计时
    recorderManager.onResume((res) => {
    })
  },

停止录音

//停止录音
  stop: function() {
    recorderManager.stop();
    recorderManager.onStop((res) => {
      this.tempFilePath = res.tempFilePath;
      console.log('停止录音', res.tempFilePath)
      clearTimeout(timer);
    })
  },

播放声音

  //播放声音
  play: function() {
    innerAudioContext.autoplay = true
    innerAudioContext.src = this.tempFilePath,
      innerAudioContext.onPlay(() => {
        console.log('开始播放')
      })
    innerAudioContext.onError((res) => {
      console.log(res.errMsg)
      console.log(res.errCode)
    })
  },
// 倒计时
function Countdown(that) {
  timer = setTimeout(function() {
    console.log("----secondes----" + formatSeconds(secondes));
    secondes++;
    if (secondes >= 600) {
      recorderManager.stop();
      clearTimeout(timer);
    }
    that.setData({
      times: formatSeconds(secondes)
    });
    Countdown(that);
  }, 1000);
};
// 时间展示
function formatSeconds(value) {
  var secondTime = parseInt(value); // 秒
  var minuteTime = 0; // 分
  var hourTime = 0; // 小时
  if (secondTime > 60) { //如果秒数大于60,将秒数转换成整数
    //获取分钟,除以60取整数,得到整数分钟
    minuteTime = parseInt(secondTime / 60);
    //获取秒数,秒数取佘,得到整数秒数
    secondTime = parseInt(secondTime % 60);
    //如果分钟大于60,将分钟转换成小时
    if (minuteTime > 60) {
      //获取小时,获取分钟除以60,得到整数小时
      hourTime = parseInt(minuteTime / 60);
      //获取小时后取佘的分,获取分钟除以60取佘的分
      minuteTime = parseInt(minuteTime % 60);
    }
  }
  var result;
  //时间的展示方式为00:00
  if (secondTime < 10) {
    result = "0" + parseInt(secondTime);
  } else {
    result = "" + parseInt(secondTime);
  }
  if (minuteTime > 0) {
    if (minuteTime < 10) {
      result = "0" + parseInt(minuteTime) + ":" + result;
    } else {
      result = "" + parseInt(minuteTime) + ":" + result;
    }
  } else {
    result = "00:" + result;
  }
  //由于限制时长最多为三分钟,小时用不到
  if (hourTime > 0) {
    result = "" + parseInt(hourTime) + ":" + result;
  }
  return result;
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现订单倒计时功能可以使用小程序的定时器功能,具体实现步骤如下: 1. 在订单详情页面,获取订单创建时间和订单过期时间。 2. 计算当前时间与订单过期时间的时间差,并将时间差转换为倒计时显示格式(例如:00:00:00)。 3. 使用小程序的定时器功能,每隔一秒更新倒计时显示。 4. 当倒计时结束,即订单过期时间到达时,提示用户订单已过期。 以下是示例代码: ``` // 获取订单创建时间和订单过期时间 const createTime = new Date(order.create_time); const expiredTime = new Date(createTime.getTime() + order.expires_in * 1000); // 计算倒计时时间差 let diffTime = Math.ceil((expiredTime.getTime() - Date.now()) / 1000); let hour = Math.floor(diffTime / 3600); let minute = Math.floor((diffTime - hour * 3600) / 60); let second = diffTime - hour * 3600 - minute * 60; let countDown = `${hour < 10 ? '0' + hour : hour}:${minute < 10 ? '0' + minute : minute}:${second < 10 ? '0' + second : second}`; // 更新倒计时显示 this.setData({ countDown: countDown }); this.timer = setInterval(() => { diffTime--; hour = Math.floor(diffTime / 3600); minute = Math.floor((diffTime - hour * 3600) / 60); second = diffTime - hour * 3600 - minute * 60; countDown = `${hour < 10 ? '0' + hour : hour}:${minute < 10 ? '0' + minute : minute}:${second < 10 ? '0' + second : second}`; this.setData({ countDown: countDown }); if (diffTime <= 0) { clearInterval(this.timer); wx.showToast({ title: '订单已过期', icon: 'none' }) } }, 1000); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值