无法在移动设备上禁用navigator.mediaDevices.getUserMedia网络摄像头

时间:2018-08-10 11:47:53

标签: javascript android html5 webrtc

我正在使用navigator.mediaDevices.getUserMedia访问网络摄像头并扫描element中的QR码。成功检测到QR码后,我正在调用功能deactivate_camera(),该功能应释放并禁用网络摄像头。该功能可在笔记本电脑上使用,但在手机(Android Chrome)上似乎仍在使用相机(并且网站速度变慢)。

我注意到,只有在刷新页面后,网络摄像头才会释放。感谢大家找到解决方案。

Screen: Camera still in use on Android

启用摄像头:

function activate_camera() {
    console.log("Aktywacja kamery.");
    if (video !== undefined || video !== null) {
        video = document.createElement("video");
    }
    // Use facingMode: environment to attempt to get the back camera on phones
    navigator.mediaDevices.getUserMedia({video: {facingMode: "environment"}})
        .then(function (stream) {
            video.srcObject = stream;
            videostream = stream;
            video.setAttribute("playsinline", true); // required to tell iOS safari we don't want fullscreen
            video.play();
            requestAnimationFrame(tick);
        })
        .catch(function(err) {
            //console.log(err);
            $('#loadingMessage').html("Cant access webcam");
        });
}

禁用相机:

function deactivate_camera() {
    console.log("Wyłączanie kamery.");
    if (video !== undefined && video !== null) {
        alert("Wyłączanie kamery.");
        if (typeof video.pause === 'function') video.pause();
        if (video.srcObject !== null) {
            //video.srcObject.getTracks()[0].stop();
            video.srcObject.getTracks().forEach(function (track) {
                alert(track);
                track.stop();
            });
        }
        video.srcObject = null;
        if (videostream !== null) {
            if (typeof videostream.stop === 'function') {
                videostream.stop();
                alert('videostream stop');
            }
        }
        videostream = null;
        if ($(video).length) $(video).remove();
        video = null;
    }
}

0 个答案:

没有答案