动态创建的视频元素上的captureStream()

时间:2017-07-28 01:36:55

标签: javascript html5 firefox html5-video webrtc

我正在尝试从动态创建的视频元素中捕获流。当我获得视频元素并尝试使用videoElement.captureStream()时,Firefox返回 videoElement.captureStream不是函数,但相同的代码适用于Chrome。问题似乎Firefox有动态创建的视频元素的问题。我尝试使用变异观察器检测新添加的视频元素并访问captureStream方法而没有运气我在下面发布了代码示例(我正在使用适用于WebRTC https://webrtc.github.io/adapter/adapter-latest.js的适配器。)

(function (win) {

    var listeners = [],
        doc = win.document,
        MutationObserver = win.MutationObserver || win.WebKitMutationObserver,
        observer;

    function ready(selector, fn) {
        listeners.push({
        selector: selector,
        fn: fn
        });
        if (!observer) {
        observer = new MutationObserver(check);
        observer.observe(doc.documentElement, {
            childList: true,
            subtree: true
        });
        }
        check();
    }

    function check() {
        for (var i = 0, len = listeners.length, listener, elements; i < len; i++) {
        listener = listeners[i];
        elements = doc.querySelectorAll(listener.selector);
        for (var j = 0, jLen = elements.length, element; j < jLen; j++) {
            element = elements[j];
            if (!element.ready) {
            element.ready = true;
            listener.fn.call(element, element);
            }
        }
        }
    }

    win.ready = ready;

})(this);



ready('.jw-video',function(element){
    var stream = null;
    element.addEventListener('play',function(e){
    //with firefox here e.target.captureStream or element.captureStream returns error
    stream = e.target.captureStream();

    var mediaRecorder = new MediaRecorder(stream);
    mediaRecorder.onerror = function(error){
        console.log("media recorder error:",error);
    }
    mediaRecorder.start();
    mediaRecorder.ondataavailable = function(evt){
        console.log("mediaRecorderData");
        var blob = evt.data;

        var fileReader = new FileReader();
        var arraybuffer;
        fileReader.onload = function(){
            arraybuffer = fileReader.result;
            console.log("arraybuffer");
            console.log(arraybuffer);
            chunks.push(arraybuffer);
            sendChunks(chunks);
        };
        fileReader.readAsArrayBuffer(blob);
    }
    window.setInterval(function(){
        mediaRecorder.requestData();                            
    },3000);

    });
});

有没有人知道如何访问新添加的视频元素的captureStream方法,该方法既可以在Chrome和Firefox上运行,也可以采用其他方法捕获流并将其从JWPlayer转换为arraybuffer?

1 个答案:

答案 0 :(得分:2)

不,问题只是Firefox仍然是HTMLMediaElement#captureStream的前缀。

因此,您需要致电video.mozCaptureStream(),即使是内联视频元素也是如此。

&#13;
&#13;
console.log(`In document : 
captureStream : ${inDoc.captureStream}
mozCaptureStream : ${inDoc.mozCaptureStream}
`)

var dyn = document.createElement('video');
console.log(`Dynamic : 
captureStream : ${dyn.captureStream}
mozCaptureStream : ${dyn.mozCaptureStream}
`)
&#13;
<video id="inDoc">
&#13;
&#13;
&#13;

所以为了支持chrome和FF:

let stream = video.mozCaptureStream ? video.mozCaptureStream() : video.captureStream();