gpt4 book ai didi

javascript - 试图获得完整的视频时长但以 Nan 身份返回

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:07:07 27 4
gpt4 key购买 nike

我正在尝试获取我的播放器的视频持续时间,但它返回 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>

日志告诉我:NaN

最佳答案

等待onloadedmetadata事件,loadedmetadata 事件在加载元数据 时触发。

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>

关于javascript - 试图获得完整的视频时长但以 Nan 身份返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40763057/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com