Thanks for all of your previous help.
I am testing this new code, where video is captured/recorded successfully. However, I don’t have the upload part correct. It’s showing this error:
“Uncaught TypeError: Cannot read property ‘name’ of undefined
at uploadFile (index.html:166)
at HTMLButtonElement.onclick (index.html:76)”
Line 166 is:
formData.append('video-filename', blob.name);
Line 76 is:
<button id="uploadFile" onclick="uploadFile()">Upload</button>
Here’s the pertinent html:
<button id="button1" onclick="startRecording()">Start</button>
<button id="button2" onclick="endRecording()">Stop</button>
<button id="uploadFile" onclick="uploadFile()">Upload</button>
<video id="video" autoplay="autoplay" controls="controls" poster="/thumbnail.jpg"
type="video/mp4" muted playsInline></video>
And the JS:
function supportsRecording(mimeType)
{
if (!window.MediaRecorder)
return false;
if (!MediaRecorder.isTypeSupported)
return mimeType.startsWith("audio/mp4") || mimeType.startsWith("video/mp4");
return MediaRecorder.isTypeSupported(mimeType);
}
var video = document.querySelector('#video');
let blobs = [];
let stream;
let mediaRecorder;
async function startRecording()
{
stream = await navigator.mediaDevices.getUserMedia({ audio: true, video: true });
mediaRecorder = new MediaRecorder(stream);
mediaRecorder.ondataavailable = (event) => {
// Let's append blobs for now, we could also upload them to the network.
if (event.data)
blobs.push(event.data);
}
mediaRecorder.onstop = doPreview;
// Let's receive 1 second blobs
mediaRecorder.start(1000);
}
function endRecording()
{
// Let's stop capture and recording
mediaRecorder.stop();
stream.getTracks().forEach(track => track.stop());
}
function doPreview()
{
if (!blobs.length)
return;
// Let's concatenate blobs to preview the recorded content
video.src = URL.createObjectURL(new Blob(blobs, { type: mediaRecorder.mimeType }));
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function uploadFile() {
let formData = new FormData();
}
var blob, fileName, fileObject;
function uploadBlob(blob) {
prepareBlob(blob);
InitUploading();
}
function prepareBlob(blob){
// generating a random file name
fileName = getFileName('mp4');
// we need to upload "File" --- not "Blob"
fileObject = new File([blob], fileName, {
type: 'video/mp4'
});
}
function InitUploading()
{
uploadFile(fileObject, function (response, fileDownloadURL) {
if (response !== 'ended') {
document.getElementById('header').innerHTML = response; // upload progress
return;
}
document.getElementById('header').innerHTML = '<a href="' + fileDownloadURL +
'" target="_blank">' +
fileDownloadURL + '</a>';
//alert('Successfully uploaded recorded blob.');
// preview uploaded file
video.src = fileDownloadURL;
// open uploaded file in a new tab
window.open(fileDownloadURL);
});
}
function uploadFile(blob, callback) {
// create FormData
var formData = new FormData();
formData.append('video-filename', blob.name);
formData.append('video-blob', blob);
callback('Uploading recorded-file to server.');
makeXMLHttpRequest('save1.php', formData, function (progress) {
if (progress !== 'Upload Complete') {
callback(progress);
return;
}
// var initialURL = 'uploads/' + blob.name;
callback('ended', initialURL);
});
}
Everything works successfully up to the ///////////////////////////////////////////////////////////
Ultimately, I’m trying to successfully upload upon upload click, but I first need to resolve those errors. Any help with solving those errors is greatly appreciated.
Any comments/guidance with the upload code portion is equally welcomed.