Thursday, May 23, 2024
 Popular · Latest · Hot · Upcoming
94
rated 0 times [  98] [ 4]  / answers: 1 / hits: 21574  / 14 Years ago, mon, february 28, 2011, 12:00:00

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 unreliable. Is there a better way, or is this safe?



Note, I will be keeping a localStorage cache of videos that have been completely downloaded by each user, so I'll know in advance if a video has already loaded, and could probably bypass progress->buffer if that's a sticking point.



Thanks in advance.


More From » html

 Answers
7

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

[#93548] Friday, February 25, 2011, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mckinleyk

Total Points: 730
Total Questions: 99
Total Answers: 99

Location: South Georgia
Member since Fri, Nov 13, 2020
4 Years ago
mckinleyk questions
Tue, Sep 7, 21, 00:00, 3 Years ago
Fri, Aug 21, 20, 00:00, 4 Years ago
Sun, May 10, 20, 00:00, 4 Years ago
Wed, Mar 25, 20, 00:00, 4 Years ago
Fri, Sep 27, 19, 00:00, 5 Years ago
;