如何停止视频的预加载(HTML5 - Javascript)

发布于 2024-09-12 03:03:52 字数 176 浏览 7 评论 0原文

我有一个带有视频播放器的网页,预加载了 3 个视频(同一视频的低质量、中质量和高质量)。然后,当用户单击与所需版本相对应的按钮时,视频将打开。

我想做的是停止另外两个视频的预加载。

这可能吗?换句话说,是否可以使用某些 Javascript 来取消或即时停止 HTML5 Video 标记的“预加载”属性?

I have a web page with a video player preloading 3 videos (low, med, and high quality of the same video). Then, when the user clicks on one the button corresponding to the desired version, the video opens.

What I would like to do is to then stop the preloading of the two other videos.

Is that possible? In other words, can the "preload" attribute of the HTML5 Video tag be cancelled or stopped on the fly with some Javascript ?

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

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

发布评论

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

评论(3

爱殇璃 2024-09-19 03:03:53

使用 jQuery,您可以尝试:

$('#videoPlayerId').removeAttr('preload');

我不知道它会停止已经预加载的视频。

从 UI 角度来看,为什么要尝试同时预加载所有 3 个视频?这会减慢所有三个的加载速度;如果您只预加载其中之一,则更多视频将有机会在用户开始观看之前进行缓冲。

我建议预加载默认质量的视频之一,并且仅在用户选择时才加载不同质量的视频。这是 YouTube、Netflix 和其他公司使用的行为。

With jQuery, you can try:

$('#videoPlayerId').removeAttr('preload');

I don't know that it will stop a video that's already preloading.

From a UI perspective, why are you trying to preload all 3 videos at the same time? This will slow down the loading speed of all three; if you only preload one of them, more of the video will have a chance to buffer before the user starts viewing it.

I would suggest preloading one of the videos of a default quality and only loading a different quality video if the user selects it. This is the behaviour used by YouTube, Netflix, and others.

荒人说梦 2024-09-19 03:03:53

现在有专门的标签:

<video preload="none" ....>

There is dedicated tag nowadays:

<video preload="none" ....>
娇女薄笑 2024-09-19 03:03:52

我刚刚想出了一个解决方案来解决我遇到的与您类似的问题。我正在页面上预加载一系列电影列表,但我需要能够跳转到其中一个电影,并将其优先于可能已经预加载的电影,以便尽快播放。

我有一个 div#prebuffer 元素,用于保存预加载的视频,因为它们是缓冲的。当我需要忘记预加载时,我只需这样做:

var $video = $('#prebuffer video:last');
$video.find('source').attr('src', '');
$video[0].load();
// it has now stopped preloading.
// and then, because I don't want this half-loaded broken cruft hanging around:
$video.remove();

它有点难看,但我无法找到更好的方法。它完成了工作。

I just came up with a solution to a problem I had that resembles your own. I am preloading a list of movies on my page, in series, but I need to be able to jump to one of them and prioritize it ahead of whatever might have already been preloading, in order to play it as quickly as possible.

I have a div#prebuffer element that holds the preloaded videos, as they are buffered. When I need to forget about preloading, I simply do this:

var $video = $('#prebuffer video:last');
$video.find('source').attr('src', '');
$video[0].load();
// it has now stopped preloading.
// and then, because I don't want this half-loaded broken cruft hanging around:
$video.remove();

It's slightly ugly, but I wasn't able to find a nicer way. And it gets the job done.

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