是否可以检查用户是否有相机和麦克风,并且是否已经通过Javascript授予权限?

前端之家收集整理的这篇文章主要介绍了是否可以检查用户是否有相机和麦克风,并且是否已经通过Javascript授予权限?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想知道用户的设备是否具有连接的相机和麦克风,如果是,则授予使用 Javascript获取音频和视频流的权限.我想将此检查至少在Chrome和Firefox上进行.什么是一致的API?

解决方法

现场演示:

> https://www.webrtc-experiment.com/DetectRTC/

如果用户没有允许网络摄像头和/或麦克风,则媒体设备的“label”属性将具有“NULL”值.以上页面显示此消息:“请调用getUserMedia一次”.

PS.您可以在Chrome控制台开发人员工具中输入“DetectRTC.MediaDevices”.

注意:它仅适用于Chrome. Firefox还不支持类似的API. (更新:Firefox支持)

于2015年12月16日更新

注意:以下代码段在Chrome和Firefox中都可用.

if (navigator.mediaDevices && navigator.mediaDevices.enumerateDevices) {
    // Firefox 38+ seems having support of enumerateDevicesx
    navigator.enumerateDevices = function(callback) {
        navigator.mediaDevices.enumerateDevices().then(callback);
    };
}

var MediaDevices = [];
var isHTTPs = location.protocol === 'https:';
var canEnumerate = false;

if (typeof MediaStreamTrack !== 'undefined' && 'getSources' in MediaStreamTrack) {
    canEnumerate = true;
} else if (navigator.mediaDevices && !!navigator.mediaDevices.enumerateDevices) {
    canEnumerate = true;
}

var hasMicrophone = false;
var hasSpeakers = false;
var hasWebcam = false;

var isMicrophoneAlreadyCaptured = false;
var isWebcamAlreadyCaptured = false;

function checkDeviceSupport(callback) {
    if (!canEnumerate) {
        return;
    }

    if (!navigator.enumerateDevices && window.MediaStreamTrack && window.MediaStreamTrack.getSources) {
        navigator.enumerateDevices = window.MediaStreamTrack.getSources.bind(window.MediaStreamTrack);
    }

    if (!navigator.enumerateDevices && navigator.enumerateDevices) {
        navigator.enumerateDevices = navigator.enumerateDevices.bind(navigator);
    }

    if (!navigator.enumerateDevices) {
        if (callback) {
            callback();
        }
        return;
    }

    MediaDevices = [];
    navigator.enumerateDevices(function(devices) {
        devices.forEach(function(_device) {
            var device = {};
            for (var d in _device) {
                device[d] = _device[d];
            }

            if (device.kind === 'audio') {
                device.kind = 'audioinput';
            }

            if (device.kind === 'video') {
                device.kind = 'videoinput';
            }

            var skip;
            MediaDevices.forEach(function(d) {
                if (d.id === device.id && d.kind === device.kind) {
                    skip = true;
                }
            });

            if (skip) {
                return;
            }

            if (!device.deviceId) {
                device.deviceId = device.id;
            }

            if (!device.id) {
                device.id = device.deviceId;
            }

            if (!device.label) {
                device.label = 'Please invoke getUserMedia once.';
                if (!isHTTPs) {
                    device.label = 'HTTPs is required to get label of this ' + device.kind + ' device.';
                }
            } else {
                if (device.kind === 'videoinput' && !isWebcamAlreadyCaptured) {
                    isWebcamAlreadyCaptured = true;
                }

                if (device.kind === 'audioinput' && !isMicrophoneAlreadyCaptured) {
                    isMicrophoneAlreadyCaptured = true;
                }
            }

            if (device.kind === 'audioinput') {
                hasMicrophone = true;
            }

            if (device.kind === 'audiooutput') {
                hasSpeakers = true;
            }

            if (device.kind === 'videoinput') {
                hasWebcam = true;
            }

            // there is no 'videoouput' in the spec.

            MediaDevices.push(device);
        });

        if (callback) {
            callback();
        }
    });
}

// check for microphone/camera support!
checkDeviceSupport(function() {
    document.write('hasWebCam: ',hasWebcam,'<br>');
    document.write('hasMicrophone: ',hasMicrophone,'<br>');
    document.write('isMicrophoneAlreadyCaptured: ',isMicrophoneAlreadyCaptured,'<br>');
    document.write('isWebcamAlreadyCaptured: ',isWebcamAlreadyCaptured,'<br>');
});
原文链接:https://www.f2er.com/js/152737.html

猜你在找的JavaScript相关文章