0

I was wondering how I could control/edit the status bar of an html5 video controller (i think that's what its called.. Its the bar that has your current position in the video)?

What I'm trying to do is create a program that will enable a user to pick part of the video and loop it over and over again.

Once a user hits a button, there will be 2 slider buttons underneath the progress bar, (one for beginning and ending) and the user can select the beginning and ending times by sliding the sliders and having the program highlight the portion they selected.

What I am confused about is how the video element (progress bar) is effected by the java script, and how to make the selection portion of the bar. (the highlighted section)

any help would be awesome.

here are pictures of what I am trying to explain

https://i.sstatic.net/A3NmN.jpg

Thanks Guys

1
  • Is this guide helpful? Commented Jan 15, 2014 at 3:10

1 Answer 1

2

The progress bar of a <video> element isn't really "affected" by JS, per se... The progress bar is little more than a <div> with coloured <div>s inside (to show progress).

The video object (the JS object) has properties and methods which control playback/position, and fire update events to allow you to react.

<video id="my-video" src="/media/my-video.mp4"></video>

Then the JS properties of that object are pretty straightforward:

var video = document.querySelector("#my-video");
video.play();      // plays
video.pause();     // pauses
video.currentTime; // gets or sets the current playback time
video.duration;    // gets the length of the media file

// stop doesn't exist, but it's easy enough to make
video.stop = function () { video.pause(); video.currentTime = 0; };

video.addEventListener("timeupdate", function () {
    /* more of the video has played */
});

All you really need to do here, is spend some time thinking about how the bar itself will operate.
If 100% of the bar is equal to 100% of the video's duration, then as users click on other divs to move them around, you can figure out where those cursors are on the time-bar...

eg: your bar is 500px, your video is 300 seconds.
The user sets one cursor to 100px from the left of the bar (100px/500px == 0.2 * 300s == 60s).

So now you know that when you loop back around, you're going to set video.currentTime = 60;.
How do you know when to do that?

video.addEventListener("timeupdate", function () {
    var end_percent = end_cursor_pos_px / bar_width_px,
        start_percent = start_cursor_pos_px / bar_width_px,
        end_time = video.duration * end_percent;

    if (video.currentTime > end_time) {
        /* set video.currentTime to the "start_time" */
        video.currentTime = video.duration * start_percent;
    }
});

As a performance consideration, you should update the start-percent/end-percent whenever the user moves either cursor.
Then, when the video tells you it's played more, you should calculate the percentage of the duration.

Note that you might not have the full duration available to you until the video has played a bit (or until it's played all the way through, even). There is a "loadedmetadata" event you can listen to, that will tell you when you've got it all.
But still, if only 30 seconds have loaded, 50% of 30 seconds is still 50%.
If you want that not to be the way it works, then you need to work around it, either by waiting for that event (even if the video has to be 100% done), or by sending it in a JSON request, or reading it from headers in an AJAX "HEAD" request.

The math for the positions of the cursors and bar is pretty easy, too. As long as you guarantee that the cursors stay within the bounds of the bar, (and the user hasn't scrolled the window to the point where part of the bar is off-screen, left or right), it's just going to be:

var start_cursor = startEl.getBoundingClientRect(),
    end_cursor   = endEl.getBoundingClientRect(),
    scrub_bar    = scrubEl.getBoundingClientRect();

var start_percent = (start_cursor.left - scrub_bar.left) / scrub_bar.width,
    end_percent   = (end_cursor.left - scrub_bar.left)   / scrub_bar.width;

If they have scrolled left/right, then you just want to add window.pageXOffset to those values.

There isn't much more to it.
Just be sure to update the start/end percent any time a cursor gets moved, and check your playback percentage against the end-percent, and update the currentTime, any time it's gone past (the callbacks happen several times a second).

Hope that helps a little.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.