【问题标题】:Cordova - Capture video and retrieve base64 dataCordova - 捕获视频并检索 base64 数据
【发布时间】:2014-11-04 10:41:03
【问题描述】:

我正在使用 phonegap 录制视频,我想保存 base64 数据编码的字符串。到目前为止,我已经尝试过这个..

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

我使用了 documentation 中指定的 readAsDataURL。 evt.target.result 的输出是 "data:video/mp4;base64," 但文件类型后面没有任何编码数据。

为了获取视频的完整 base64 数据,我还需要添加什么吗?

我真的很难找到任何可以帮助我的东西。任何帮助将不胜感激。

【问题讨论】:

  • 嘿 Pooshonk,你找到解决办法了吗?

标签: javascript cordova encoding base64


【解决方案1】:
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);
  });
});

【讨论】:

  • 嗨 kevohagan,请考虑添加关于您的代码如何工作的解释。
  • @kevohagan 我的代码卡在reader.readAsDataURL(data)。它没有给出任何错误,但没有出现在 onloadend 函数中。
猜你喜欢
  • 2018-01-23
  • 2015-05-25
  • 2016-09-18
  • 2015-10-02
  • 2010-12-30
  • 1970-01-01
  • 2017-09-17
  • 2015-07-05
  • 1970-01-01
相关资源
最近更新 更多