如何在HTML页面中显示Javascript/Jquery值?VideoJs 显示视频的持久性

How to display Javascript/Jquery value in HTML page? VideoJs Display durration of the video

本文关键字:显示 Jquery VideoJs 持久性 视频 Javascript HTML      更新时间:2023-09-26

嗨,我目前正在研究HTML5 Jquery视频播放器。在这里,您可以获得有关此HTML视频播放器的更多信息:http://www.videojs.com/docs/api/

从此链接中,您可以看到myPlayer.duration()是必须在几秒钟内显示视频耐久性的功能。就是这样,我只想在简单的 HTML 页面中显示此值,就像我正在尝试使用我的代码一样。

这是我的代码:

 <head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
</head>
  <video id="vemvo-player" class="video-js vjs-default-skin" controls autoplay="true" width="950" height="534"
      data-setup="{}">
    <source src="[var.base_url]/uploads/[var.video_play]" type='video/flv' />
  </video>
<div id="duration"></div>
<script type="text/javascript">
        var myPlayer = _V_("vemvo-player");
    _V_("vemvo-player").ready(function(){
        var howLongIsThis = myPlayer.duration();
        $('#duration').html('Duration: ' + howLongIsThis);
    });
</script>

此代码的问题在于,当视频持续时间远低于 0 时,它会显示Duration: 0

我的视频播放器工作正常,并且正确显示视频的持续时间。我的问题是如何在 HTML 页面值中显示这种持久性?

当我var myPlayer = _V_("vemvo-player");发布到_V_("vemvo-player").ready(function(){函数中并尝试在控制台中运行myPlayer.duration()使用上面的代码时,它会给我 Undefined 变量的错误,但是当var myPlayer = _V_("vemvo-player");像在我的帖子中一样在函数之外并且我在控制台中键入myPlayer.duration()时,它会在几秒钟内为我提供durration,就像我需要它一样。

问题是我可以在 HTML 页面中将此变量显示为数字。我只想在 HTML 页面中显示这个数字。

我的代码中的错误在哪里以及如何修复它?

提前感谢!

<script type="text/javascript">
var myPlayer = _V_("vemvo-player");
_V_("vemvo-player").ready(function(){
   // Any ready/init code you want here
   $('#duration').html('Duration: ');        
});
myPlayer.addEventListener('loadedmetadata', function() {
    $('#duration').html('Duration: ' + myPlayer.duration);
});
</script>

您是否尝试过将变量声明保留在函数外部,但将赋值保留在函数内部?

<script type="text/javascript">
    var myPlayer;
    _V_("vemvo-player").ready(function(){
        myPlayer = _V_("vemvo-player");
        var howLongIsThis = myPlayer.duration();
        $('#duration').html('Duration: ' + howLongIsThis);
    });
</script>