zoukankan      html  css  js  c++  java
  • 小程序中开启录音功能(使用wepy)

    1.检查是否获取到录音权限---获取录音权限

    onLoad(options, data) {
        this.judgeRecord().then(res=>{
            if(res==true) {
                this.recordInit = true;
                this.$apply();
            }
        })
        this.$apply();
    }
    

      

     1 judgeRecord() {
     2             return new Promise(function (resolve, reject) {
     3                 wx.getSetting({
     4                     success(res) {
     5                         if (!res.authSetting['scope.record']) {
     6                             if (res.authSetting['scope.record'] == undefined) {
     7                                 wx.authorize({
     8                                     scope: 'scope.record',
     9                                     success() {
    10                                         // 用户已经同意小程序使用录音功能,后续调用    wx.startRecord 接口不会弹窗询问
    11                                         // wx.startRecord()
    12                                         resolve(true)
    13                                     },
    14                                     fail() {
    15                                         resolve(false)
    16                                     }
    17                                 })
    18                             }else{
    19                                 wx.showModal({
    20                                     title: '请求授权录音权限',
    21                                     content: '需要获取您的录音权限,请确认授权',
    22                                     success: function (res) {
    23                                         if (res.cancel) {
    24                                             wx.showToast({
    25                                                 title: '拒绝授权',
    26                                                 icon: 'none',
    27                                                 duration: 1000
    28                                             })
    29                                             resolve(false)
    30                                         } else if (res.confirm) {
    31                                             wx.openSetting({
    32                                                 success: function (dataAu) {
    33                                                     if (dataAu.authSetting["scope.record"] == true) {
    34                                                         wx.showToast({
    35                                                             title: '授权成功',
    36                                                             icon: 'success',
    37                                                             duration: 1000
    38                                                         })
    39                                                         resolve(true)
    40                                                         //再次授权,调用wx.getLocation的API
    41                                                     } else {
    42                                                         wx.showToast({
    43                                                             title: '授权失败',
    44                                                             icon: 'none',
    45                                                             duration: 1000
    46                                                         })
    47                                                         resolve(false)
    48                                                     }
    49                                                 }
    50                                             })
    51                                         }
    52                                     }
    53                                 })
    54                             }
    55 
    56                         }else{
    57                             resolve(true)
    58                         }
    59                     }
    60                 });
    61             })
    62 
    63         }            

    2.初始加载如果用户拒绝授权,则在需要操作录音的位置继续调用授权

    onTouchStart(e) {
                    this.$apply();
                    if (!this.recordInit) {
                        this.judgeRecord();
                        return false;
                    }
                },
    

      

  • 相关阅读:
    set基本用法-----2
    set基本用法---1
    最大和
    最长公共上升子序列||LCIS
    CODEVS【3556】科技庄园
    CODEVS【3372】选学霸
    CODEVS【1025】选菜
    hlg1398邮局问题【找中位数】
    hlg1175小陈老师、桌子、盘子【计算几何】
    hlg1216数的划分【地推公式|dfs】
  • 原文地址:https://www.cnblogs.com/fairy62/p/12612064.html
Copyright © 2011-2022 走看看