H5实现多摄像头视频源显示

准备环境:
此功能要基于http服务器,将下面的代码放到任意的web服务器下运行即可

<html>
<head>
<title>2video</title>
<script>
function gotDevices(deviceInfos) {
    let i = 0;
    deviceInfos.map(function(deviceInfo){
        if (deviceInfo.kind === 'audioinput') {
        // option.text = deviceInfo.label || `microphone ${audioInputSelect.length + 1}`;
    } else if (deviceInfo.kind === 'audiooutput') {
        // option.text = deviceInfo.label || `speaker ${audioOutputSelect.length + 1}`;
    } else if (deviceInfo.kind === 'videoinput') {
            console.log(deviceInfo.deviceId);
            constraints = {
                video: {deviceId: {exact: deviceInfo.deviceId}}
            };
            navigator.mediaDevices.getUserMedia(constraints).then((function(i){
                return (stream)=>{
                    document.getElementById('view' + i).srcObject = stream;
                }
            })(i));
            i++;
        } else {
        console.log('Some other kind of source/device: ', deviceInfos);
    }
    });
}
 init = () => {
    let constraints = {
        video: {deviceId: undefined}
    };
    navigator.mediaDevices.getUserMedia(constraints).then(()=>{
        navigator.mediaDevices.enumerateDevices().then(gotDevices);
    });
}

window.onload = function(){
      init();
}
</script>
</head>
<body>
<video width="320" height="240" id="view0" playsinline autoplay></video>
<video width="320" height="240" id="view1" playsinline autoplay></video>
</body>
</html>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容