HTML5 视频 - 加载百分比?
有谁知道我需要查询什么事件或属性才能获取 HTML5 视频已加载量的百分比?我想画一个 CSS 样式的“加载”栏,其宽度代表这个数字。就像 You Tube 或任何其他视频播放器一样。
因此,就像您管一样,即使整个视频尚未加载,视频也会播放,并向用户提供有关视频已加载和剩余加载量的反馈。
就像 YouTube 上的红条:
Does anyone know what event or property I need to query in order to get a percentage figure of the amount an HTML5 video has loaded? I want to draw a CSS styled "loaded" bar that's width represents this figure. Just like You Tube or any other video player.
So just like you tube a video will play even if the whole video hasn't loaded and give the user feedback on how much of the video has loaded and is left to load.
Just like the Red Bar on YouTube:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
下载一些数据后会触发
progress
事件,每秒最多 3 次。 浏览器通过buffered
属性提供可用媒体范围的列表; MDN 上的媒体缓冲、搜索和时间范围提供了这方面的完整指南。单一加载开始
如果用户不跳过视频,文件将在一个
TimeRange
中加载,并且buffered
属性将有一个范围:要知道该范围有多大范围是,这样读:
多个加载开始
如果用户在加载时更改播放头位置,则可能会触发新的请求。这会导致
buffered
属性碎片化:请注意缓冲区的数量如何变化。
由于它不再是连续的加载,因此“加载百分比”不再有多大意义。您想知道当前的 TimeRange 是什么以及加载了多少。在此示例中,您将获得负载条应开始的位置(因为它不为 0)和结束位置。
The
progress
event is fired when some data has been downloaded, up to three times per second. The browser provides a list of ranges of available media through thebuffered
property; a thorough guide to this is available on Media buffering, seeking, and time ranges on MDN.Single load start
If the user doesn't skip through the video, the file will be loaded in one
TimeRange
and thebuffered
property will have one range:To know how big that range is, read it this way:
Multiple load starts
If the user changes the playhead position while it's loading, a new request may be triggered. This causes the
buffered
property to be fragmented:Notice how the number of the buffer changes.
Since it's no longer a contiguous loaded, the "percentage loaded" doesn't make a lot of sense anymore. You want to know what the current
TimeRange
is and how much of that is loaded. In this example you get where the load bar should start (since it's not 0) and where it should end.其他遮阳篷对我不起作用,所以我开始深入研究这个问题,这就是我想到的。该解决方案使用 jquery 制作进度条。
我希望这也对其他人有帮助
The other awnsers didn't work for me so I started digging into this problem and this is what I came up with. The solutions uses jquery to make an progressbar.
I hope this helps others as well
已加载字符串的百分比修复..输出类似 #loaded 元素内已加载 99% 的内容...
Percentage fix for loaded string.. Output something like 99% loaded inside #loaded element...
我认为更新缓冲进度条的最佳事件是 timeupdate。每当媒体时间更新时,事件就会被触发。
它提供了缓冲属性,我们可以像这样使用
该事件的最佳优点是在播放媒体时触发该事件。而当浏览器下载并通知媒体时,会触发进度事件。
所以就像 youtube 一样,缓冲百分比只能在播放媒体时显示
I think best event to update the buffered progress bar is timeupdate. whenever time of the media is updated event is fired.
It gives buffered property which we can use like this
Best advantage of this event is this is fired when media is played. Whereas progress event is fired when media is downloaded and notified by browser.
So just like youtube, buffered percentage can only be shown when media is played
我的答案比所有其他答案都好,因为您想在视频暂停时更新缓冲区进度。这种情况发生在进度事件中。当进度失败时,时间更新事件就会触发,有时也会发生这种情况。
你只需要 video.buffered.end(i)。
My answer is better than all of the other ones because you want to update buffer progress when the video is paused. This happens with the progress event. The time update event fires when progress fails, as it sometimes does.
you only need video.buffered.end(i).