Я хотел бы показать продолжительность моего видео и фактическое время.Media video player HTML5 JS
Для этого я использую API HTMLMediaElement в JS.
Все работает (воспроизведение, пауза, перезагрузка, ...), но продолжительность и текущее время не работают, и я не знаю почему. Я не обычно код в JS поэтому, пожалуйста, хорошо ^^
Это мой код:
<div class="wrap_video">
<video id="video" width="298" height="240">
<source src="videos/windowsill.mp4" type="video/mp4">
Désolé, votre navigateur ne vous permet pas de visualiser cette vidéo.
</video>
</div>
<div class="datas">
<span id="currentTime"><script>document.write(currentTime);</script></span>
<span id="duration"><script>document.write(duration);</script></span>
</div>
<div id="buttonbar">
<button id="restart" class="btn btn-default" onclick="restart();">
<span class="glyphicon glyphicon-repeat"></span>
</button>
<button id="rewind" class="btn btn-default" onclick="skip(-10)">
<span class="glyphicon glyphicon-fast-backward"></span><!--<<-->
</button>
<button id="play" class="btn btn-default" onclick="playPause()">
<span class="glyphicon glyphicon-play"></span><!-->-->
</button>
<button id="fastForward" class="btn btn-default" onclick="skip(10)">
<span class="glyphicon glyphicon-fast-forward"></span>
</button>
</div>
</div>
И мои JS:
/**
* Play or Pause the video
*/
function playPause() {
var video = document.getElementById("video");
var button = document.getElementById("play");
var currentTime = 0;
currentTime += document.getElementById("video").currentTime;
var duration = document.getElementById("video").duration;
if (video.paused) {
video.play();
button.innerHTML = "<span class=\"glyphicon glyphicon-pause\"></span>";
} else {
video.pause();
button.innerHTML = "<span class=\"glyphicon glyphicon-play\"></span>";
}
}
/**
* Restart the video
*/
function restart() {
var video = document.getElementById("video");
video.currentTime = 0;
}
/**
* Skip the video to the given value
*
* @param int value The value
*/
function skip(value) {
var video = document.getElementById("video");
video.currentTime += value;
}
На самом деле я просто хочу, чтобы показать текущее время и продолжительность
Edit: Ok, так что теперь у меня есть что: Для текущего времени
setInterval(function() {
document.getElementById("currentTime").innerHTML = document.getElementById("video").currentTime;
}, 1000);
var duration = document.getElementById("video").duration;
document.getElementById("duration").innerHTML = duration;
Но теперь моя проблема заключается в том, что у меня есть [ObjectHTMLSpanElement] вместо 0:00 и NaN вместо 20:03 ...
кажется ваш 'вар currentTime' просто обновляется играть или приостанавливать, я не знаю, было ли это ваше намерение. –
У вас есть это: http://stackoverflow.com/questions/6380956/current-duration-time-of-html5-video? – caramba
'document.write' будет выполнен только один раз, когда страница будет отображаться, это не динамическое связывание. – pawel