试图获得完整的视频时长但返回为Nan

Php*_*ude 4 javascript jquery video.js

我试图获得播放器的视频持续时间,但它返回NaN,我不完全确定如何从这里访问它.

<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>
Run Code Online (Sandbox Code Playgroud)

日志告诉我: NaN

Ray*_*yon 9

等待onloadedmetadata活动!

var vid = document.getElementById("videoPlayerNew");
vid.onloadedmetadata = function() {
  console.log('metadata loaded!');
  console.log(vid.duration);
};
Run Code Online (Sandbox Code Playgroud)
<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>

<script>
</script>
Run Code Online (Sandbox Code Playgroud)