javascript HTML5:从存储的二进制字符串播放视频
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16251136/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HTML5: Play video from stored binary string
提问by source.rar
I am trying to read the contents of a video file as a binary string using the FileReader.readAsBinaryString(Blob|File) as shown in the example http://www.html5rocks.com/en/tutorials/file/dndfiles/#toc-reading-filesand then store and play the video.
我正在尝试使用 FileReader.readAsBinaryString(Blob|File) 将视频文件的内容作为二进制字符串读取,如示例http://www.html5rocks.com/en/tutorials/file/dndfiles/#toc 中所示-读取文件,然后存储和播放视频。
I tried it using the below (with a webm video file),but get a "Video format or MIME type not supported."
我尝试使用下面的(带有 webm 视频文件),但得到“不支持视频格式或 MIME 类型”。
function readBlob (file, startByte, endByte, callback) {
console.log('readBlob():', file, startByte, endByte);
var reader = new FileReader();
reader.onloadend = function (evt) {
if (evt.target.readyState == FileReader.DONE) {
callback(evt.target.result);
var player = document.getElementById('player');
player.src = "data:video/webm;base64,"+evt.target.result;
player.load();
player.play();
}
}
var blob = file.slice(startByte, endByte);
reader.readAsBinaryString(blob);
}
Does anyone know if it is possible to read a video file (one supported by the browser being used) as a binary string and play it in the browser HTML5 video player?
有谁知道是否可以将视频文件(正在使用的浏览器支持的文件)作为二进制字符串读取并在浏览器 HTML5 视频播放器中播放?
TIA
TIA
回答by lostsource
Your problem might be with the player.src
您的问题可能出在 player.src
player.src = "data:video/webm;base64,"+evt.target.result;
It is expecting the data to be in base64 but you're giving it a binary string.
它期望数据在 base64 中,但您给它一个二进制字符串。
Try encoding it to base64 using btoa
尝试使用btoa将其编码为 base64
player.src = "data:video/webm;base64,"+btoa(evt.target.result);
回答by sebilasse
How about FileReader.readAsDataURL(Blob|File)
?
It is explained in your html5rocks-link as well.
怎么样FileReader.readAsDataURL(Blob|File)
?
它也在您的 html5rocks-link 中进行了解释。