求助RecorderManager录音自写计时有问题,真机调试暂停时间后计时会双倍?

发布于 8 年前作者 lijun11678 次浏览最后编辑 8 年前来自 ask

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

菜鸟求指点!!!

//js
let RM = wx.getRecorderManager();
const innerAudioContext = wx.createInnerAudioContext()
var timer;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    url: '',
    start: true,
    pause: false,
    resume: false,
    stop: false,
    play: false,
    complete: false,
    delete: false,
    times: '0min 0s',
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {},
  onShow: function () {
    RM.stop();
  },
  //开始录音
  onStart: function () {
    wx.showToast({
      title: '开始录音',
      icon: 'none'
    })
    this.setData({
      start: false,
      pause: true,
      stop: true,
      delete: false,
      complete: false
    })
    let option = {
      duration: 600000, // 10 minutes
      sampleRate: 48000,
      numberOfChannels: 2,
      encodeBitRate: 320000,
      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); //出错时停止计时
    })
  },
  //暂停录音
  onPause: function () {
    wx.showToast({
      title: '暂停录音',
      icon: 'none'
    })
    this.setData({
      pause: false,
      resume: true
    })
    RM.pause();
    RM.onPause(() => {
      console.log('录音暂停事件');
      clearTimeout(timer);
    })
  },
  //继续录音
  onResume: function () {
    var that = this;
    wx.showToast({
      title: '继续录音',
      icon: 'none'
    })
    this.setData({
      pause: true,
      resume: false
    })
    RM.resume();
    RM.onResume(() => {
      console.log('录音继续事件')
      Countdown(that); //开始计时
    })
  },
  //结束录音
  onEnd: function () {
    wx.showToast({
      title: '结束录音',
      icon: 'none'
    })
    RM.stop();
    RM.onStop((res) => {
      clearTimeout(timer);
      this.setData({
        pause: false,
        resume: false,
        stop: false,
        complete: true,
        delete: true,
      })
      console.log(res)
      // res.tempFilePath;//是临时的文件地址
      //res.duration;//录音的时长
      // res.fileSize;//文件的大小
      this.data.url = res.tempFilePath;
    })
  },
  //删除录音
  delete: function () {
    var that = this
    wx.showModal({
      title: '提示',
      content: '确定要删除此录音吗?',
      success: function (res) {
        if (res.confirm) {
          that.setData({
            times: '0min 0s',
            start: true,
            pause: false,
            resume: false,
            stop: false,
            play: false,
            complete: false,
            delete: false,
          })
        } else if (res.cancel) {
          return false;
        }
      }
    })
  },
  //保存
  complete: function () {
    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,
      success: res => {
        this.setData({
          play: true,
          complete: false,
          delete: false
        })
      },
      fail: e => {
        console.error('上传失败:', e)
      },
      complete: () => {
        wx.hideLoading()
        wx.showToast({
          title: '已保存,可以播放',
          icon: 'none'
        })
      }
    })
  },
  //播放录音
  onPlay: function () {
    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>
0 回复
暂无回复