简体   繁体   English

Safari 和 Chrome 中的 HTML5 视频海报属性

[英]HTML5 Video poster attribute in Safari and Chrome

In Firefox the image specified by the poster attribute of the VIDEO tag remains on screen until the play button is pressed.在 Firefox 中,由VIDEO标签的海报属性指定的图像会一直显示在屏幕上,直到按下播放按钮。 However in the Webkit browsers (Safari and Chrome) the poster is dumped in favor of the first frame from the video as soon as the video metadata is fetched.然而,在 Webkit 浏览器(Safari 和 Chrome)中,一旦获取视频元数据,海报就会转储到视频的第一帧。

I want to avoid having to place the poster on top of the video element manually if I could.如果可以的话,我想避免手动将海报放在视频元素的顶部。 Does anyone know of a fix for this?有谁知道解决这个问题?

<video src="some_url" poster="images/poster.jpg">
    <source type="video/ogg" src="some_url" />
</video>

It seems that WebKit strips the attribute as soon as the video is fethced, because iOS 3.x for Iphone and Ipad has a serious bug where it is not possible to play the video at all when there is a poster attribute specified.似乎 WebKit 会在视频被提取后立即剥离该属性,因为适用于 Iphone 和 Ipad 的 iOS 3.x 有一个严重的错误,即在指定海报属性时根本无法播放视频。 This was fixed in iOS 4, but the workaround still stays, even in Safari 5...There are a lot of users who didn't upgrade to iOS 4 yet, so no luck with the poster...这在 iOS 4 中得到了修复,但解决方法仍然存在,即使在 Safari 5 中......有很多用户还没有升级到 iOS 4,所以海报不走运......

I'm going to try and position the image absolutely over the video using Javascript, and removing it when the video is played - that seems like the best solution...我将尝试使用 Javascript 将图像绝对定位在视频上,并在播放视频时将其删除 - 这似乎是最好的解决方案......

If you can get away with not preloading the video you can set preload="none" on the video element.如果您可以不预加载视频,则可以在视频元素上设置 preload="none"。 In Safari this results in the poster being displayed.在 Safari 中,这会导致显示海报。

Safari on iOS probably sets preload="none" as the default to save bandwidth, while the desktop version preloads unless you explicitly tell it not to. iOS 上的 Safari 可能将 preload="none" 设置为默认设置以节省带宽,而桌面版本会预加载,除非您明确告诉它不要。

It's 2020 Jan. so you may figure out the solution.现在是 2020 年 1 月,所以您可能会想出解决方案。 but I left my solution for other people.但我把我的解决方案留给了其他人。 Many people say it's a bug.很多人说是BUG。 but I disagree.但我不同意。 It's just a difference between firefox and chrome/edge.这只是 Firefox 和 chrome/edge 之间的区别。

Suppose you have <video preload="metadata"> in your HTML and post to the server thumbnail image.假设您的 HTML 中有<video preload="metadata">并发布到服务器缩略图。 then you'll change the poster value to the src server gives to you.然后您将海报值更改为 src 服务器提供给您的值。 like this:像这样:

xhr.addEventListener("load", function() {
    if (this.status === 200) {
      const data = JSON.parse(this.responseText);
      player.poster = `/image/${data.thumbnailMain}`; // change the poster value
      player.load();                                  // then load.
    } else {
      console.error(this.responseText);
    }
  });

In firefox, video tag is designed when poster property value changes, it will load automatically.在 firefox 中,视频标签是在海报属性值改变时设计的,它会自动加载。 but chrome/edge won't load() ;但 chrome/edge 不会load() so you should player.load() manually;所以你应该手动player.load()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM