WeChatアプレットの権限検証とその授権

2131 ワード

録音機能を例にとって、WeChatウィジェットに付与されたプロセスを記録してください.
wxml:3つの方法を定義します.

  
  
  
  
js:
       
const recorderManager = wx.getRecorderManager()
const innerAudioContext = wx.createInnerAudioContext()

data: {
    tempFilePath:""
  },


record(){
    var that = this;
    wx.getSetting({
      success(res){
        if (res.authSetting['scope.record']){
          wx.showToast({
            title: '    ',
            icon: 'success',
            duration: 1000
          })
          that.start();

        } else {
          wx.authorize({
            scope: 'scope.record',
            success: function(){
              that.start()
            },
            fail:function(){
              wx.showModal({
                title: '  ',
                content: '        ',
                confirmText:" "
              })
            }
          })
        }
      },
      fail:function(){

      }
    })
  },

start(){
    const options = {
      duration: 10000,//       ,   ms
      sampleRate: 16000,//   
      numberOfChannels: 1,//     
      encodeBitRate: 96000,//    
      format: 'mp3',//    ,    aac/mp3
      frameSize: 50,//     ,   KB
    }
    recorderManager.start(options);
    recorderManager.onStart(() => {
      console.log('recorder start')
    });
    //    
    recorderManager.onError((res) => {
      console.log(res);
    })
  },
  stop(){
    recorderManager.stop();
    recorderManager.onStop((res) => {
      this.tempFilePath = res.tempFilePath;
      console.log('    ', res.tempFilePath)
      const { tempFilePath } = res
    })
  },

play(){
    innerAudioContext.autoplay = true
    innerAudioContext.src = this.tempFilePath,
      innerAudioContext.onPlay(() => {
        console.log('    ')
      })
    innerAudioContext.onError((res) => {
      console.log(res.errMsg)
      console.log(res.errCode)
    })
  },