Jothi Kannan
Jothi Kannan

Reputation: 3358

HTML 5 video player update currentTime with given number

i want to update the currentTime with given number by user

For example i have video with 18.20 Mins of video, when user gives 1.46 as input it should play the video from 1.46 Mins, if user gives 4.56Mins it should play from 4.56Mins

What am trying is converting the given Minutes into number, but i think it is wrong

function minsToNumber(d) {
        d = Number(d);
        var h = Math.floor(d / 3600);
        var m = Math.floor(d % 3600 / 60);
        var s = Math.floor(d % 3600 % 60);
        return ((h > 0 ? h + "." + (m < 10 ? "0" : "") : "") + m + "." + (s < 10 ? "0" : "") + s);
    }

any one can give correct calculation to do this ?

Upvotes: 2

Views: 984

Answers (1)

Ali Mamedov
Ali Mamedov

Reputation: 5256

The HTMLMediaElement.currentTime property gives the current playback time in seconds. Setting this value seeks the media to the new time.

source

So if you need 4 minutes 55 seconds, you have to use this value: 4*60 + 55 = 295

var video = document.getElementById('video');
video.currentTime = 295; // seconds

Complete sample: (created in partnership with @Kaiido)

var toSeconds = function(time) {

  time = ('' + time).split('.');

  return +time[0] * 60 + +time[1];
};

toSeconds('4.55'); // 295
toSeconds(4.55); // 295

Upvotes: 2

Related Questions