HBuilderx打包h5的APP,解决获取相机、麦克风权限问题

2023-09-27  本文已影响0人  喜欢走弯路的人

index.html文件script中加入以下内容

获取照相机权限

document.addEventListener('plusready',function () { // 这里一定要加上  不然会报push 未定义

        plus.android.requestPermissions(['android.permission.CAMERA'], function(e){

        if(e.deniedAlways.length>0){    //权限被永久拒绝

            // 弹出提示框解释为何需要定位权限,引导用户打开设置页面开启

            //alert('永久');

            document.addEventListener("plusready", onPlusReady, false);

            // 扩展API加载完毕,现在可以正常调用扩展API

            function onPlusReady() {

                // 创建扫码控件来调获取手机的相机权限

                barcode = plus.barcode.create('barcode', [plus.barcode.QR], {

                    top: '-9999', //改为-9999px隐藏该页面

                    left: '0',

                    width: '100%',

                    height: '500px',

                    position: 'static'

                });

                //plus.webview.currentWebview().append(barcode);;

                //barcode.start();开始扫码识别(我们把这句代码注释了,因为我们不需要扫描任何东西)

            }

            //上面打开权限后,这里直接重启APP

            plus.runtime.restart();

        }

        if(e.deniedPresent.length>0){   //权限被临时拒绝

            // 弹出提示框解释为何需要定位权限,可再次调用plus.android.requestPermissions申请权限

            //alert('临时 ');

        }

        if(e.granted.length>0){ //权限被允许

            //调用依赖获取定位权限的代码

            //alert('允许!!! ');

        }

    }, function(e){

        alert('Request Permissions error:'+JSON.stringify(e));

    });

    },false);

获取麦克风权限

  document.addEventListener('plusready',function () {

    plus.android.requestPermissions(['android.permission.RECORD_AUDIO'], function(e){

    if(e.deniedAlways.length>0){    //权限被永久拒绝

        // 弹出提示框解释为何需要定位权限,引导用户打开设置页面开启

        //alert('永久');

        document.addEventListener("plusready", onPlusReady, false);

        // 扩展API加载完毕,现在可以正常调用扩展API

        function onPlusReady() {

            // 创建扫码控件来调获取手机的相机权限

            barcode = plus.barcode.create('barcode', [plus.barcode.QR], {

                top: '-9999', //改为-9999px隐藏该页面

                left: '0',

                width: '100%',

                height: '500px',

                position: 'static'

            });

            //plus.webview.currentWebview().append(barcode);;

            //barcode.start();开始扫码识别(我们把这句代码注释了,因为我们不需要扫描任何东西)

        }

        //上面打开权限后,这里直接重启APP

        plus.runtime.restart();

    }

    if(e.deniedPresent.length>0){   //权限被临时拒绝

        // 弹出提示框解释为何需要定位权限,可再次调用plus.android.requestPermissions申请权限

        //alert('临时 ');

    }

    if(e.granted.length>0){ //权限被允许

        //调用依赖获取定位权限的代码

        //alert('允许!!! ');

    }

    }, function(e){

      alert('Request Permissions error:'+JSON.stringify(e));

    });

  },false);

上一篇下一篇

猜你喜欢

热点阅读