Cordova - Capture video and retrieve base64 data

I am using phonegap to record a video and I am wanting to save the base64 data-encoded string. So far I have tried this..

function captureSuccess(mediaFiles) {
    var i, path, len;
    path = mediaFiles[0];
    win(path);
}

function win(file) {
    var reader = new FileReader();
    reader.onloadend = function (evt) {
        console.log("read success");
        console.log(evt.target.result);
    };
    reader.readAsDataURL(file); 
};

function captureError(error) {
    navigator.notification.alert('Error code: ' + error.code, null, 'Capture Error');
}

function captureVideo() {
    navigator.device.capture.captureVideo(captureSuccess, captureError, {limit: 1});
}

I have used readAsDataURL as specified in the documentation. The output of evt.target.result is "data:video/mp4;base64," but there isn't any encoded data after the filetype.

Is there anything else I need to add in order to get the full base64 data of the video?

I am really struggling to find anything that can help me. Any help would be greatly appreciated.

Answers:

Answer
var b64toBlobAlt = function(dataURI, contentType) {
  var ab, byteString, i, ia;
  byteString = atob(dataURI.split(',')[1]);
  ab = new ArrayBuffer(byteString.length);
  ia = new Uint8Array(ab);
  i = 0;
  while (i < byteString.length) {
    ia[i] = byteString.charCodeAt(i);
    i++;
  }
  return new Blob([ab], {
    type: contentType
  });
};
var path = mediaFiles[0].fullPath;

window.resolveLocalFileSystemURL(path, function(fileEntry) {
  return fileEntry.file(function(data) {
    var reader = new FileReader();
    reader.onloadend = function(e) {
      var blob = b64toBlobAlt(e.target.result, 'video/mp4');
      if (blob) {
         // do whatever you want with blob
        });
      }
    };
    return reader.readAsDataURL(data);
  });
});

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.