'Display/ Hide Markers on Video based on current time Javascript
I am trying to implement a function that displays/ hides markers (could be subtitles, highlighted area) based on "timeUpdate" event which tracks current time.
My content source has multiple tracks. Based on current time, one or more tracks containing marker objects may be shown or hidden.
Here's the starter code. I only trying to implement an efficient algorithm which can be scalable in case number of tracks increase. Need suggestions on improvements
player.on('timeUpdate', function(event) {
// currentTime (seconds, float) is exposed as event.data.currentTime
var currentTime = event.data.currentTime;
var time = convertTimeStamp(currentTime);
// your algorithm will be called here with the currentTime as the argument
handleAnnotations(time);
});
function handleAnnotations(currentTime) {
for (var i = 0; i < tracks.length; i++) {
for (var j = 0; j < tracks[i].length; j++) {
var currentAnnotationStartTime = convertTimeStamp(tracks[i][j].startTime);
var currentAnnotationEndTime = convertTimeStamp(tracks[i][j].endTime);
if (currentTime >= currentAnnotationStartTime && currentTime < currentAnnotationEndTime) {
tracks[i][j].show;
} else {
tracks[i][j].hide;
}
}
}
}
function convertTimeStamp(time) {
var hours = Math.floor(time / 60 / 60);
var minutes = Math.floor(time / 60);
var seconds = Math.floor(time - minutes * 60);
var hourValue = hours.toString().padStart(2, '0');
var minuteValue = minutes.toString().padStart(2, '0');
var secondValue = seconds.toString().padStart(2, '0');
var mediaTime = `${hourValue}:${minuteValue}:${secondValue}`;
return mediaTime;
}
Tracks are setup like this:
tracks = [
[annotation1, annotation2, annotationN...],
[annotation1, annotation2, annotationN...], ...
]
Where each annotation
looks like:
{
"startTime": timestamp,
"endTime": timestamp,
"show": fn,
"hide": fn
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|