在不详细说明我为什么使用 XHR 的情况下,谁能告诉我如何让下面的代码正常工作?我的目标是首先加载视频数据,然后将其放入视频标签的源中。
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
window.onload = function() {
var elem = document.getElementById("myVideo");
var req = new XMLHttpRequest();
req.onload = function () {
var blob_uri = URL.createObjectURL(this.response);
elem.appendChild(document.createElement("source"))
.src = blob_uri;
};
req.responseType = "blob";
req.open('GET', 'http://www.latentmotion.com/player/h264/clips/16154_2832659676_170_180.mp4', false);
req.send(null);
};
</script>
</head>
<body>
<video id="myVideo" width="600" height="334" controls></video>
</body>
</html>
最佳答案
Refer to this workaround for Google Chrome until responseType = "blob" is implemented.
— Me, responding to your same question.
如果您阅读了我的全部回答和链接,您会发现 Google Chrome 浏览器实际上还不支持 responseType = "blob"
。我为您提供了一个假设的实现,并使用 responseType = "arraybuffer"
将您链接到解决方法。
关于javascript - XHR加载视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5997839/