Adding Markers to the Progress Indicator in an HTML5 Video Player (Edited)
I have an HTML5 video player with a custom progress indicator. I want to add markers on the timeline so that they appear at specific moments in the video.
I wrote some code, but with it, the markers only appear in one place, and they don’t always display correctly. How can I position the markers at the desired locations on the timeline?
Here’s what I have so far in the HTML:
<div id="media-wrapper" canplay> <div id="media-area"> <video id="mediaPlayer" ref="media" autoplay playsinline webkit-playsinline> <source src="assets/videos/sampleVideo.mp4" type="video/mp4"> </video> </div> <div id="media-controls" style="position: relative; display: block;"> <div id="progress-wrapper"> <div id="progress-bar"> <div id="played-progress"> </div> </div> </div> <div id="toggle-play"> <img id="play-btn" src="assets/icons/play.svg"> <img id="pause-btn" src="assets/icons/pause.svg" style="display:none"> </div> </div> </div>
And in JavaScript:
$(document).ready(function(){ // Get the video element var videoElement = document.getElementById("mediaPlayer"); var timeRanges = [ { start: 0, end: 5 }, { start: 5, end: 10 }, { start: 10, end: 25 }, { start: 25, end: 40 }, ]; console.log(timeRanges[0].end); videoElement.addEventListener("timeupdate", function () { if ($(this)[0].duration) { $(this).parent().parent().find("#played-progress").css("width", ($(this)[0].currentTime * 100 / $(this)[0].duration) + "%"); } if (this.currentTime >= timeRanges[0].end) { var bubble = $('<div class="bubbles"></div>'); $("#played-progress").append(bubble); } }); });
Answers
Stéphane Claes
7 months ago
1 comment
Rating
Your method of updating the width of the #played-progress element can be expanded to add multiple markers positioned based on the time relative to the video's total duration.
Here's how I see it:
1. After loading the video metadata (loadedmetadata), calculate the position of each marker as a percentage of the video's total duration. This ensures correct placement of markers regardless of the video's length.
2. For each range, create a div with the class marker-point, set its CSS left property to the calculated percentage, and append it to the #progress-bar.
3. Use the .marker-point class to position the markers absolutely within the progress bar and style them to be visible.
Here's the code implementing this:
JavaScript:
CSS:
Oliver Williams
7 months ago
Rating
Thank you, I used your ideas in my work, and my markers are now working.
Lukas Müller
7 months ago
1 comment
Rating
If you don't want to reinvent the wheel, you can use libraries for implementing a video player like Video.js. These libraries have plugins that provide the functionality to add markers.
In short, you would need to:
Initialize the Video.js player.
Define an array of marker objects with time and label fields.
After loading the video metadata, iterate through the marker array, calculate their positions, and then add them to the progress bar element.
Use the .marker-dot class to style the markers and add tooltips with the marker labels on hover.
Here's how you can implement markers using Video.js:
JavaScript:
CSS:
Lukas Müller
7 months ago
Rating
I would like to create my own custom video player without using third-party libraries. But in any case, thank you for taking the time to help me with my question.