jwplayer - how do I get video duration before playback?

痞子三分冷 提交于 2019-12-05 14:07:39

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 {
    ...
  }
}

Its true. I did not get any solution for that so i used two events. OnBuffer and OnPlay like below. It provides -1 in first event and real length in second event. It might be helpful. Well i am looking for any other solution if exits.

           var duration;  
           jwplayer().onBuffer(function () {
            duration = this.getDuration();              
            alert(duration);
            }       
    );

    jwplayer().onPlay(function () {
            if(duration==-1)
            duration=this.getDuration();
            alert(duration);
            }       
    );

JW Player will only show duration when video is playing

$.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();
        }
    });

Just heads up in this question guys recently i analysed other video player as well and all the video player except you tube are showing video duration initially as 00:00:00 Most probably youtube is saving the duration in DB and Showing it

Otherwise @Scott Evernden idea makes more sense

http://mediaelementjs.com/, http://www.videojs.com/ and mostly other site except youtube has duration as 00:00:00 jwplayer also like other player provide video duration after play event.

Since getDuration doesn't seem to work unless the video is actively playing (not even when paused). I do this:

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

    }
},

You can get the duration when player gets ready and fetch the value of right controller like :

  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]);

it will fetch the duration from player if you wish to convert it into relative time ie in seconds then 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
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!