I am unable to get blobs recorded by an instance of MediaRecorder that are sent over WebSocket to playback on the client machine's video element. Here is an excerpt of my code:
sender.js:
navigator.getUserMedia({
video: {
facingMode: 'user'
},
audio = true
}).then((stream) => {
const recorder = new MediaRecorder(stream);
recorder.ondataavailable((event) => {
socket.send(event.data); // according to the client console, THIS SENDS A BLOB!!!
});
recorder.start(1000);
});
receiver.js:
socket.onmessage = (message) => {
console.log(message.data); // instanceof Blob
blob = message.data;
// tried this answer but it still does not work: https://stackoverflow.com/a/72772924/1079320
blob = new Blob([message.data], { // the Blob.type attribute is ignored when sent by WebSocket: https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/send
type: 'video/webm; codecs=vp9'
});
// blob = blob.slice(0, blob.size, 'video/webm; codecs=vp9'); // this answer does not work: https://stackoverflow.com/a/50875615/1079320
video.srcObject = blob; // under "Usage notes" from https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/srcObject the URL.createObjectUrl() method is no longer needed!
video.play(); // const video = document.getElementById('video-element-id');
};
I get no errors, but the video element simply does not play. Testing using the Firefox browser.
recorder.ondataavailable(event => {should berecorder.ondataavailable = (event) => {}orrecorder.addEventListener("dataavailable", (event) => {}). Ifmessage.datais aBloband not anArrayBufferyou don't have to create a newBlob, just use theBlobdefined on theevent. Additionally,Blobset assrcwon't work. You are not setting aMediaStreamtosrcObject. Pass theBlobtoURL.createObjectURL()then set the Blob URL as theHTMLMediaElementsrc.