jwplayer - how do I get video duration before playback?

妖精的绣舞 提交于 2019-12-10 09:22:30

问题


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>

回答1:


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



回答2:


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



回答3:


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



回答4:


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.




回答5:


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

    }
},



回答6:


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
}


来源:https://stackoverflow.com/questions/11438047/jwplayer-how-do-i-get-video-duration-before-playback

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!