【问题标题】:play html5 video in reverse, on screen load在屏幕加载时反向播放 html5 视频
【发布时间】:2013-08-08 05:30:43
【问题描述】:

我正在开发一个功能,在我的屏幕加载时,视频应该开始反向播放。 (从最后一帧到第一帧)。 它实际上可以在屏幕加载时,甚至在按钮点击时。

此时,我已经实现了直到(在 StackOverflow 的帮助和支持下)播放我的视频 - 并在一段时间后反向播放。这是我使用的代码-

<!DOCTYPE HTML>
<html>
<head>
<style>
video{
    height: 200px;
}
</style>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
$(function() {

var video = document.getElementById('video');
var intervalRewind;
$(video).on('play',function(){
    //video.playbackRate = 1.0;
    clearInterval(intervalRewind);
});
$(video).on('pause',function(){
    video.playbackRate = 1.0;
    clearInterval(intervalRewind);
});
$("#speed").click(function() { // button function for 3x fast speed forward
    video.playbackRate = 8.0;
});
$("#negative").click(function() { // button function for rewind
   intervalRewind = setInterval(function(){
       video.playbackRate = 1.0;
       if(video.currentTime == 0){
           clearInterval(intervalRewind);
           video.pause();
       }
       else{
           video.currentTime += -.1;
       }
                },30);
});
});
</script>

</head>
<body>



<video id="video" controls>
    <source src="http://www.quirksmode.org/html5/videos/big_buck_bunny.mp4" type="video/mp4">
    <source src="http://www.quirksmode.org/html5/videos/big_buck_bunny.webm" type="video/webm">
    <source src="http://www.quirksmode.org/html5/videos/big_buck_bunny.ogv" type="video/ogg">
</video>
        <button id="speed">Fast Forward</button>
        <button id="negative">Rewind</button>
</body>
</html>

现在,我想知道是否有机会直接反向播放我的视频,即在不向前推进任何持续时间的情况下,视频应该开始反向播放。

【问题讨论】:

    标签: javascript jquery html html5-video


    【解决方案1】:

    你需要等到视频加载完毕,然后你可以通过video.duration获取它的长度,然后你可以将视频当前时间设置为这个值(跳转到视频结尾);

    然后你可以使用你的否定函数从最后一帧开始播放视频。

      video.addEventListener('loadeddata', function() {
         video.currentTime = video.duration;
         $('#negative').click();
      }, false);
    

    http://jsfiddle.net/UkMV2/5/ 上的视频播放也很不错,来自play a video in reverse using HTML5 video element

    以下示例是您的超级轻微修改版本,可在页面和视频加载事件时实现“反向”播放。

    <!DOCTYPE HTML>
    <html>
    <head>
    <style>
    video{
        height: 200px;
    }
    </style>
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    
    </head>
    <body>
    
      <video id="video" controls>
          <source src="http://www.quirksmode.org/html5/videos/big_buck_bunny.mp4" type="video/mp4">
          <source src="http://www.quirksmode.org/html5/videos/big_buck_bunny.webm" type="video/webm">
          <source src="http://www.quirksmode.org/html5/videos/big_buck_bunny.ogv" type="video/ogg">
      </video>
      <button id="speed">Fast Forward</button>
      <button id="negative">Rewind</button>
    
    
      <script>
      $(function() {
    
      var video = document.getElementById('video');
      var intervalRewind;
    
    
      $(video).on('play',function(){
          //video.playbackRate = 1.0;
          clearInterval(intervalRewind);
      });
      $(video).on('pause',function(){
          video.playbackRate = 1.0;
          clearInterval(intervalRewind);
      });
      $("#speed").click(function() { // button function for 3x fast speed forward
          video.playbackRate = 8.0;
      });
      $("#negative").click(function() { // button function for rewind
         intervalRewind = setInterval(function(){
             video.playbackRate = 1.0;
             if(video.currentTime == 0){
                 clearInterval(intervalRewind);
                 video.pause();
             }
             else{
                 video.currentTime += -.1;
             }
                      },30);
      });
    
      video.addEventListener('loadeddata', function() {
         video.currentTime = video.duration;
         $('#negative').click();
      }, false);
    
    
    
      });
      </script>
    
    </body>
    </html>
    

    【讨论】:

    • 刚刚注意到这是大约 4 年前被问到的,在最新的 jQuery 问题中仍然出现在我面前 :) 我猜回答没有坏处
    猜你喜欢
    • 1970-01-01
    • 2020-01-16
    • 1970-01-01
    • 2017-04-18
    • 1970-01-01
    • 2013-08-05
    • 1970-01-01
    • 2011-04-05
    • 2011-07-13
    相关资源
    最近更新 更多