How to disable seeking with HTML5 video tag ?

29,855

Solution 1

The question is quite old but still relevant so here is my solution:

var video = document.getElementById('video');
var supposedCurrentTime = 0;
video.addEventListener('timeupdate', function() {
  if (!video.seeking) {
        supposedCurrentTime = video.currentTime;
  }
});
// prevent user from seeking
video.addEventListener('seeking', function() {
  // guard agains infinite recursion:
  // user seeks, seeking is fired, currentTime is modified, seeking is fired, current time is modified, ....
  var delta = video.currentTime - supposedCurrentTime;
  if (Math.abs(delta) > 0.01) {
    console.log("Seeking is disabled");
    video.currentTime = supposedCurrentTime;
  }
});
// delete the following event handler if rewind is not required
video.addEventListener('ended', function() {
  // reset state in order to allow for rewind
    supposedCurrentTime = 0;
});

JsFiddle

It is player agnostic, works even when the default controls are shown and cannot be circumvented even by typing code in the console.

Solution 2

Extending the answer from @svetlin-mladenov, you can do the following to prevent the user from seeking any part of the video which has not been watched yet. This will also allow the user to rewind and the seek out any part of the video which had already watched previously.

var timeTracking = {
                    watchedTime: 0,
                    currentTime: 0
                };
                var lastUpdated = 'currentTime';

                video.addEventListener('timeupdate', function () {
                    if (!video.seeking) {
                        if (video.currentTime > timeTracking.watchedTime) {
                            timeTracking.watchedTime = video.currentTime;
                            lastUpdated = 'watchedTime';
                        }
                        //tracking time updated  after user rewinds
                        else {
                            timeTracking.currentTime = video.currentTime;
                            lastUpdated = 'currentTime';
                        }
                    }


                });
                // prevent user from seeking
                video.addEventListener('seeking', function () {
                    // guard against infinite recursion:
                    // user seeks, seeking is fired, currentTime is modified, seeking is fired, current time is modified, ....
                    var delta = video.currentTime - timeTracking.watchedTime;
                    if (delta > 0) {
                        video.pause();
                        //play back from where the user started seeking after rewind or without rewind
                        video.currentTime = timeTracking[lastUpdated];
                        video.play();
                    }
                });
Share:
29,855
ajan
Author by

ajan

Updated on September 29, 2021

Comments

  • ajan
    ajan over 2 years

    I know this is not advisable. But still need this feature to be implemented. Tried everything from onseeking,onseeked to media controller. Nothing worked. Are there any external libraries to disable seeking. would be helpful if some pointers on how to go about using custom controls.

    • ajan
      ajan over 11 years
      The seek bar should still be visible but with seeking disabled.
  • ajan
    ajan over 11 years
    Are you sure i can disable seeking with the seek bar still there?
  • ajan
    ajan over 11 years
    Are you sure i can disable seeking with the seek bar still there using video.js? Can videos.js be used to display videos with sencha touch for mobile devices???? FYI the custom controls are not visible in the link you mentioned but the code contains almost everything i need.
  • Stephane Rolland
    Stephane Rolland about 9 years
    Welcome to SO! Please, consider adding some text as explanation, rather than code only answers. It's often easier to understand.
  • Rafael Moni
    Rafael Moni almost 8 years
    any reason I should care to use '0.01' in the comparison 'Math.abs(delta)' instead of just 0?
  • Svetlin Mladenov
    Svetlin Mladenov almost 8 years
    @RafaelMoni Usually it's a good idea to compare floating point number to a certain delta and not for absolute equality because of the way they are implemented in hardware. In this case there is additional incentive to do so because currentTime is not a continuous value. It takes only values equal to the start times of the video frames. This could lead to cases where currentTime jumps back/forward unexpectedly depending on the video stream, decoder and browser. 0.01 was chosen at random as a sufficiently small number.
  • Prateek Agarwal
    Prateek Agarwal about 6 years
    @SvetlinMladenov The seeking happens in Microsoft Edge 40.15063.674.0 | Microsoft EdgeHTML 15.15063, if we try continuously. Any fix for it?
  • Svetlin Mladenov
    Svetlin Mladenov about 6 years
    @PrateekAgarwal what do you mean "if we try continuously"?
  • Prateek Agarwal
    Prateek Agarwal about 6 years
    I’ve attempted 2 times and it did seek. The video.currentTime is getting set to seeked time not sure how?