Guide to retrieve Player duration, current timestamp, and video frames or Seek Position using JQuery

I have successfully managed to put together this code that retrieves player duration, current time, and calculates a value used to update the progress bar width.

For Example

var slideOff = Math.floor((100 / totalTime) * currentTime); 

progBar.css({width:slideOff+'%'});

However, I am facing an issue where I also need to obtain the seek position.

Although the following code works fine, the seek figure does not match the current time.

If you run this code on JSFiddle, you will notice discrepancies like:

Duration 29:05

Current Time 2:25

SeekPosition 1:44 The problem lies in seeking position not aligning with the current time when calculated.

Html

<div id="seekbar">
<div id="seeker"></div>
<div id="seekLay" ></div>
</div>
Duration<span id="currenT">00:00</span>
<br/>
Current Time<span id="durationT">00:00</span>
<br/>
Seek Position <span id="resulk">00:00</span>

Css

#seekbar{
    border:none;
    width:480px;
    height:6px; 
    background-color:#00ffcc;   
    position:relative;
    margin:auto;
}

#seekLay{
    margin:0;
    height:4px;
    background-color:#001001;
    position:relative;
    top:1px;
}

#seeker{
    width:6px;
    height:100%;
    position:absolute;  
    left:0;    
    background-color:red;
    cursor:pointer;
}
span{margin-left:30px;}

Javascript

String.prototype.pad = function(l, s){
return (l -= this.length) > 0 ? (s = new Array(Math.ceil(l / s.length) + 1).join(s)).substr(0, s.length) + this + s.substr(0, l - s.  length) : this;}

function playerTime(currentTime) {
       var tCurrent = currentTime,
           totalTime = 1745,
           dbar=$("#seekbar"),
           progressBar=$("#seeker"),
           progBar=$("#seekLay"),
           currenT = tCurrent,
           slideOff=Math.floor((100 / totalTime) * tCurrent);

$('span#currenT').html(Math.floor(totalTime / 60) + ":" + (totalTime % 60).toFixed().pad(2, "0"));
$('span#durationT').html(Math.floor(tCurrent / 60) + ":" + (tCurrent % 60).toFixed().pad(2, "0"));     
progressBar.css({marginLeft:slideOff+'%'});
progBar.css({width:slideOff+'%'});

var ratio=Math.floor((progBar.width())/progressBar.width()),           
    xpos=Math.floor(totalTime * (ratio / 100));

$('span#resulk').html(Math.floor(xpos / 60) + ":" + (xpos % 60).toFixed().pad(2, "0"));

}
playerTime(145);//change

//Output
//Duration  29:05 
//Current Time   2:25 
//Seek Position 1:44

This should not take too long for you to figure out.

Answer №1

Make a simple adjustment here

var ratio=Math.floor((progBar.width())/progressBar.width()),           
        xpos=Math.floor( tDuration* (ratio / 100));

============

Substitute the previous line with this new one

var xpos=Math.floor(tDuration*progBar.width()/dbar.width());

==============

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using Angular service worker to pre-fetch video files

Issue arises when the service worker prefetches the entire video embedded on the page, leading to performance problems. My ngsw-config.json only contains configurations for local files, whereas the video is located on a different subdomain under /sites/def ...

In AngularJS, the visibility of controller data seems to be elusive to me

