Ready to watch this entire course?
Become a member and get unlimited access to the entire skills library of over 4,900 courses, including more Video and personalized recommendations.Start Your Free Trial Now
- View Offline
- Understanding the differences in media formats
- Preloading, autoplaying, and looping in HTML5
- Setting a video's width and height attributes
- Displaying subtitles and captions
- Generating embed code with an embed code builder
- Encoding files using HandBrake, Firefogg, and VLC
- Creating a load progress bar
- Recognizing Android and iOS issues
Skill Level Intermediate
The next control we are going to build is a load progress bar which shows us how much of the video has been downloaded. I'll show you an example here. (Music playing) If I reload the page, you could see the blue bar grow as the video plays. Now if you are loading the video from your computer, the blue bar with probably jump all the way to the end right away, but I am loading this from the Internet so we can actually see the gradual downloaded video. So now we are going to go and build this bar.
Let's go back to our code. I am going to start right where we left off with the play_progress and we are going to add another div inside the progress bar holder and this one is going to be for our load_progress. And we are going to put this above the play_progress because we want the green bar to show in front of the blue bar so that even wants the entire video has been downloaded the green bar still shows in front of it. So I am going to add a div with an id of load_progress and add div.
And then with the Load Progress, we are going to bind to the progress event. The progress event is triggered continuously as the video loads from the Internet. So I am going to bind to the video element again and the progress event, all right. And then we need to pass a function to it so that we can tell it what to do.
And then the property we're going to use to get the loaded amount is the buffered property of the video element. This isn't a simple percentage unfortunately. It's in the form of an object called a time range which has multiple pieces to it. It actually stores an array or list of offered amounts for the video or at least it will in the future. Right now we just need two pieces of it and first is the length. I am going to start with a conditional statement because we are actually just checking to make sure there is length. So if video, and then we are using the buffered attribute, and then the length attribute of buffered, and we want to make sure that the length is greater than zero.
Because if we try and use the buffered attribute before there's any length to it, it can throw an error and we don't want that. Now we are basically calculating percent in a similar way that we did for the play_progress. So I am going to go ahead and create a new variable. We'll just call this percent and now we are going to use another attribute of buffered, which is end, and that's actually a method that we will pass zero to.
That's not very straightforward but what we are basically doing is looking at the end in seconds of the first buffer in the list. So zero means the first buffer in the list and we're looking at the end of that. All right, so it's a little bit complicated but once you done it the first time you can basically just copy and paste and know how it works. Then we are going to divide that. We got the amount downloaded in seconds, so we can divide that by the video's duration and so we can do video.duration.
It's very similar to the play progress and so just like that we're going to multiply the whole thing again by 100 so we can get the integer representation of it. Okay, so now we're dividing the amount that's been downloaded in seconds by the duration and multiplying that by 100 to get our percent and basically how big we want the load bar to be. So now let's actually apply that to the load bar. We are going to select it with jQuery and the load_progress id that we gave to it.
And we can use the CSS method again to change the styles dynamically and we change the width and give it our percent that we just calculated, plus the percent character so that CSS knows that it is the percent that we're passing to the width, and we will end that line. Now if we save that and we go to our web browser, open up that page again and reload it, we'll play it so you'd see the bar jump again.
(Music playing) Now it didn't start loading automatically because we have preload none on our video tag so it knew not to start preloading it. But as soon as we played, it started loading video and since the video is local, it just jump directly to the end. So just so we can see it in action I want to go back to our HTML and load up the version that's on the web site. So I'm using Firefox. I am just going to change the URL of the webm version which Firefox is playing. I am going to change that to the www.explorercalifornia.org.
So now we are pointing to the one that's hosted on the web and I am going to change preload to auto. So now it'd just start preloading automatically. We go back to Firefox. Reload the page. We can see the blue bar now start to grow gradually as the browser downloads the video. Okay that's what we want to happen. Now if we hit Play we can see the green bar still on top of the blue bar. That's what we want too. Okay, now unfortunately browsers aren't super consistent when they trigger the progress event.
And then in this function we're going to take all this code that we just wrote in the progress event and cut that and paste it in our new function. So now it's a little bit more portable. It's in the form of a function that we can call in multiple places. Now we're going to actually copy that name and put it in our progress event so we continue to call the same code that we just copied out of it. All right, so now it's actually doing the exact same thing. We just have a more portable bit of code that calculates the load progress.
So if we save that and go back to our browser, we should see the same thing happen. Okay, yes, so the blue bar is still continuing to grow. So, good. Now what we can do is actually call this update load progress method in a couple of different events that the video element might trigger to make sure that it's backed up and actually gets called. So what I am going to do is actually just copy this line for our progress event and paste it. We are going to use it three more times actually.
So the first time we want to bind it to the progress event. Next time we are going to bind it to the loadeddata event, which is called whenever the browser starts loading data. And so this is just a backup in case the browser doesn't call that progress event. It will call it when the browser first starts loading the data. All right then the next time that we want to make sure the load progress is calculated is on the canplaythrough event.
It's another event that the browser will trigger. In this case it plays it when the browser thinks it's downloaded enough information that it can play through the video without having to stop and wait for more data to download. So that's just another point that we want to make sure that the load progress has been calculated. In the last one we are actually going to bind it to the playing event. And the playing event it's just called continuously throughout as the video is being played. We are binding it to playing just so that it continues to calculate continuously through the playback of the video.
So let's save that and just make sure that it continues to work as expected and I am going to play the video. (Music playing) It seems to work, great! That's the end and it finishes loading. That's working as expected and that's how we create a load progress bar.