在屏幕加载时反向播放html5视频

时间:2013-08-08 05:30:43

标签: javascript jquery html5 html5-video

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

此时此刻,我已经实现了(在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>

现在,我想知道是否有机会反向播放我的视频,即在前进方向没有任何持续时间,视频应该开始反向播放。

1 个答案:

答案 0 :(得分:2)

您需要等到视频加载完毕,然后您可以使用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>
相关问题