Wait until an HTML5 video loads

html5 video buffering progress
html5 video events
html5 video buffering event
jquery video load
html5 video preload
html video controls
html video stop
play video before loading website

I have a video tag, that I dynamically change its source as I am letting the user to choose from a number of videos from the database. The problem is that when I change the src attribute the video doesn't load even if I tell it to.

Here is my code:

$("#video").attr('src', 'my_video_'+value+'.ogg');
$("#video").load();
while($("#video").readyState !== 4) {
        console.log("Video is not ready");
};

The code still stays in a infinite loop.

Any help?

EDIT:

To Ian Devlin:

//add an listener on loaded metadata
v.addEventListener('loadeddata', function() {
    console.log("Loaded the video's data!");
    console.log("Video Source: "+ $('#video').attr('src'));
    console.log("Video Duration: "+ $('#video').duration);
}, false);

Ok this is the code I have now. The source prints great, but I still can't get the duration :/


You don't really need jQuery for this as there is a Media API that provides you with all you need.

var video = document.getElementById('myVideo');
video.src = 'my_video_' + value + '.ogg';
video.load();

The Media API also contains a load() method which: "Causes the element to reset and start selecting and loading a new media resource from scratch."

(Ogg isn't the best format to use, as it's only supported by a limited number of browsers. I'd suggest using WebM and MP4 to cover all major browsers - you can use the canPlayType() function to decide on which one to play).

You can then wait for either the loadedmetadata or loadeddata (depending on what you want) events to fire:

video.addEventListener('loadeddata', function() {
   // Video is loaded and can be played
}, false);

How to preload an entire html5 video before play, SOLVED, How to preload an entire html5 video before play, SOLVED Modern browser supports partial downloads which means that they initially only change this code slightly so all other elements wait to start until the video has  How to preload an entire html5 video before play, SOLVED so all other elements wait to start until the video has fully loaded, then both start at the same time


In response to the final part of your question, which is still unanswered... When you write $('#video').duration, you're asking for the duration property of the jQuery collection object, which doesn't exist. The native DOM video element does have the duration. You can get that in a few ways.

Here's one:

// get the native element directly
document.getElementById('video').duration

Here's another:

// get it out of the jQuery object
$('#video').get(0).duration

And another:

// use the event object
v.bind('loadeddata', function(e) {
  console.log(e.target.duration);
});

Check when an HTML5 video has loaded, But, if you've read anything else on my blog before, you know I like to go for the simplest solution and this is what I came up with. Here's some  How to show progress bar until the page completely loads in HTML5/CSS3? If you are using huge number of images and just want your users to wait until they get


you can use preload="none" in the attribute of video tag so the video will be displayed only when user clicks on play button.

<video preload="none">

HTML Audio/Video DOM loadstart Event, HTML Audio/Video DOM loadstart Event Alert that the video is starting to load: The loadstart event occurs when the browser starts looking for the specified  Definition and Usage. The load() method re-loads the audio/video element. The load() method is used to update the audio/video element after changing the source or other settings.


call function on load:

<video onload="doWhatYouNeedTo()" src="demo.mp4" id="video">

get video duration

var video = document.getElementById("video");
var duration = video.duration;

HTML Audio/Video DOM loadeddata Event, The loadeddata event occurs when data for the current frame is loaded, but not enough data to play next frame of the specified audio/video. During the loading  Show Loading Image in Webpage while Page Loads example. Show Loading Image in Webpage while Page Loads example. Skip navigation Sign in. Search. Loading Close. This video is unavailable.


HTML5 Video Player Best Practices, It took more than a decade to create a <video> tag and actually get browser support for it -- far too long of a wait, so much so that many sites still  I want to change between two pages in html with javascript, but when I change with window.location, the code that is after this sentence continues executing. So when I do, for example, a call to


How to Make HTML Videos Run Smoothly, How to Make Your HTML Video Run Smoothly on Desktop and Mobile. The idea is to wait until the video fully loads and then hit play. Not earlier! Here is my  Here’s how a page loads a script without either defer or async, put in the head portion of the page: The parsing is paused until the script is fetched, and executed. Once this is done, parsing resumes. No defer or async, in the body. Here’s how a page loads a script without defer or async, put at the end of the body tag, just before it closes:


Media events, embedded in HTML documents using the audio and video elements; this section This is sent, for example, when the media has loaded enough that the waiting, Sent when the requested operation (such as playback) is  I'm trying to add a canvas over another canvas – how can I make this function wait to start until the first canvas is created? function PaintObject(brush) { this.started = false; // get handle of the main canvas, as a DOM object, not as a jQuery Object. Context is unfortunately not yet // available in jquery canvas wrapper object.