JS检查浏览器是否支持摄像头

 方式一、

方式二、

let videoNum = 0,
microphoneNum = 0,
deviceList = [];
navigator.mediaDevices.enumerateDevices().then(devices = >{
	devices.forEach(device = >{
		//console.log(device);
		deviceList.push(device.kind);
		if (device.kind === "videoinput") videoNum++;
		if (device.kind === "audioinput") microphoneNum++;
	});
	console.log("设备列表:", deviceList);
	document.querySelector('#videoNum').value = "摄像头[" + videoNum + "]个";
	document.querySelector('#microphoneNum').value = "麦克风[" + microphoneNum + "]个";
}).
catch(function(err) {
	document.querySelector('#videoNum').value = "摄像头[" + videoNum + "]个";
	document.querySelector('#microphoneNum').value = "麦克风[" + microphoneNum + "]个";
	alert(err.name + ": " + err.message);
});

参照:https://cloud.tencent.com/developer/ask/145992

WEBRTC官方提供的检测方法:https://developer.mozilla.org/zh-CN/docs/Web/API/MediaDevices/enumerateDevices

 

你可能感兴趣的:(JS)