WebRTC

test WebRTC feature

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>WebRTC</title>
    </head>
    <body>
        <video autoplay playsinline></video>
        <div>
            <button id="capture">capture</button>
        </div>
        <div>
            <canvas id="canvas"></canvas>
        </div>
        <div id="errorMessage"></div>
    </body>
    <script type="text/javascript">
        var video = document.querySelector('video');   
        
        if(!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia){
            console.log("your browser doesn't support getUserMedia");
            alert("your browser doesn't support getUserMedia");
        }else {
            var constraints = {                
                audio: true,
                video: true
            }
            navigator.mediaDevices.getUserMedia(constraints)
            .then(function(stream){
                video.srcObject = stream;
            })
            .catch(function(err) {
                console.log(err);
                alert(err);
            });    
        }
        var capture = document.getElementById('capture');
        var canvas = document.getElementById('canvas');
        canvas.width = 320;
        canvas.height = 240;
        capture.onclick = function() {
            canvas.getContext('2d').drawImage(video, 0, 0, canvas.width, canvas.height);
        }
    </script>
</html>
posted @ 2021-02-07 14:13  许仙儿  阅读(58)  评论(0编辑  收藏  举报