How do I get permission to navigator.getusermedia via webview in cordoba?

时间:2017-07-19 00:34:27

标签: android cordova

I want to develop Android through cordova webview on the web made with HTML. However, cordova is not allowed in navigator.getusermedia. Is there a way?

1 个答案:

答案 0 :(得分:0)

您可以通过cordova-diagnostic-plugin

请求使用相机的运行时权限
  • 将诊断插件安装到您的项目中
    • cordova plugin add cordova.plugins.diagnostic
  • cordova-custom-config插件安装到您的项目中
    • cordova plugin add cordova-custom-config
  • 为config.xml添加相机权限的清单条目(请参阅Android permissions):

    <platform name="android">
        <config-file target="AndroidManifest.xml" parent="/*">
            <uses-permission android:name="android.permission.CAMERA" />
        </config-file>
    </platform>
    
  • Request camera authorization在运行时:

    document.addEventListener('deviceready', function(){
        cordova.plugins.diagnostic.requestCameraAuthorization(
            function(status){
                if(status == cordova.plugins.diagnostic.permissionStatus.GRANTED){
                    // permission granted - use camera
                    var video = document.querySelector('video');
    
                    if (navigator.getUserMedia) {
                        navigator.getUserMedia({audio: true, video: true}, function(stream) {
                            video.src = window.URL.createObjectURL(stream);
                        }, function(e){
                            console.log("Error:" + e);
                        });
                    } else {
                        console.log("No media available");
                    }
                }else{
                    // permission denied - inform user
                }
            }, function(error){
                console.error("The following error occurred: "+error);
            }, false
        );  
    }, false);