webRTC開啟攝像頭


配置htts之后就可以開啟webRTC了。

<!DOCTYPE html>
<html>
<head>
    <title>OpenCamera</title>
</head>
<body>
    <video id="video" autoplay></video>
</body>
    <script type="text/javascript">
        var getUserMedia=(navigator.getUserMedia ||navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia);
        getUserMedia.call(navigator,{
            video:true,
            audio:true
        },function(localMediaStream){
            
                var video =document.getElementById('video');
                video.src=window.URL.createObjectURL(localMediaStream);
                video.onloadedmetadata=function(e){
                console.log("Label: "+localMediaStream.id);
                console.log("AudioTracks",localMediaStream.getAudioTracks());
                console.log("VideoTracks ",localMediaStream.getVideoTracks());
            };
        },function(e){console.log("Reeeejected!",e);
    });
    </script>
</html>
View Code

這里給出了一個簡單的例子。需要用到H5的video標簽。通過webRTC注冊攝像頭和麥克風,生成mediastream然后作為video的輸出。

這里的id是該媒體流的唯一標識。

音頻和視頻被放到兩個數組中。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM