XHR加载视频

发布于 2024-11-06 19:08:14 字数 1006 浏览 0 评论 0原文

在不详细了解我为什么使用 XHR 的情况下,谁能告诉我如何让以下内容发挥作用?我的目标是首先加载视频数据,然后将其放入视频标签的源中。

http://jsfiddle.net/u2vhG/

<!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>

Without getting into the details of why I'm using XHR, can anyone tell me how to get the below to work doing so? My goal is to first load the video data and then place it into the video tag's source.

http://jsfiddle.net/u2vhG/

<!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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

属性 2024-11-13 19:08:14

请参阅 Google Chrome 的此解决方法直到实现responseType = "blob"。

- 我,回答你同样的问题。

如果你阅读了我的整个答案并且通过该链接,您会看到 Google Chrome 实际上尚不支持 responseType = "blob"。我为您提供了一个假设的实现,并使用 responseType = "arraybuffer" 将您链接到解决方法。

Refer to this workaround for Google Chrome until responseType = "blob" is implemented.

Me, responding to your same question.

If you read my whole answer and the link, you'd see Google Chrome doesn't actually support responseType = "blob" yet. I gave you a hypothetical implementation and linked you to a workaround using responseType = "arraybuffer".

一念一轮回 2024-11-13 19:08:14

你这里有两个问题,

  1. req.responseType = "blob"; 应该出现在之后 req.open(...)
  2. 你正在做一个跨站点请求,出于安全原因可能会失败。

设置 url 后,您还应该URL.revokeObjectURL。请注意,在 Chrome 中,它们仍然具有 webkitURL 而不是 URL

You have two problems here,

  1. req.responseType = "blob"; should come after req.open(...)
  2. You are doing a cross-site request, which might fail for security reasons.

You should also URL.revokeObjectURL once you set the url. Note that in Chrome they still have webkitURL rather than URL.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文