Rewind HTML5 video after clicking without acceleration or lag

Asked

Viewed 71 times

0

What I need

I need a script (Javascript, jQuery, etc.) to rewind a video after clicking. I used a script I found on some forums, but it rewinds the video with acceleration, which makes the animation look bad/weird. It would need to be at the same speed.

What I got

//[Rewind]  

var video = document.getElementById('video');
var intervalRewind;
jQuery(video).on('play',function(){
        video.playbackRate = 1.0;
        clearInterval(intervalRewind);
});
jQuery(video).on('pause',function(){
        video.playbackRate = 1.0;
        clearInterval(intervalRewind);
});
jQuery("#btnVoltar").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);
});
  • Try changing the interval of setInterval to 100 instead of 30 and see if it works (in the last part of the code, after Else else{
 video.currentTime += -.1;
 }
 },100);

1 answer

0

So the acceleration happens because where you copied from, this seems to me to be the goal. If you want a Rewind as a "reverse play" you need to match the size of the range with the subtraction factor. And still watch other details like framerate and video uploading to deliver a better experience.

//[Rewind]  
$(document).ready(function() {

  var video = document.getElementById('video');
  var intervalRewind;
  $("#video").on('play', function() {
    video.playbackRate = 1.0;
    clearInterval(intervalRewind);
    //console.log('playing');
  });
  jQuery(video).on('pause', function() {
    video.playbackRate = 1.0;
    clearInterval(intervalRewind);
  });
  jQuery("#btnVoltar").click(function() { // button function for rewind
    clearInterval(intervalRewind);
    
    
    intervalRewind = setInterval(function() {
      
     // video.playbackRate = 1.0;
      if (video.currentTime <= 0) {
        video.pause();
        clearInterval(intervalRewind);
         
      } else {        
         video.currentTime -= .06;
         
      }
     // console.clear();
     //console.log(video.currentTime);
    }, 60);
    
  });
  
  video.pause();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<video id="video" controls width="50%">
	<source src="https://www.quirksmode.org/html5/videos/big_buck_bunny.mp4" type="video/mp4">
	<p>Your browser does not support H.264/MP4.</p>
</video>

<button id="btnVoltar">Voltar</button>

  • Got it! Thank you very much. I’m going to do some tests and try to align the FPS, the interval and the subtraction factor. Also, I have another question: if you’re going to use multiple videos on one page (somewhere between 7 or 8), you have some optimized way to control these distinct rewinds?

  • @Guilhermexavier, Another interesting thing would be to modify the video in Rewind, I did not understand your second doubt, you have about 7 to 8 different videos running simultaneously?

  • I was able to solve it! Sorry for the delay. The videos were not simultaneous. They were only present on the same page, but it was fired 1 at a time from one click (the same for Rewind). Thank you very much for the help!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.