Jwplayer - how can I get the duration of a video before playing?

I am trying to get the duration of a video before starting jwplayer playback. I tried calling getDuration () on the onReady callback, but it returns -1. When I call getDuration () on the onPlay event callback, I get the correct value. Any ideas?

Here is 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> 
+4
source share
6 answers

This is roughly how I dealt with 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 playing so we can discover it... duration = jwplayer().getDuration(); jwplayer().stop(); // do something with duration here } else { ... } } 
+4
source

It's true. I had no solution, so I used two events. OnBuffer and OnPlay as shown below. It provides -1 in the first event and a valid length in the second event. This may be helpful. Well, I'm looking for any other solution if it comes out.

  var duration; jwplayer().onBuffer(function () { duration = this.getDuration(); alert(duration); } ); jwplayer().onPlay(function () { if(duration==-1) duration=this.getDuration(); alert(duration); } ); 
+3
source

JW Player will only show video playback duration

 $.each(all_video_ids, function(video_idx, video_id){ console.log(video_id); var temp_video = jwplayer(video_id); var metaData = temp_video.getMeta(); console.log(metaData); console.log(temp_video); console.log(temp_video.getDuration()); if(temp_video.getState() == "PLAYING"){ temp_video.pause(); } }); 
+1
source

It’s just that the guys recently analyzed other video players in this matter, and all video players except the tube show the duration of the videos at first as 00:00:00 Most likely, youtube saves the duration in DB and shows it

Otherwise, @Scott Evernden's idea makes sense

http://mediaelementjs.com/ , http://www.videojs.com/ and basically a site other than youtube that has a duration of 00:00:00 jwplayer, like other players, provides a duration of video after a game event .

0
source

Since getDuration does not seem to work if the video is not actively playing (even when paused). I'm doing it:

 onPlay: function(){ var obj = this; //save a reference to 'this' jw object for reference in the setInterval callback. if (!obj.playBegan){ //we only want this to trigger once when play begins (not on seek or ffd) obj.playBegan = true; obj.durationProcessor = setInterval(function(){ if (obj.getDuration() !== -1){ //sometimes the movie duration isn't immediately available. clearTimeout(obj.durationProcessor); console.log(obj.getDuration()); } }, 250); //look for a duration every .25 seconds } }, 
0
source

You can get the duration when the player is ready and get the value of the right controller, for example:

  var duration = $('.jw-group.jw-controlbar-right-group.jw-reset')[0]; var spanText = $(duration).children('.jw-text.jw-reset.jw-text-duration') youTubeVideoDuration = ($(spanText)[0]); 

he will choose the duration from the player if you want to convert it to a relative time, that is, in seconds, you can use the function

 function toseconds(hms) { var a = hms.split(':'); // split it at the colons if (a.length == 3) return ((+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2])); // if video is in hrs else return ((+a[0]) * 60 + (+a[1])); // video is in minutes } 
0
source

All Articles