如何从HTML中的Blob对象播放视频?

时间:2018-09-11 05:59:32

标签: javascript file blob chunks

我的目标是分割文件,然后将其发送到另一个对等方。因为这些块是blob,我可以将文件拆分为blob,但是首先,我想在发送方使用整个blob对象播放视频。

在我的代码中,我正在做的是将文件转换为blob对象。这样做之后,我想读取blob,然后在html video元素中播放它,但是我无法播放视频。有人可以帮我吗,我被困在这里。

这是我的代码:

videoUpload(event) {

console.log("video event occured");
if (event.target.files && event.target.files[0]) {


  var reader2 = new FileReader();


  reader2.onload = (event: any) => {
    // CAN ANY ONE TELL ME HOW TO PLAY VIDEO BY USING THIS BLOB EVENT
  }


  **at this stage i am converting my file into the blob**
  var file = event.target.files[0];
  console.log(file.byteLength);
  var chunkSize = 6808850;
  var fileSize = file.size;
  var chunks = Math.ceil(file.size / chunkSize);
  var chunk = 0;

  console.log('file size..', fileSize);
  console.log('chunks...', chunks);

  while (chunk < chunks) {
    var offset = chunk * chunkSize;
    console.log('current chunk..', chunk);
    console.log('offset...', chunk * chunkSize);
    console.log('file blob from offset...', offset)
    var blob = file.slice(offset, offset + chunkSize);
    console.log(blob);
    chunk++;
  }

  // sending the blob to the event listener as array buffer, you can also tell me what "read as" method i can use.

  reader2.readAsArrayBuffer(blob);
}

}

0 个答案:

没有答案