小程序录音并上传

<view open-type="openSetting" bindtap="getRecord" >
//录音计时器
  recordingTimer: function() {
    var that = this;
    //将计时器赋值给setInter
    that.data.setInter = setInterval(
      function() {
        var time = that.data.recordingTimeqwe + 1;
        that.setData({
          recordingTimeqwe: time
        })
      }, 1000);
  },
  // 录音开始
  recordStart: function() {
    let that = this;
    const options = {
      duration: 60000, //指定录音的时长,单位 ms,最大为10分钟(600000),默认为1分钟(60000)
      format: 'mp3', //音频格式,有效值 aac/mp3
    }
    //开始录音计时   
    that.recordingTimer();
    //开始录音
    recorderManager.start(options);
    //屏幕常亮
    wx.setKeepScreenOn({
      keepScreenOn: true
    })
    recorderManager.onStart(() => {
      console.log('。。。开始录音。。。')
    });
    //错误回调
    recorderManager.onError((res) => {
      console.log("录音错误", res);
    })
  },
  //录音结束
  recordEnd: function() {
    let that = this;
    console.log("结束录音")
    recorderManager.stop();
    //取消屏幕常亮
    wx.setKeepScreenOn({
      keepScreenOn: false
    })
    recorderManager.onStop((res) => {
      const {duration} = res; //播放时长
      const {fileSize} = res; //文件大小
      console.log("大小", fileSize)
      const {tempFilePath } = res; // 文件临时路径
      //结束录音计时  
      clearInterval(that.data.setInter);
       //上传片段录音
        wx.uploadFile({
          url: '', //要上传的url
          filePath: tempFilePath,
          name: "file", //后台要绑定的名称
          header: {
            "Content-Type": "multipart/form-data"
          },
          //参数绑定
          formData: {
            userId: wx.getStorageSync('userId'),
            duration: duration,
            size: fileSize
          },
          success: function(res) {
			 console.log("录音保存成功");
		},
          fail: function(res) {
            console.log("录音保存失败");
          }
        })
    })
  },
  //录音取消操作
  cancelRecord: function() {
    let that = this;
    console.log("结束录音")
    recorderManager.stop();
    //取消屏幕常亮
    wx.setKeepScreenOn({
      keepScreenOn: false
    })
    recorderManager.onStop((res) => {
      const {duration } = res; //播放时长
      const {fileSize} = res; //文件大小
      const {tempFilePath } = res;
      //结束录音计时
      clearInterval(that.data.setInter);
      wx.showToast({
        title: '录音已取消',
        duration: 800,
        icon: 'none'
      })
    })
  },
  //录音授权
  getRecord: function() {
    var that = this;
    wx.getSetting({
      success(res) {
        console.log("录音授权", res);
        if (!res.authSetting['scope.record']) {
          //调取小程序新版授权页面
          wx.authorize({
            scope: 'scope.record',
            success() {
              console.log("录音授权成功");
              that.setData({
                status: 2
              })
              that.recordStart()
            },
            fail() {
              wx.showModal({
                title: '提示',
                content: '您未授权录音,功能将无法使用',
                showCancel: true,
                confirmText: "授权",
                confirmColor: "#52a2d8",
                success: function(res) {
                  if (res.confirm) {
                    //确认则打开设置页面(重点)
                    wx.openSetting({
                      success: (res) => {
                        console.log("重新授权", res.authSetting);
                        if (!res.authSetting['scope.record']) {
                          //未设置录音授权
                          console.log("未设置录音授权");
                          wx.showModal({
                            title: '提示',
                            content: '您未授权录音,功能将无法使用',
                            showCancel: false,
                            success: function(res) {
                            },
                          })
                        } else {
                          //第二次才成功授权
                          console.log("第二次设置录音授权成功");
                          that.recordStart()
                        }
                      },
                      fail: function() {
                        console.log("授权设置录音失败");
                      }
                    })
                  } else if (res.cancel) {
                    console.log("cancel");
                  }
                },
                fail: function() {
                  console.log("openfail");
                }
              })
            }
          })
        } else {
          that.recordStart();
        }
      }
    })
  },
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值