【问题标题】:How to get current timestamp (not just in seconds) in HTML video如何在 HTML 视频中获取当前时间戳(不仅仅是秒)
【发布时间】:2016-07-28 21:18:28
【问题描述】:

所以我试图从 HTML5 视频中获取当前时间戳,例如 HH:MM:SS。我正在使用 this:
HTML

的变体
<video
    id="video-active"
    class="video-active"
    width="640"
    height="390"
    controls="controls">
    <source src="myvideo.mp4" type="video/mp4">
</video>
<div id="current">0:00</div>
<div id="duration">0:00</div>

JQUERY

$(document).ready(function(){
    $("#video-active").on(
    "timeupdate", 
    function(event){
    onTrackedVideoFrame(this.currentTime, this.duration);
});

function onTrackedVideoFrame(currentTime, duration){
    $("#current").text(currentTime);
    $("#duration").text(duration);
}

我的主要问题是当前和持续时间 div 只显示 0:00 而没有别的。代码有问题吗,还是有更简单的方法来获取 HTML 视频的时间戳?

【问题讨论】:

  • 你的意思是显示0:00不增加?或者您只想更改该时间戳的格式?
  • 您确定这不仅仅是语法错误吗?你的 jquery sn-p 有不匹配的大括号/括号。

标签: jquery html


【解决方案1】:

首先,为了解决 0:00 的问题,我会将 TEXT 更改为 HTML

function onTrackedVideoFrame(currentTime, duration){
    $("#current").html(currentTime);
    $("#duration").html(duration);
}

然后包括 MOMENT

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.12.0/moment.min.js"></script>

将秒转换为毫秒并创建一个 MOMENT 持续时间

    val = Number(duration) * 1000; //convert seconds to milliseconds
    var d = moment.duration(val);
var show_time = d.hours() + "hours" + d.minutes() + "minutes" + d.seconds() + "seconds";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-20
    • 2021-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-16
    • 1970-01-01
    • 2018-01-08
    相关资源
    最近更新 更多