Get a stream of bytes from navigator.mediaDevices.getUserMedia()?

后端 未结 1 1613
轮回少年
轮回少年 2021-01-17 16:53

I am aware of how to retrieve eg. a video stream from a webcam source:

const constraints = { video: true };

navigator.mediaDevices

    .getUserMedia(constr         


        
1条回答
  •  -上瘾入骨i
    2021-01-17 17:33

    MediaStream Recording API

    By further investigating into MDN and the HTML 5 APIs related to Audio and Video I have found the MediaStream Recording API.

    So, to get the byte stream (or chunks as soon as some are available) we can do this:

    const constraints = { video: true };
    
    navigator.mediaDevices
    
        .getUserMedia(constraints)
    
        .then(mediaStream => {
    
            // use MediaStream Recording API
            const recorder = new MediaRecorder(stream);
    
            // fires every one second and passes an BlobEvent
            recorder.ondataavailable = event => {
    
                // get the Blob from the event
                const blob = event.data;
    
                // and send that blob to the server...
            };
    
            // make data available event fire every one second
            recorder.start(1000);
        });
    

    Browser Support:

    The MediaStream Recording API is still in Working Draft status (March 2018). Currently only natively supported in Chrome and Firefox.

    Polyfill: streamproc/MediaStreamRecorder

    Further reading: Record to an audio file

    0 讨论(0)
提交回复
热议问题