zoukankan      html  css  js  c++  java
  • HTMLVideoElement.srcObject MediaStream MediaSource Blob File

    MediaSource

    <!DOCTYPE html>
    <html lang="zh">
    
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <style>
            video {
                background: black;
                position: relative;
                left: 50%;
                transform: translate(-50%, 0);
            }
        </style>
    </head>
    
    <body>
        <video width="854" height="480" muted></video>
    
        <script>
            var video = document.getElementsByTagName('video')[0];
            video.oncanplay = () => video.play();
    
            var mediaSource = new MediaSource();
            var obj_url = URL.createObjectURL(mediaSource);
            video.src = obj_url; // blob:http://127.0.0.1:5500/629075b2-e0e2-4ebb-96a7-37a1d0ebed2c
            var mimeCodec = 'video/mp4; codecs="avc1.42E01E, mp4a.40.2"';
    
            mediaSource.addEventListener('sourceopen', () => {
                var sourceBuffer = mediaSource.addSourceBuffer(`video/mp4; codecs="avc1.42E01E, mp4a.40.2"`);
    
                fetch(`https://nickdesaulniers.github.io/netfix/demo/frag_bunny.mp4`)
                    .then(async resp => {
                        var reader = resp.body.getReader();
    
                        sourceBuffer.onupdateend = async () => {
                            console.log(mediaSource.readyState); // open
                            const { done, value } = await reader.read();
                            if (done) {
                                return;
                            }
                            sourceBuffer.appendBuffer(value);
                        }
    
                        sourceBuffer.onupdateend();
                    });
            });
        </script>
    </body>
    
    </html>
    

    srcObject = MediaStream 播放摄像头

    <!DOCTYPE html>
    <html lang="zh">
    
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <style>
            video {
                background: black;
                position: relative;
                left: 50%;
                transform: translate(-50%, 0);
            }
        </style>
    </head>
    
    <body>
        <video width="854" height="480"></video>
    
        <script>
            var video = document.getElementsByTagName('video')[0];
    
            navigator.mediaDevices.getUserMedia({ video: true, audio: false })
                .then(mediaStream => {
                    video.srcObject = mediaStream;
                    video.oncanplay = () => {
                        video.play();
                    };
                })
                .catch(err => {
                    console.log(err);
                    alert('对不起,无法获取您的摄像头!');
                });
        </script>
    </body>
    
    </html>
    

    END

  • 相关阅读:
    左旋一个字符串和手摇反转法
    LCS
    游戏服务器学习_1
    面试题_带答案_2
    面试题_带答案
    安卓_13
    安卓_12activity
    安卓_12
    多盟_1
    安卓没删干净导致报错
  • 原文地址:https://www.cnblogs.com/develon/p/15213626.html
Copyright © 2011-2022 走看看