如何在加载HTML5视频时制作加载图像?
由于视频播放器加载mp4视频需要时间,HTML5是否支持在加载视频时播放“正在加载”标志?
由于我的asp.net应用程序是移动页面,因此需要用户单击视频才能播放视频(android、iphone不支持自动播放)。所以,我不能将“正在加载”标志制作为海报,否则用户会对此感到困惑。我想在用户单击 iPad 上的播放按钮时显示加载徽标。
谢谢
乔
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我花了很长时间才真正弄清楚如何做到这一点,但我将在这里分享它,因为我终于找到了一种方法!仔细想想,这很荒谬,因为加载是所有视频都必须做的事情。您可能认为他们在创建 html5 视频标准时会考虑到这一点。
我认为应该有效(但不会)的最初理论是,
简单,对吧?问题是,当设置源元素或设置 video.src 属性时,我无法显示背景图像。天才/幸运的最后一击是发现(通过实验)如果海报被设置为某种东西,背景图像将不会消失。我使用的是假海报图像,但我想它也可以与透明的 1x1 图像一起使用(但为什么担心有另一个图像)。因此,这可能是一种 hack,但它有效,而且我不必向我的代码添加额外的标记,这意味着它可以在我使用 html5 视频的所有项目中工作。
HTML
CSS(使用 JS 加载应用于视频的类)
JS
这对我来说非常适合,但仅在 mac 上的 chrome 和 safari 中进行了测试。如果有人发现错误或改进,请告诉我!
It took me a way too long to actually figure out how to do this, but I'm going to share it here because I FINALLY found a way! Which is ridiculous when you think about it, because loading is something that all videos have to do. You'd think they would have taken this into account when creating the html5 video standard.
My original theory that I thought should have worked (but wouldn't) was this
Simple, right? The problem was that I couldn't get the background image to show when the source elements were set, or the video.src attribute was set. The final stroke of genius/luck was to find out (through experimentation) that the background-image will not disappear if the poster is set to something. I'm using a fake poster image, but I imagine it would work as well with a transparent 1x1 image (but why worry about having another image). So this makes this probably a kind of hack, but it works and I don't have to add extra markup to my code, which means it will work across all my projects using html5 video.
HTML
CSS (loading class applied to video with JS)
JS
This works perfectly for me but only tested in chrome and safari on mac. Let me know if anyone finds bugs and or improvements!
还有一个在加载视频时添加预加载器图像的简单解决方案:
HTML:
海报是1px的透明图像。
CSS:
Also a simple solution to add a preloader image while loading the video:
HTML:
the poster is a transparent image 1px.
CSS:
使用标签 id 海报
更多信息请参见 http://www.w3schools.com/html5/att_video_poster .asp
Use the tag id poster
More info can be found http://www.w3schools.com/html5/att_video_poster.asp
您可以使用 JavaScript 创建一个带有动画“加载”gif 的图像叠加层,仅在视频正在加载且未准备好播放时显示该图像叠加层。
您必须编写 JavaScript 来与 Media API 链接,以检测视频何时准备好播放等(这样您就可以再次隐藏图像),但这应该是可能的。
You could probably do it with JavaScript by creating an image overlay with an animated "loading" gif which you only display when the video is loading and is not ready to play.
You'd have to write JavaScript to link up with the Media API for detecting when the video is ready to play and so forth though (so you could hide the image again), but it should be possible.
对于那些需要或喜欢仅使用 js 而不使用 css 的人:
我们之所以被迫使用 css 背景而不是仅仅设置 GIF 作为视频海报,是因为 Safari 不会播放 GIF,只会显示 GIF 动画的第一帧。然而,我们希望能够通过不向用户显示静态框架来在每个平台上提供最佳的用户体验。
解决方法:
3 帧动画示例,
假设您的 HTML 中有 2 个视频,
在 HTML 中
为了使其更好,您可以使用
ontimeupdate
而不是onplaying
,它会连续触发。调整延迟时间以满足您的需要。
注意:如有必要,请检查您是否需要 playsinline 来处理视频
在 Safari 中正确自动播放
。For those who need or prefer to do it with js only without using css:
The reason why we are forced to use css background instead of just setting a GIF as the video poster is because Safari will not play the GIF and will show only the first frame of the GIF animation. Yet we want to be able to offer the best UX on every platform by not showing the user a static frame.
The workaround:
Example with a 3 frame animation,
Let's say there are 2 videos in your HTML,
In your HTML
To make it even better you could use
ontimeupdate
instead ofonplaying
which would fire continuously.Adjust delay times to suit your needs.
Note: If necessary check whether you need playsinline to handle video
autoplay
correctly in Safari.我的解决方案是在 window.fbAsyncInit = function() 中添加以下内容:
Found: https://developers.facebook.com/docs/reference/javascript/FB.Event.subscribe/v2.12
My solution was to add the following inside of the window.fbAsyncInit = function():
Found: https://developers.facebook.com/docs/reference/javascript/FB.Event.subscribe/v2.12