Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Creating a Load Progress bar

From: HTML5: Video and Audio in Depth

Video: Creating a Load Progress bar

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.

Creating a Load Progress bar

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.

Okay, that's all we need in the HTML and let's go to our CSS. For this I am basically going to do the same thing we did with the play_progress. So I am going to copy that and paste it and change the name to load_progress. We want the position to be absolute again and I am going to change the background color to that blue which is #69C. Now I am going to go to our JavaScript, which is where we are going to do the bulk of the work here, and let's start with a new section called Load Progress. All right.

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.

So we actually need to add some backups to measure the load progress gets calculated. So to do that let's go back to our code and we're going to first go to JavaScript. We are going to break out this code here that calculates the progress into its own function so that we can call it from multiple events. So below this we are going to make a new function. I am going to call it updateLoadProgress, all right.

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.

Show transcript

This video is part of

Image for HTML5: Video and Audio in Depth
HTML5: Video and Audio in Depth

37 video lessons · 14758 viewers

Steve Heffernan
Author

 
Expand all | Collapse all
  1. 1m 24s
    1. Welcome
      42s
    2. Using the exercise files
      42s
  2. 15m 18s
    1. Understanding HTML5 video and audio
      4m 46s
    2. The state of HTML5 video and audio
      4m 29s
    3. Understanding the format war
      3m 53s
    4. Configuring your server to deliver HTML5 media formats
      2m 10s
  3. 32m 36s
    1. Using the video and audio tags
      5m 34s
    2. Using multiple sources for browser compatibility
      5m 53s
    3. Preloading
      4m 16s
    4. Autoplaying
      2m 2s
    5. Looping
      5m 11s
    6. Setting a poster frame
      3m 43s
    7. Setting the video width and height
      3m 40s
    8. Displaying subtitles and captions
      2m 17s
  4. 12m 40s
    1. Falling back to Flash
      4m 4s
    2. Falling back to download links
      6m 21s
    3. Using an embed code builder
      2m 15s
  5. 22m 17s
    1. Understanding HTML5 video formats
      5m 10s
    2. Understanding HTML5 audio formats
      2m 57s
    3. Encoding MP4/H.264 video with HandBrake
      3m 13s
    4. Encoding Ogg, Theora, and WebM with Firefogg
      3m 18s
    5. Encoding audio formats with VLC
      5m 54s
    6. Automating video and audio encoding
      1m 45s
  6. 34m 59s
    1. Setting up the JavaScript
      4m 39s
    2. Creating Play and Pause buttons
      7m 52s
    3. Creating a Play Progress bar
      5m 49s
    4. Creating a Load Progress bar
      9m 29s
    5. Creating a Current Time and Duration display
      7m 10s
  7. 4m 37s
    1. Overview of known issues
      35s
    2. Autobuffer or preload?
      56s
    3. Load progress in Firefox 3.6
      41s
    4. Android 2.0 issues
      1m 5s
    5. iOS 3 issues
      1m 20s
  8. 2m 8s
    1. Flight of the Navigator
      50s
    2. ZEN Audio Player by simurai
      34s
    3. The Wilderness Downtown by Google and Arcade Fire
      44s
  9. 1m 40s
    1. Further exploration
      1m 40s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now "Already a member? Log in

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed HTML5: Video and Audio in Depth.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.