简体   繁体   English

Cordova - 捕获视频并检索base64数据

[英]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. 我正在使用phonegap录制视频,我想保存base64数据编码的字符串。 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 . 我已经使用了文档中指定的readAsDataURL。 The output of evt.target.result is "data:video/mp4;base64," but there isn't any encoded data after the filetype. evt.target.result的输出是"data:video/mp4;base64," 但文件类型后没有任何编码数据。

Is there anything else I need to add in order to get the full base64 data of the video? 为了获得视频的完整base64数据,还有什么我需要添加的吗?

I am really struggling to find anything that can help me. 我真的很难找到任何可以帮助我的东西。 Any help would be greatly appreciated. 任何帮助将不胜感激。

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);
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM