html5视频在MouseOut时重新显示海报图像

发布于 2024-11-05 15:04:15 字数 485 浏览 2 评论 0原文

如何“停止” html5 视频播放并恢复为海报图像?在调用 this.play() 之前,海报图像会正常显示。

我的代码:

<video loop="loop" onMouseOver="this.play();" onMouseOut="this.pause();" poster="/oceans-clip.thumb.jpg">
    <source src="/oceans-clip.mp4" type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"' />
    <source src="/oceans-clip.webm" type='video/webm; codecs="vp8, vorbis"' />
</video>

这按预期工作,但是我想在MouseOut 上恢复到海报图像,而不是仅仅暂停视频。有什么好的方法可以做到这一点?

How can I 'stop' a html5 video playback, and revert to poster image? Until this.play() is called, the poster image is showing properly.

My code:

<video loop="loop" onMouseOver="this.play();" onMouseOut="this.pause();" poster="/oceans-clip.thumb.jpg">
    <source src="/oceans-clip.mp4" type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"' />
    <source src="/oceans-clip.webm" type='video/webm; codecs="vp8, vorbis"' />
</video>

This is working as expected, however I want to revert to poster image onMouseOut instead of just pausing the video. What's a good way to do this?

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

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

发布评论

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

评论(6

赴月观长安 2024-11-12 15:04:15

规范排除了这种情况:

显示一帧视频后不应再次显示海报帧

即如果您想要与规范不同的行为,您需要自己实现它,也许可以使用覆盖包含所需图像的视频的元素,然后隐藏/显示它。

The spec rules this out:

the poster frame should not be shown again after a frame of video has been shown

I.e. if you want behaviour different to the spec you'll need to implement it yourself, maybe by using an element that overlays the video which contains the desired image and then hide/show it.

晨曦÷微暖 2024-11-12 15:04:15

我一直在谷歌搜索这个问题的解决方案,显然,

史蒂夫·莱西的解决方案:

当然。您可以执行相当于 'video.src = "";

似乎可以在我的 OSX 10.9 上运行,浏览器包括:Safari 7.0、Firefox 26.0 和 Chrome 31。不过我还没有在移动设备上测试过它。

我已经使用 JS 创建的视频对象对其进行了测试:

var object = document.createElement("video");
/// ... some setup like poster image, size, position etc. goes here...
/// now, add sources:
var sourceMP4 = document.createElement("source"); 
sourceMP4.type = "video/mp4";
sourceMP4.src = "path-to-video-file.mp4";
object.appendChild(sourceMP4);
//// same approach add ogg/ogv and webm sources

现在,当我想停止视频并再次显示海报时,我只需这样做:

object.pause();
object.src = "";

但是,这还不够,因为视频将无法再次播放。为了使其在此之后可以播放,我删除了“src”属性(同时保留“源”子对象不变):

object.removeAttribute("src");

此后它可以工作:

  1. 在停止视频时播放视频
  2. ,海报将重新出现
  3. 可以再次播放相同的视频

I've been googling for solution for this problem and apparently,

Steve Lacey's solution:

Sure. You could do the equivalent of 'video.src = "";

appears to work on my OSX 10.9, in browsers: Safari 7.0, Firefox 26.0 and Chrome 31. I haven't tested it on mobile devices though.

I've tested it using video object created with JS:

var object = document.createElement("video");
/// ... some setup like poster image, size, position etc. goes here...
/// now, add sources:
var sourceMP4 = document.createElement("source"); 
sourceMP4.type = "video/mp4";
sourceMP4.src = "path-to-video-file.mp4";
object.appendChild(sourceMP4);
//// same approach add ogg/ogv and webm sources

Now when I want to stop video and show poster again, I just do:

object.pause();
object.src = "";

But, that's not enough since video will not be able to play again. To make it playable after this point, I removed 'src' attribute (while leaving 'source' sub-objects as is):

object.removeAttribute("src");

After this it works:

  1. play video
  2. on stopping video, poster will re-appear
  3. can play same video again
仲春光 2024-11-12 15:04:15

这篇文章很旧,但我遇到了同样的问题并像这样解决了。

onmouseout="this.load();"

This post is old but I had the same problema and solve it like this.

onmouseout="this.load();"
×纯※雪 2024-11-12 15:04:15

我用过它,对我来说很好

$('container video').hover(function () {$(this).get(0).play();}
   ,function () {$(this).get(0).load();});});

i used it and it is good with me

$('container video').hover(function () {$(this).get(0).play();}
   ,function () {$(this).get(0).load();});});
朱染 2024-11-12 15:04:15

这对我有用:

<div class="rollover">
  <video class="thevideo" loop poster="" height="100" width="250">
    <source src="" type="video/mp4" >
          Your browser does not support the video tag.
  </video>
</div>

<script>
    var figure = $(".rollover").hover( hoverVideo, hideVideo );

    function hoverVideo(e) {  
        $('video', this).get(0).play(); 
    }

    function hideVideo(e) {
        $('video', this).get(0).pause(); 
        v=$('video', this).get(0).currentSrc;
        $('video', this).get(0).src = "";
        $('video', this).get(0).src = v;   
    }
</script>

This works for me:

<div class="rollover">
  <video class="thevideo" loop poster="" height="100" width="250">
    <source src="" type="video/mp4" >
          Your browser does not support the video tag.
  </video>
</div>

<script>
    var figure = $(".rollover").hover( hoverVideo, hideVideo );

    function hoverVideo(e) {  
        $('video', this).get(0).play(); 
    }

    function hideVideo(e) {
        $('video', this).get(0).pause(); 
        v=$('video', this).get(0).currentSrc;
        $('video', this).get(0).src = "";
        $('video', this).get(0).src = v;   
    }
</script>
音盲 2024-11-12 15:04:15

您可以使用:

    <script type="text/javascript">
    function videostop() {
      window.location.reload()
    }
    </script>

这不会真正停止您的视频,它会重新加载整个文档。
将显示“海报框架”。

里奇

PS我来自德国。
抱歉,如果我的英语不好。
但我认为,我的 JavaScript 没问题:-)

You can use:

    <script type="text/javascript">
    function videostop() {
      window.location.reload()
    }
    </script>

That will not really stop your video, it will reload the whole document.
The "Poster Frame" will be shown.

Richie

P.S. I'm from Germany.
Sorry if my English is bad.
But I think, my JavaScript is OK :-)

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