Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jwplayer - how do I get video duration before playback?

Tags:

jwplayer

I'm trying to get the duration of a video before the jwplayer starts playing. I tried calling getDuration () in the onReady event callback, but it returns -1. When I call getDuration () in the onPlay event callback, I get the correct value. Any ideas?

Here's my code:

<video src="movie.mp4" id="video" width="800" height="600"></video>
<script type="text/javascript">
jwplayer ('video').setup ({
    flashplayer: '/js/mediaplayer-5.10/player.swf',
    width: 600,
    height: 400,
    events: {
        onReady: function () {
            var duration = this.getDuration();
            alert ('ready, duration: ' + duration);
        },
        onPlay: function (state) {
            alert ('play, duration: ' + this.getDuration());
        }
    }
});
</script>
like image 644
Redtopia Avatar asked Dec 26 '22 21:12

Redtopia


1 Answers

This is approximately how I have handled this problem:

var duration = 0;
jwplayer().onReady(function() {
  if (duration == 0) {
    // we don't have a duration yet, so start playing
    jwplayer().play();
  }
});

jwPlayer().onTime(function() {
  if (duration == 0) {
    // we don't have a duration, so it's playing so we can discover it...
    duration = jwplayer().getDuration();
    jwplayer().stop();
    // do something with duration here
  } else {
    ...
  }
}
like image 115
Scott Evernden Avatar answered Feb 16 '23 03:02

Scott Evernden