微信小程序,录音管理 - recorderManager 封装

1,176 阅读1分钟

录音管理封装

封装了录音管理的权限校验,没有权限则拉起授权

注册调用

    const recodManage = Record()
    recodManage.create({
        recordConfig: {
          duration: 60000,
          format: 'mp3',
        },// 非必传:参数参照微信 recorderManager API
        onStart: () => {
            console.log('录音开始了')
        },
        onStop: fileInfo => {
            console.log('录音结束了,文件信息:', fileInfo)
        }
    })
    
    // 使用举例:
    recodManage.action() // 开始录音
    
    recodManage.stopRecord() // 停止录音

代码如下

/**
   * 录音封装
   */
  Record() {
    class recordManage {
      constructor() {
        this.recordContext = null
        this.recordStatus = false // 录音当前状态
        this.recordConfig = {
          duration: 60000,
          format: 'mp3',
        }
      }
      create(config = {}) {
        this.recordOnStart = config.onStart
        this.recordOnStop = config.onStop
        if (typeof config.recordConfig === 'object') {
          this.recordConfig = Object.assign(this.recordConfig, this.config.recordConfig)
        }
        console.log('录音初始化完成');
      }
      init() {
        if (this.recordContext) return
        this.recordContext = wx.getRecorderManager()
        this.recordContext.onStart(res => {
          if (typeof this.recordOnStart === 'function') this.recordOnStart(res)
        });
        this.recordContext.onStop(res => {
          if (typeof this.recordOnStop === 'function') this.recordOnStop(res)
        })
      }
      // 检查录音权限
      action() {
        if (this.recordStatus) return
        this.recordStatus = true
        wx.getSetting({
          success: (res) => {
            if (!res.authSetting["scope.record"]) {
              this.recordStatus = false
              // 如果拒绝授权,则开启引导授权菜单
              if (res.authSetting["scope.record"] === false) {
                return this.openSetting();
              }
              // 没有授权记录 - 拉起授权
              this.authorizeRecord();
            } else {
              this.init()
              // 已授权 - 开始录音
              this.startRecord();
            }
          },
        });
      }
      // 初次 授权录音权限
      authorizeRecord() {
        wx.authorize({
          scope: "scope.record",
          success() {
            wx.showToast({
              title: "授权成功!\n请点击录音按钮开始录音吧!",
              icon: "none",
            });
          },
        });
      }
      // 引导用户授权 拉起授权菜单
      openSetting() {
        wx.openSetting({
          success(res) {
            if (res.authSetting["scope.record"] === true) {
              wx.showToast({
                title: "授权成功!\n请点击录音按钮开始录音吧!",
                icon: "none",
              });
            } else {
              wx.showToast({
                title: "授权失败!",
                icon: "none",
              });
            }
          },
        });
      }
      // 开始录音
      startRecord() {
        this.recordContext.start(this.recordConfig);
      }
      // 停止录音
      stopRecord() {
        if (!this.recordStatus) return
        this.recordStatus = false
        this.recordContext.stop();
      }
    }

    return new recordManage()
  }