재생중인 video의 전체시간과 현재 재생중인 위치를 표시하도록 하겠습니다.
youtube
|views
|mixins
*|videoPlayer.pug
|assets
|js
*|videoPlayer.js
span.timeline
span#jsCurrentTime 00:00:00
span |
span#jsTotalTime 00:00:00
The media's metadata has finished loading; all attributes now contain as much useful information as they're going to.
이 event를 사용하지 않고 video의 duration을 불러오게 되면 nan만을 확인할 수 있습니다.
const videoContainer = document.querySelector("#jsVideoContainer");
const videoPlayer = document.querySelector("#jsVideoPlayer");
const currentTime = document.querySelector("#jsCurrentTime");
const totalTime = document.querySelector("#jsTotalTime");
function setTime(){
totalTime.innterHTML = formatDate(videoPlayer.duration);
setInterval(()=>{
currentTime.innerHTML = formatDate(videoPlayer.currentTime);
}, 1000);
}
function init(){
videoPlayer.addEventListener("loadedMetadata", setTime);
}
function formatDate(seconds){
const secondsNumber = parseInt(seconds, 10);
let hours = Math.floor(secondsNumber / 3600);
let minutes = Math.floor((secondsNumber - hours * 3600) / 60);
let totalSeconds = secondsNumber - hours * 3600 - minutes * 60;
if (hours < 10) {
hours = `0${hours}`;
}
if (minutes < 10) {
minutes = `0${minutes}`;
}
if (totalSeconds < 10) {
totalSeconds = `0${totalSeconds}`;
}
return `${hours}:${minutes}:${totalSeconds}`;
};
if(videoContainer){
init();
}