HTML5 Video - File Loading Complete Event?

前端 未结 3 1840
广开言路
广开言路 2020-12-23 18:20

I need to detect when a video file has completed loading. I\'m thinking I should use progress->buffer, but in the back of my mind, I remember reading that this was unreliabl

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-23 18:53

    You can bind the "buffered" event, but (in Chrome at least) this works fine except that it doesn't call the last "buffered" event (i.e. it will detect 90%...94%...98%... but won't call on 100%).

    Note: recent versions of jQuery should use .prop() instead of .attr()

    To get around this I've used setInterval() to check the buffer every 500ms (where $html5Video is your element:

    var videoDuration = $html5Video.attr('duration');
    
    var updateProgressBar = function(){
        if ($html5Video.attr('readyState')) {
            var buffered = $html5Video.attr("buffered").end(0);
            var percent = 100 * buffered / videoDuration;
    
            //Your code here
    
            //If finished buffering buffering quit calling it
            if (buffered >= videoDuration) {
                    clearInterval(this.watchBuffer);
            }
        }
    };
    var watchBuffer = setInterval(updateProgressBar, 500);
    

提交回复
热议问题