2

I am working on an HTML5 video player with jQuery. For now I have my video player working very well but I want to get the variable for the video duration and show/display it in pure HTML page.

So from here you can take more info about this Jquery video player: http://www.videojs.com/docs/api/

I think the variable for video duration is: myPlayer.duration();

How I can display this value in HTML?

Here is my HTML code to display the player:

  <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>

This is what I have tried to display this variable but it says that it is = "0" when on the video player it says that it's 4min:

  <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">
    _V_("vemvo-player").ready(function(){
        var myPlayer = this;
        var howLongIsThis = myPlayer.duration();
        $('#duration').html('Duration: ' + howLongIsThis);
    });
</script>

Where is my mistake?

Daniel Beck
  • 18,929
  • 5
  • 34
  • 51
user2979725
  • 41
  • 2
  • 2
  • 6
  • Can you create a demo of your problem online, maybe on [jsFiddle](http://jsfiddle.net), so we can see it? – andyb Nov 11 '13 at 17:06
  • possible duplicate of [How to display Javascript/Jquery value in HTML page? VideoJs Display durration of the video](http://stackoverflow.com/questions/19915141/how-to-display-javascript-jquery-value-in-html-page-videojs-display-durration-o) – P_Enrique Nov 11 '13 at 21:56
  • **Exact** duplicate of [How to print one specific Javascript variable in HTML?](http://stackoverflow.com/questions/19910095/how-to-print-one-specific-javascript-variable-in-html). Please avoid asking the same question multiple times. Also see http://stackoverflow.com/questions/19915141/how-to-display-javascript-jquery-value-in-html-page-videojs-display-durration-o – andyb Nov 12 '13 at 08:55

4 Answers4

3

You can try this. It works for me.

var myPlayer = videojs('vemvo-player');

    if (myPlayer.readyState() < 1) {
        // wait for loadedmetdata event
        myPlayer.one("loadedmetadata", onLoadedMetadata);
    }
    else {
        // metadata already loaded
        onLoadedMetadata();
    }
    
    function onLoadedMetadata() {
        alert(myPlayer.duration());
        $('#duration').html("Duration: " + myPlayer.duration());
    }
2
var player = videojs('my-video', {
    fluid:false, // videojs settings
    controls:true,
    height: '300'          
  });
$(document).ready(function(){
  console.log(player.duration()); 
 // will return video duration. Can't be used while page is loading.

  console.log(player.currentTime()); 
 // will return current time if video paused at some time. Intially it would be 0.

});
Kaushik Thakkar
  • 327
  • 3
  • 10
0

Those who might come here and are using videojs with Vue 3, also might work with similar frameworks like React.

Referring to this code and component usage - https://docs.videojs.com/tutorial-vue.html you can do the following changes to get the duration, height, and width.

// New Code here
this.player = videojs(
  this.$refs.videoPlayer,
  this.options,
  function onPlayerReady() {
    console.log("onPlayerReady", this);
  }
);
this.player.one("loadedmetadata", () => {
  var duration = this.player.duration();
  console.log(`Duration of Video ${duration}`);
  console.log(`Original Width of Video ${this.player.videoWidth()}`);
});
damitj07
  • 2,421
  • 1
  • 18
  • 36
-1

As you're using jQuery, that can be done much easier :) You can use $.html to edit the html contents of a dom element. Your code will look something like this:

<div id="duration"></div>

<script type="text/javascript">
    _V_("vemvo-player").ready(function(){
        var myPlayer = this;
        var howLongIsThis = myPlayer.duration();
        $('#duration').html('Duration: ' + howLongIsThis);
    });
</script>
giorgio
  • 9,885
  • 2
  • 27
  • 41