javascript 如何在视频播放结束时显示 html5 视频海报?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22859656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 23:54:11  来源:igfitidea点击:

How to show html5 video poster end of video play?

javascriptjqueryhtmlhtml5-video

提问by Logan

I want to show video poster after play. I am trying following code but no luck.

我想在播放后显示视频海报。我正在尝试以下代码,但没有运气。

var video=$('#cms_video').get(0);
video.play();
video.addEventListener('ended',function(){
    this.posterImage.show();
});  

回答by Arnaud Leyder

A more straightforward way of doing this:

一种更直接的方法:

<script type="text/javascript">
var video= $('#cms_video').get(0);       
video.addEventListener('ended',function(){
    video.load();     
},false);
</script>

Which is a shortcut to loganphp answer. Indeed when changing the src of a video tag you implicitly call a load() on it.

这是 loganphp 答案的快捷方式。实际上,在更改视频标签的 src 时,您会隐式调用它的 load()。

This method has the caveat to request the media URL again and depending on caching settings it may re-download the full-length media resource causing unnecessary network/CPU usage for the client. But still it does answer the question the way loganphp asked it.

此方法有一个警告,即再次请求媒体 URL,并且根据缓存设置,它可能会重新下载完整的媒体资源,从而导致客户端不必要的网络/CPU 使用。但它仍然以 loganphp 的方式回答了这个问题。

If you want to bypass this caveat you can use and overlay image/div and bind a click/touchstart event on it to show the video tag and hide the overlay. When the ended event has fired just hide the video tag and show the overlay image.

如果您想绕过这个警告,您可以使用并覆盖图像/div 并在其上绑定一个 click/touchstart 事件以显示视频标签并隐藏覆盖层。当结束事件触发时,只需隐藏视频标签并显示叠加图像。

回答by Logan

Finally I achieved my goal with following code

最后我用以下代码实现了我的目标

var video=$('#cms_video').get(0);        
video.play();
video.addEventListener('ended',function(){
    v=video.currentSrc;
    video.src='';
    video.src=v;            
});

回答by keypaul

Simply at the end of video, show a div with the same src of the poster (with upper z-index or hide the video) If you need to replay the video, bind a click event on the showed div (to switch visibility and replay)

只需在视频的末尾,显示一个与海报 src 相同的 div(使用上 z-index 或隐藏视频)如果您需要重播视频,请在显示的 div 上绑定一个点击事件(以切换可见性和重播)

回答by Niraj patel

**video start autoplay and Poster showing at end of video **

**视频开始自动播放并在视频结束时显示海报**

<script src="http://ajax.googleapis.com/ajax/libs/prototype/1.7/prototype.js"></script>
<body>
<script type="text/javascript">
document.observe('dom:loaded', function(evt){
    $$('video').each(function(elm){
        elm.play();
      var wrapper = elm.wrap('span');
      var vid = elm.clone(true);
      elm.observe('ended', function(){
        wrapper.update(vid);
     });
    });
});

</script>
<video id="myvideo" poster="1.png" >
    <source src="1.mp4" type="video/mp4" />
</video>

回答by Felix

Try to give your poster an idor class then you can do:

尝试给你的海报一个id或类然后你可以这样做:

var video=$('#cms_video').get(0);
video.play();
video.addEventListener('ended',function(){
    $('#poster').show();
    // ^ Use . here if you apply class instead of if for your poster 
});