I am trying to get the video duration of my player but it returns NaN
, I am not entirely sure how to access it from here.
<video id="videoPlayerNew" class="video-js vjs-default-skin vjs-controls-enabled" poster="http://camendesign.com/code/video_for_everybody/poster.jpg" data-setup="{}" controls="">
<source src="sample.mp4" type="video/mp4">
<p class="vjs-no-js">Javascript was disabled or not supported</p>
</video>
<script>
var vid = document.getElementById("videoPlayerNew");
console.log(vid.duration);
</script>
The log tells me: NaN
Wait for onloadedmetadata
event, The loadedmetadata
event is fired when the metadata has been loaded.
var myVideo = document.getElementById("videoPlayerNew");
myVideo.onloadedmetadata = function() {
console.log('metadata loaded!');
console.log(this.duration);//this refers to myVideo
};
<video id="videoPlayerNew" class="video-js vjs-default-skin vjs-controls-enabled" poster="http://camendesign.com/code/video_for_everybody/poster.jpg" data-setup="{}" controls="">
<source src="http://www.w3schools.com/html/mov_bbb.mp4" type="video/mp4">
<p class="vjs-no-js">Javascript was disabled or not supported</p>
</video>
Try this code below, it adds the event after everything is loaded :
$(document).ready(function(){
$("#videoPlayerNew").on( "timeupdate", function(event){
console.log( this.duration);
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With