xinray
xinray
  • 发布:2019-11-22 11:45
  • 更新:2020-03-25 21:01
  • 阅读:2751

【报Bug】web-view里面打开的页面无法获取到摄像头

分类:uni-app

详细问题描述

(DCloud产品不会有明显的bug,所以你遇到的问题大都是在特定环境下才能重现的问题,请仔细描述你的环境和重现方式,否则DCloud很难排查解决你的问题)

[内容]

重现步骤

window.navigator.getUserMedia = navigator.getUserMedia || navigator.mediaDevices.getUserMedia || navigator.webKitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;  

            function invokingCarera() {  
                if(navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {  
                    navigator.mediaDevices.getUserMedia({  
                        'audio': true,  
                        'video': {  
                            'facingMode': "user"  
                        } //调用前置摄像头,后置摄像头使用video: { facingMode: { exact: "environment" } }  
                    })  
                    .then(function(mediaStream) {  
                        console.log(555);  
                        getVideoStream(mediaStream)  
                    })  
                    .catch(function(error) {  
                        console.log(666);  
                        console.log(error)  
                        alert(error)  
                    })  
                } else if(navigator.getUserMedia) {  
                    navigator.getUserMedia({  
                        'video': true,  
                        'audio': true  
                    }, getVideoStream, getFail)  
                } else {  
                    alert('不支持摄像头调用!')  
                }  
            }

[步骤]
执行invokingCarera事件后调用到摄像头
[结果]
返回NotReadableError: Could not start audio source错误
[期望]
返回mediaStream
[如果语言难以表述清晰,拍一个视频或截图,有图有真相]

IDE运行环境说明

HBuilderX2.3.7
[IDE版本号]

[windows版本号]

[mac版本号]

uni-app运行环境说明

app

[运行端版本号]
HBuilderX创建的
自定义组件模式

App运行环境说明

[Android版本号]
8.1.0

[iOS版本号]

[手机型号]

[模拟器型号]

附件

[IDE问题请提供HBuilderX运行日志。菜单帮助-查看运行日志,点右键打开文件所在目录,将log文件压缩成zip包上传]

[App问题请提供可重现问题的代码片段,你补充的细一点,问题就解决的快一点]

[App安装包或H5地址]

[可重现代码片段]

<!DOCTYPE html>  
<html lang="en">  

    <head>  
        <meta charset="UTF-8">  
        <meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0,user-scalable=no">  
        <title>Document1</title>  
        <style type='text/css'>  
            * {  
                margin: 0;  
                padding: 0;  
            }  

            html,  
            body {  
                height: 100%;  
            }  

            .flex {  
                display: flex;  
            }  

            .flex-row {  
                flex-direction: row;  
                justify-content: space-around;  
                align-items: center;  
            }  

            .flex-column {  
                flex-direction: column;  
                justify-content: flex-start;  
                align-items: center;  
            }  

            body {  
                overflow: auto;  
                background: #fff;  
            }  

            .title {  
                width: 920px;  
                padding: 30px;  
                align-items: flex-end;  
            }  

            .title h1 {  
                padding-bottom: 20px;  
                font-size: 38px;  
                color: #ffffff;  
                text-shadow: 0 1px 3px #222222;  
            }  

            .title p {  
                font-size: 18px;  
                color: #f5f5f5;  
                text-shadow: 0 1px 3px #565656;  
            }  

            .wrap {  
                width: 1220px;  
            }  

            .wrap .reference {  
                position: relative;  
                padding: 10px;  
                background-color: rgba(255, 255, 255, 0);  
                border-radius: 10px;  
                box-shadow: 0 0 20px #a1a19f;  
            }  

            .wrap .reference img.face {  
                display: block;  
                width: 320px;  
                height: auto;  
                border-radius: 10px;  
            }  

            .wrap .reference img.toggle {  
                position: absolute;  
                right: 10px;  
                top: 10px;  
                width: 50px;  
                height: 50px;  
            }  

            .wrap .scan video {  
                background-color: rgba(0, 0, 0, .8);  
                border-radius: 10px;  
            }  

            .wrap .control {  
                justify-content: space-around;  
                height: 456px;  
                padding: 0 20px;  
            }  

            .wrap .control p {  
                width: 160px;  
                height: 60px;  
                background-color: #f9f9f9;  
                text-align: center;  
                line-height: 60px;  
                color: #ffffff;  
                font-size: 24px;  
                border-radius: 8px;  
                cursor: pointer;  
                box-shadow: -8px -8px 150px -8px #b2b3b5 inset, 0 0 5px #222222;  
                text-shadow: 0 0 1px #222222;  
                transition: .5s;  
            }  

            .wrap .control p:hover {  
                box-shadow: -8px -8px 150px -8px #50c4f1 inset, 0 0 5px #ffffff;  
            }  

            .wrap .scan {  
                position: relative;  
                overflow: hidden;  
            }  

            .wrap .scan .strainer {  
                position: absolute;  
                top: 10px;  
                width: 320px;  
                z-index: 999;  
                height: 3px;  
            }  

            .wrap .scan .capture {  
                width: 320px;  
                height: 456px;  
            }  

            .wrap .scan .strainer.on {  
                background: linear-gradient(to left, transparent, #0bffb2, transparent);  
                animation: scan 1s linear infinite;  
            }  

            @keyframes scan {  
                0% {  
                    top: 10px;  
                }  
                50% {  
                    top: 456px;  
                }  
                100% {  
                    top: 10px;  
                }  
            }  
        </style>  
        <script type="text/javascript" src="vconsole.min.js"></script>  
    </head>  

    <body>  
        <div class="title flex flex-column">  
        </div>  
        <div class="wrap flex flex-row">  
            <div class="control flex flex-column">  
                <p class="open">开启摄像头</p>  
                <p class="recognition">显示到Canvas</p>  
                <p class="close">关闭摄像头</p>  
            </div>  
            <div class="scan reference">  
                <div class="strainer"></div>  
                <video class="capture" width="320" height="456" src=""></video>  
            </div>  

        </div>  

        <script type="text/javascript">  
            var buffer;  
            var oCapture = document.querySelector(".capture"),  
                open = document.querySelector(".open"),  
                recognition = document.querySelector(".recognition"),  
                close = document.querySelector(".close");  
            var control = document.querySelector(".control");  

            window.navigator.getUserMedia = navigator.getUserMedia || navigator.mediaDevices.getUserMedia || navigator.webKitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;  

            function invokingCarera() {  
                if(navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {  
                    navigator.mediaDevices.getUserMedia({  
                        'audio': true,  
                        'video': {  
                            'facingMode': "user"  
                        } //调用前置摄像头,后置摄像头使用video: { facingMode: { exact: "environment" } }  
                    })  
                    .then(function(mediaStream) {  
                        console.log(555);  
                        getVideoStream(mediaStream)  
                    })  
                    .catch(function(error) {  
                        console.log(666);  
                        console.log(error)  
                        alert(error)  
                    })  
                } else if(navigator.getUserMedia) {  
                    navigator.getUserMedia({  
                        'video': true,  
                        'audio': true  
                    }, getVideoStream, getFail)  
                } else {  
                    alert('不支持摄像头调用!')  
                }  
            }  
            //调用成功  
            function getVideoStream(stream) {  
                buffer = stream;  
                if(oCapture.mozSrcObject !== undefined) {  
                    oCapture.mozSrcObject = buffer;  
                } else {  
                    oCapture.src = window.URL && window.URL.createObjectURL(buffer);  
                }  
                oCapture.play();  
            }  

            function getFail() {  

            }  
            recognition.onclick = function() {  

            }  
            control.addEventListener('click', function(e) {  
                e = e || window.event;  
                var className = e.target.className;  
                switch(className) {  
                    case 'open':  
                        invokingCarera();  
                        break;  
                    case 'close':  
                        closeCamera();  
                        break;  
                    case 'recognition':  
                        screenShot();  
                        break;  
                    default:  
                        break;  
                }  
            })  

            function closeCamera() {  
                buffer && buffer.getTracks()[1].stop(); //关闭摄像头  
            }  
            window.requestAnimFrame = (function() {  
                return window.requestAnimationFrame ||  
                    window.webkitRequestAnimationFrame ||  
                    window.mozRequestAnimationFrame ||  
                    function(callback) {  
                        window.setTimeout(callback, 1000 / 60);  
                    };  
            })();  

            function screenShot() {  
                var canvas = document.createElement('canvas');  
                canvas.width = 320, canvas.height = 456;  
                document.querySelector(".wrap").appendChild(canvas);  
                var ctx = canvas.getContext('2d');  

                function drawVideo() {  
                    ctx.drawImage(oCapture, 0, 0, 320, 456);  
                    ctx.font = "30px sans-serif";  
                    ctx.fillStyle = "blue";  
                    ctx.fillText("请眨眼", 50, 50);  
                    requestAnimationFrame(drawVideo);  
                }  
                window.requestAnimationFrame(drawVideo);  
            }  
        </script>  
    </body>  

</html>

联系方式

[QQ] 1045247746

2019-11-22 11:45 负责人:无 分享
已邀请:
spackkr

spackkr

请手动设置基座相机和麦克风权限,如果是安卓手机在手机管家里边手动打开基座的摄像头和麦克风权限即可

  • 独孤求剑

    为什么没有提示呢,能否实现像原生开发一样的打开权限提示并跳转到权限设置页?

    2023-06-12 00:38

s***@163.com

s***@163.com

试试这个
video.srcObject = stream;
一个兼容的写法如下:

try {  
  this.srcObject = stream;} catch (error) {  
  this.src = window.URL.createObjectURL(stream);}  

该问题目前已经被锁定, 无法添加新回复