gpt4 book ai didi

javascript - MediaStreamRecorder 不会触发 ondataavailable 事件

转载 作者:行者123 更新时间:2023-11-29 21:07:15 25 4
gpt4 key购买 nike

我正在使用开源 JavaScript 库 MediaStreamRecorder在 Mozilla Firefox 和 Google Chrome 中录制音频和视频 webRTC 通话。

通话录音成功,但我面临以下问题。

如果我在 multiStreamRecorder.start() 中使用 1 秒 (1000ms) 的时间间隔,则 multiStreamRecorder.ondataavailable 事件不会触发。这就是为什么没有错误或没有登录控制台的原因。

但是,如果我使用 1.5 秒(1500 毫秒)或更长的时间间隔,它会触发 multiStreamRecorder.ondataavailable 事件并且一切正常。(仅限视频案例)

我只想将间隔保持为 1 秒(1000 毫秒)。

var ws;

function start() {

ws = new WebSocket("wss://xyz/");

ws.onopen = function () {
console.log("WebSocket has been opened!");
};

ws.onmessage = function (message) {
console.log("A messsage is received from WebSocket Server.", message);
};

ws.onclose = function (e) {
console.log('WebSocket is closed. Reconnection will be attempted in 5 second.', e.reason);
setTimeout(function () {
start();
}, 5000);
};

ws.onerror = function (err) {
console.error('WebSocket encountered an error: ', err.message, 'Closing WebSocket');
ws.close();
};

}

start();


function startRecording(localStream, remoteStream) {

if (localStream != null && remoteStream != null) {

multiStreamRecorder = new MultiStreamRecorder([localStream, remoteStream], "video/webm");
multiStreamRecorder.mimeType = "video/webm";

multiStreamRecorder.ondataavailable = function (blob) {

console.log("sending blob to websocket server", blob);
ws.send(blob);

};

// It doesn't work with the 1000ms time interval
multiStreamRecorder.start(1500);

}
else{
console.error("One or more streams are null.");
}

}

最佳答案

我怀疑一秒钟不足以让相机流预热。虽然您可以立即将录音机附加到流中,但它似乎并没有在零时间内准备好播放/录音。

视频元素有.onloadedmetadata让你等待数据准备好;录音机没有。

不过你可以制作一个(Chrome 使用 https fiddle):

var haveLoadedMetadata = stream => {
let preview = document.createElement("video");
preview.srcObject = stream;
return new Promise(resolve => preview.onloadedmetadata = resolve);
};

var start = ms => navigator.mediaDevices.getUserMedia({video: true})
.then(stream => haveLoadedMetadata(stream)
.then(() => record(stream, ms))
.then(recording => {
stop(stream);
video.src = link.href = URL.createObjectURL(new Blob(recording));
link.download = "recording.webm";
link.innerHTML = "Download recording";
log("Playing "+ recording[0].type +" recording:");
}))
.catch(log);

var record = (stream, ms) => {
var rec = new MediaRecorder(stream), data = [];
rec.ondataavailable = e => data.push(e.data);
rec.start();
log(rec.state + " for "+ (ms / 1000) +" seconds...");
var stopped = new Promise((y, n) => (rec.onstop = y, rec.onerror = e => n(e.error || e.name)));
return Promise.all([stopped, wait(ms).then(() => rec.stop())])
.then(() => data);
};

var stop = stream => stream.getTracks().forEach(track => track.stop());
var wait = ms => new Promise(resolve => setTimeout(resolve, ms));
var log = msg => div.innerHTML += "<br>" + msg;
<button onclick="start(1000)">Record 1 second!</button>
<div id="div"></div><br>
<video id="video" height="120" width="160" autoplay></video>
<a id="link"></a>

(Chrome 和 Firefox 都直接实现了 MediaRecorder,所以我用它来回答)。

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com