Here is a snippet of code from my template: <ul> <li ng-repeat="x in names"> {{ x.name + ', ' + x.country }} </li> </ul> And here is the corresponding JavaScript code: app.controller('TestCtrl', ['$sc ...

Execute JavaScript function on click event in NextJS

Is it possible to execute a JavaScript function on the client side without using addEventListener? This situation works with addEventListener. MyComponent.js import Script from 'next/script' export default function MyComponent({ props }) { ...

Implementing Window.Open function within a jQuery Modal

I've set up my Modal Div like this: <div id="dialog-modal" title="Open File"> <img alt="progress" src="images/ajax-loader.gif"/> </div> When I click the button, the modal appears and I can see the progress icon. <sc ...

Transforming CSS with React Router Links

Why is my CSS being significantly altered by the React Router Link? Is there a way to prevent the Link from affecting the styling? Without Link: https://i.sstatic.net/ZoVEq.jpg With Link: https://i.sstatic.net/84qRS.jpg This is the code for the ...

Should using module.export = [] be avoided?

Having two modules that both need access to a shared array can be tricky. One way to handle this is by creating a separate module just for the shared array, like so: sharedArray.js module.exports = []; In your module files, you can then use it like this ...

Elements animated using intersection observer are inconsistently functioning on the current page

My current challenge involves animating elements with the intersection observer, but I am encountering strange behavior with elements that are already on the screen when the page loads. These elements sometimes animate correctly and other times they do no ...

Not all databases are retrieved in the search query

When I make an API call to get all the Database entries, I am encountering an issue. The response I receive only includes a few databases instead of all of them. async function checkDatabases(item){ if(item.object == 'database') ...

Interactive Code Playground for JS/JQuery, HTML, and CSS

I am in the process of developing my unique code for a real-time html/js/jquery/css editor. I have successfully integrated css and html, but I am facing challenges with getting jquery or javascript to function properly. Below is an excerpt of the code caus ...

Combining two objects by id and grouping identical key-value pairs together

var routePlan = [ { "id" : 1, "farmerName" : "Farmer1", "farmerId" : 1 }, { "id" : 2, "farmerName" : "Farmer2", "farmerId" : 2 }, { "id" : 1, "farmerName" : "Farm ...

Multiple executions of Google API sign in listener

I have been working on a Vue module that allows users to add events to Google Calendar easily. Once a user signs in, there is a listener in place to call a method for adding an event to the calendar. To access the gapi functionalities required for this tas ...

Check to see if the cursor is positioned on the newly created raphael element without requiring a new mouse

After creating a new element using Raphael.js, I have noticed that hover events do not trigger on Chrome (42.0.2311.90) or Internet Explorer (11.0.8) when the element is under an idle cursor. However, Firefox (31.0, 32.0.2, and 37.0.2) works as intended. ...

utilize angular4 to dynamically link images from twitter based on certain conditions

Currently, I am attempting to retrieve the URL of images from tweets only if the tweet contains an image. I have been successful in fetching tweets and some images, but the issue arises when a tweet does not have an image, causing it to break and stop disp ...

Unable to store multiple users in MongoDB

Currently, I am encountering an issue with passportJS not saving more than one user to MongoDB while using nodeJS with the expressJS server framework. Initially, I successfully set up email registration with passport-local. However, when I added passport- ...

Identifying various device platforms through CSS styling

After extensive research and testing, I have explored a variety of resources and solutions for implementing device-specific CSS on my webpage. Here are some of the references I've studied: Detect iPhone/iPad purely by css Detect Xoom browser (Andro ...

Exploring the concept of height definition within flexbox and incorporating responsive images on Chrome and Safari browsers

A unique issue arises when comparing the behavior of a simple code snippet in Chrome and Safari browsers. Check out the HTML code below: <div class="row"> <div class="col-md-6 d-flex"> <img class="main-img" src="http://placehol ...

The parameter 'Value | ValueXY' cannot be assigned to the type 'AnimatedValue<0>'

Recently, I delved into React Native documentation on Animations which can be found here: https://reactnative.dev/docs/animated After reading the docs, I decided to try out some code snippets: import {Animated as RNAnimated} from 'react-native' ...

Transferring PHP array data to JavaScript using echo

I'm currently in the process of working on a project that requires extracting data from a database and converting it into a JavaScript array. This array will be used to dynamically update a graph. Below is the PHP code I have written to retrieve the ...

Directus | The Revision is not being updated when a collection is created in Flows

I am currently working with the latest version 9 of Directus. I have set up a Data Model called "Article" with fields for title and Random_order. https://i.sstatic.net/3JcZ4.png https://i.sstatic.net/bRpBF.png I have created a Directus flow that upda ...

How do I go about showing every character on a separate line using a for loop?

var input = prompt("What is Lance attempting to convey?"); //user enters any text for (var i = 0; i <= input.length; i++) { var output = input.charAt(i); if (output == "e" || output == "o" || output == "a" || output == "u") { outp ...