网页 测试摄像头和麦克风 工具页 html5+javascript + layui

(95) 2024-06-10 13:01:01

图示:

网页 测试摄像头和麦克风 工具页 html5+javascript + layui (https://mushiming.com/)  第1张

代码:

<!DOCTYPE html>
<html lang="zh">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>打开摄像头测试</title>
	<link rel="stylesheet" href="./css/layui.css">
</head>
<body class="layui-container">
	<div class="layui-panel">
	  <div style="padding: 32px;margin: auto;text-align:center;">
		  <h1>摄像头测试<h1>
		  <br>
		  <div id="app"><video autoplay controls id="video"></video></div>
		  <button id="cameraBtn" type="button" class="layui-btn">打开摄像头测试</button>
	  </div>
	</div>
	<script type="text/javascript">
		document.querySelector('#cameraBtn').onclick = function() {
			let constraints = {
				audio: true,
				video: true
			};
			navigator.mediaDevices.getUserMedia(constraints).then(function(stream) {
				document.querySelector('#video').srcObject = stream
			}).catch(function(err) {
				console.log(err.name + ":" + err.message);
			});
		}
	</script>
</body>
</html>

THE END

发表回复