2011-06-17 131 views
27

我需要一種方式獲得視頻的總時間長度和當前時間與jquery和顯示它的幾個<div>標籤。html5視頻的當前/持續時間?

<div id="current">0:00</div> 
<div id="duration">0:00</div> 

我一直在搜索整天,我知道如何讓他們我只是不能顯示他們。 #jquerynoob

+0

您可以在''Video'標籤的timeupdate'事件得到'currentTime'和'duration'值。 – 2015-11-24 06:40:50

回答

14

此頁面可能會對您有所幫助。 Everything you need to know about HTML5 video and audio

var video = document.createElement('video'); 
var curtime = video.currentTime; 

如果您已經擁有了視頻元素,.currentTime應該工作。如果你需要更多的細節,該網頁應該能夠提供幫助。

11

工作示例在這裏:http://jsfiddle.net/tQ2CZ/1/

HTML

<div id="video_container"> 
<video poster="http://media.w3.org/2010/05/sintel/poster.png" preload="none" controls="" id="video" tabindex="0"> 
    <source type="video/mp4" src="http://media.w3.org/2010/05/sintel/trailer.mp4" id="mp4"></source> 
    <source type="video/webm" src="http://media.w3.org/2010/05/sintel/trailer.webm" id="webm"></source> 
    <source type="video/ogg" src="http://media.w3.org/2010/05/sintel/trailer.ogv" id="ogv"></source> 
    <p>Your user agent does not support the HTML5 Video element.</p> 
</video> 
</div> 

<div>Current Time : <span id="currentTime">0</span></div> 
<div>Total time : <span id="totalTime">0</span></div> 

JS

$(function(){ 
$('#currentTime').html($('#video_container').find('video').get(0).load()); 
$('#currentTime').html($('#video_container').find('video').get(0).play()); 
}) 
setInterval(function(){ 
$('#currentTime').html($('#video_container').find('video').get(0).currentTime); 
$('#totalTime').html($('#video_container').find('video').get(0).duration);  
},500) 
+0

當只有源視頻(無webm或ogg)時,此功能無效。所以我認爲這個持續時間只能存在於其他格式中。根據我對https:// stackoverflow的回答,我找到了 – lharby 2017-01-06 09:10:10

+0

。com/questions/41503253/custom-html5-video-controls-not-working-when-mp4-is-only-source/41514594#41514594,'duration'只可用於元數據加載後的視頻 – Offbeatmammal 2017-01-06 21:29:34

41

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> 

的JavaScript:

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

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

注:

每15至250毫秒,或當的MediaController的媒體控制器 位置的變化,無論發生最少的方式,用戶代理必須 排隊任務以觸發MediaController的 名爲timeupdate的簡單事件。

http://www.w3.org/TR/html5/embedded-content-0.html#media-controller-position

+1

是有可能沒有jQuery呢? – zok 2015-12-04 11:04:54

+1

@zok - jQuery只被用作這裏的選擇器。請改用document.getElementById('video-name')。 – Tom 2016-01-17 11:39:36

+0

如果只有mp4視頻,這似乎不起作用。 – lharby 2017-01-06 09:09:33

0

https://www.w3schools.com/tags/av_event_timeupdate.asp

// Get the <video> element with id="myVideo" 
var vid = document.getElementById("myVideo"); 

// Assign an ontimeupdate event to the <video> element, and execute a function if the current playback position has changed 
vid.ontimeupdate = function() {myFunction()}; 

function myFunction() { 
// Display the current position of the video in a <p> element with id="demo" 
    document.getElementById("demo").innerHTML = vid.currentTime; 
}