这是我的完整代码,用于显示和实时录制音频和视频(然后将Blob块上传到服务器):

$(function () {
    var handleSuccess = function(stream) {

        var player = document.querySelector("#vid-user");
        player.srcObject = stream;

        console.log("Starting media recording")
        var options = {mimeType: 'video/webm'};
        var recordedChunks = [];
        var mediaRecorder = new MediaRecorder(stream, options);

        mediaRecorder.ondataavailable = function(e) {
            console.log("Data available")
            if (e.data.size > 0) {
                recordedChunks.push(e.data);
                var url = URL.createObjectURL(new Blob(recordedChunks));
                console.log("URL: " + url)
            }
        }

        mediaRecorder.start();
    };
    navigator.mediaDevices.getUserMedia({ audio: true, video: true })
        .then(handleSuccess)
})

视频播放正常,但是问题是mediaRecorder.ondataavailable没有被触发/调用。这可能是什么问题?

最佳答案

start()方法采用一个称为timeslice的可选参数。除非您指定该参数,否则dataavailable事件仅在调用stop()上的MediaRecorder之后才会触发。

https://developer.mozilla.org/en-US/docs/Web/API/MediaRecorder/start

关于javascript - MediaRecorder 'ondataavailable'事件未触发,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60971938/

10-12 23:49