1. <!doctype html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport"
    6. content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    7. <meta http-equiv="X-UA-Compatible" content="ie=edge">
    8. <title>Document</title>
    9. </head>
    10. <body>
    11. <video id="video" controls autoplay></video>
    12. </body>
    13. </html>
    14. <script>
    15. // getUserMedia 可以传其他参数
    16. window.navigator.mediaDevices.getUserMedia({
    17. audio:true, // 获取音频
    18. video:{ // 获取视频 并设置宽高
    19. width:400,
    20. height:300
    21. }
    22. })
    23. .then(mediaStream =>{
    24. const oVideo = document.getElementById('video');
    25. // 把媒体流放到 video 上
    26. oVideo.srcObject = mediaStream;
    27. })
    28. </script>