如何在视频上获得精确的timeupdate以返回最多2位十进制数(毫秒)? [英] How to get a precise timeupdate on a video to return upto 2 decimal numbers (milliseconds)?

查看:170
本文介绍了如何在视频上获得精确的timeupdate以返回最多2位十进制数(毫秒)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个视频元素,我想将它的当前时间与最多2个十进制值的变量进行比较。

I have a video element where I want to compare it's current time to a variable with upto 2 decimal values.

$(video1).on('timeupdate', function(){
    var currentTime = Math.round(this.currentTime);
    var durationNum = Math.round(this.duration);
    var formattedCurrentTime = secondsToHms(currentTime);
    var formattedDurationTime = secondsToHms(durationNum)
    onTrackedVideoFram(formattedCurrentTime, formattedDurationTime)

    if(currentTime >10){ $(".box1, .box2").hide(); }

    if(currentTime == choiceHeart){
        console.log("HERE");
        video1[0].pause();

    }

这是来自教程文件所以唯一方式我知道如何让timeupdate返回一个值是通过math.round函数返回舍入的整数。

this is from a tutorial file so the only way I know how to get the timeupdate to return a value is through that math.round function which returns rounded whole number.

但是,我想触发一个事件,比如暂停视频,比如2.5分钟 (或2分15帧 - After Effects 30 fps标准)****

BUT, I want to trigger an event, like pause the video at, say, 2.5 minutes (or 2 minutes 15 frames - by After effects 30 fps standards)****

我能做的就是在早期创建一个变量这样的文件:

The way I could do is create a variable early in the document like this :

var triggerEvent = 2.5;

所以我怎么理解2.5意味着在2分半钟内返回这个值它应该将视频暂停在2&半分钟?

So how do I get to understand that 2.5 means to return this value in 2 minutes and half minutes and it should pause the video at 2 & a half minutes?

PS:

为了暂停视频我计划使用if循环:

In order to pause the video I plan to use an if loop :

if(currentTime == triggerEvent){
    video1[0].pause();
}

这里video1引用一个变量,该变量指向保存视频的div 。

here video1 is referencing to a variable which points to the div which holds the video.

推荐答案

您正在舍入比较中使用的变量...

舍入得到更接近的整数:

You are rounding the variable used in the comparison...
Rounding gets the closer integer:

0.4给0

0.6给1

0.4 gives 0
0.6 gives 1

获取浮点数两位小数,你可以这样做:

To get a floating number with two decimals, you can do this:

var currentTime = parseFloat(this.currentTime);
currentTime = parseFloat(currentTime.toFixed(2));

var currentTime = 2.8349354;
currentTime = parseFloat(currentTime.toFixed(2));
console.log(currentTime);

所以你可以使用这个:

var currentTime = parseFloat(this.currentTime.toFixed(2));

如果你是 durationNum 同样的事情还需要它是一个2位小数的浮点数。

And the same thing for durationNum if you also need it to be a float with 2 decimals.


编辑

你也可以尝试这个...它应该适用于所有的浏览器。

You could also try this... It should work on all browsers.

var currentTime = Math.round(this.currentTime*100)/100;

这篇关于如何在视频上获得精确的timeupdate以返回最多2位十进制数(毫秒)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