求助RecorderManager录音自写计时有问题,真机调试暂停时间后计时会双倍?
发布于 6 年前 作者 lijun 11503 次浏览 来自 问答

求助RecorderManager录音自写计时有问题,暂停后时间会双倍还有一个问题就是删除后重新开始

菜鸟求指点!!!

//js
let RM = wx.getRecorderManager();
const innerAudioContext = wx.createInnerAudioContext()
var timer;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    url'',
    starttrue,
    pausefalse,
    resumefalse,
    stopfalse,
    playfalse,
    completefalse,
    deletefalse,
    times'0min 0s',
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoadfunction () {},
  onShowfunction () {
    RM.stop();
  },
  //开始录音
  onStartfunction () {
    wx.showToast({
      title'开始录音',
      icon'none'
    })
    this.setData({
      startfalse,
      pausetrue,
      stoptrue,
      deletefalse,
      completefalse
    })
    let option = {
      duration600000// 10 minutes
      sampleRate48000,
      numberOfChannels2,
      encodeBitRate320000,
      format'mp3' //录音的格式,有aac和mp3两种 
    }
    RM.start(option);
    RM.onStart(() => {
      console.log('录音开始事件');
      Countdown(this); //开始计时
    });
    RM.onError((res) => {
      console.log('recorder出错:' + res);
      console.log(res);
      clearTimeout(timer); //出错时停止计时
    })
  },
  //暂停录音
  onPausefunction () {
    wx.showToast({
      title'暂停录音',
      icon'none'
    })
    this.setData({
      pausefalse,
      resumetrue
    })
    RM.pause();
    RM.onPause(() => {
      console.log('录音暂停事件');
      clearTimeout(timer);
    })
  },
  //继续录音
  onResumefunction () {
    var that = this;
    wx.showToast({
      title'继续录音',
      icon'none'
    })
    this.setData({
      pausetrue,
      resumefalse
    })
    RM.resume();
    RM.onResume(() => {
      console.log('录音继续事件')
      Countdown(that); //开始计时
    })
  },
  //结束录音
  onEndfunction () {
    wx.showToast({
      title'结束录音',
      icon'none'
    })
    RM.stop();
    RM.onStop((res) => {
      clearTimeout(timer);
      this.setData({
        pausefalse,
        resumefalse,
        stopfalse,
        completetrue,
        deletetrue,
      })
      console.log(res)
      // res.tempFilePath;//是临时的文件地址
      //res.duration;//录音的时长
      // res.fileSize;//文件的大小
      this.data.url = res.tempFilePath;
    })
  },
  //删除录音
  deletefunction () {
    var that = this
    wx.showModal({
      title'提示',
      content'确定要删除此录音吗?',
      successfunction (res{
        if (res.confirm) {
          that.setData({
            times'0min 0s',
            starttrue,
            pausefalse,
            resumefalse,
            stopfalse,
            playfalse,
            completefalse,
            deletefalse,
          })
        } else if (res.cancel) {
          return false;
        }
      }
    })
  },
  //保存
  completefunction () {
    wx.showLoading({
      title'上传中',
    });
    var filePath = this.data.url;
    var cloudPath = ''
    const name = Math.random() * 1000000;
    if (filePath) {
      cloudPath = name + filePath.match(/\.[^.]+?$/)[0]
    }
    wx.cloud.uploadFile({
      filePath,
      cloudPath,
      successres => {
        this.setData({
          playtrue,
          completefalse,
          deletefalse
        })
      },
      faile => {
        console.error('上传失败:', e)
      },
      complete() => {
        wx.hideLoading()
        wx.showToast({
          title'已保存,可以播放',
          icon'none'
        })
      }
    })
  },
  //播放录音
  onPlayfunction () {
    wx.showToast({
      title'播放录音',
      icon'none'
    })
    innerAudioContext.autoplay = true
    innerAudioContext.src = this.data.url,
      innerAudioContext.onPlay(() => {
        console.log('开始播放')
      })
    innerAudioContext.onError((res) => {
      console.log(res.errMsg)
      console.log(res.errCode)
    })
    console.log();
  },
})
// 计时
var secondes = 0;
function Countdown(that{
  timer = setTimeout(function () {
    console.log("----secondes----" + formatSeconds(secondes));
    secondes++;
    if (secondes >= 600) {
      RM.stop();
      clearTimeout(timer);
    }
    that.setData({
      times: formatSeconds(secondes)
    });
    Countdown(that);
  }, 1000);
};

// 处理时间
function formatSeconds(value{
  var secondTime = parseInt(value); // 秒
  var minuteTime = 0// 分
  if (secondTime > 60) { //如果秒数大于60,将秒数转换成整数
    //获取分钟,除以60取整数,得到整数分钟
    minuteTime = parseInt(secondTime / 60);
    //获取秒数,秒数取佘,得到整数秒数
    secondTime = parseInt(secondTime % 60);
    //如果分钟大于60,将分钟转换成小时
  }
  var result;
  if (minuteTime <= 0) {
    result = secondTime + 's';
  } else {
    result = minuteTime + 'min ' + secondTime + 's';
  }
  return result;
}
//wxml
<view class="bg">
  <text class="time">{{times}}</text>
  <input class="title" placeholder="输入录音标题" placeholder-style="color:#ffffff;" value="{{rm_title}}"></input>
  <image class="play icon" src="../../images/play.png" bindtap="onStart" wx:if="{{start}}">开始</image>
  <image class="pause icon" src="../../images/pause.png" bindtap="onPause" wx:if="{{pause}}">暂停</image>
  <image class="resume icon" src="../../images/play.png" bindtap="onResume" wx:if="{{resume}}">继续</image>
  <image class="stop icon" src="../../images/stop.png" bindtap="onEnd" wx:if="{{stop}}">结束</image>
  <image class="play icon" src="../../images/play.png" bindtap="onPlay" wx:if="{{play}}">播放</image>
  <image class="delete icon" src="../../images/delete.png" bindtap="delete" wx:if="{{delete}}">删除</image>
  <image class="complete icon" src="../../images/complete.png" bindtap="complete" wx:if="{{complete}}">完成</image>
</view>
回到顶部