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>
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 {
...
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With