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

Creating a Play Progress bar

From: HTML5: Video and Audio in Depth

Video: Creating a Play Progress bar

All right, the next control we are going to build is a progress bar, which is a visual indicator of how far the video is time- wise through the total duration of the video. We can see an example of it here. It's the green bar that continues along as the video plays. (Music playing) Nice! We are going to go and build that. So let's go to our code, and the way we're going to do this is by adding two elements. The first is going to be the container and for that we are going to use a div and we will give it an id of progress.

Creating a Play Progress bar

All right, the next control we are going to build is a progress bar, which is a visual indicator of how far the video is time- wise through the total duration of the video. We can see an example of it here. It's the green bar that continues along as the video plays. (Music playing) Nice! We are going to go and build that. So let's go to our code, and the way we're going to do this is by adding two elements. The first is going to be the container and for that we are going to use a div and we will give it an id of progress.

What this is going to do is hold our other element which is going to be the actual green bar. Give that an id of play_progress. End div. So the second element is going to grow inside of the first, and that's how we need for that. So let's save there and let's go to our CSS and style this.

So for our outer element which had an id of progress, I can give it a background color so we can see it. And we'll give it a gray and we will also give it a width of 310 pixels so it doesn't take up the whole bar. Then let's style the play_progress bar to an id of play_progress and a background, we will give it that green color, #6C3.

Okay. And then, finally we want to add some positioning to these elements so that the inner one sits in the upper left-hand corner of the outer one, even when we add an additional element later on. So for progress I am going to give it a position relative, which won't make that position any differently. But for the second one, I am going to give it a position absolute which will make sure that the inner element always sits in the upper left-hand corner of the outer element. So the green bar will always be in the very upper left-hand corner of the outer Gray Bar.

All right! Let's save that and then we'll go make this work in JavaScript. First, let's make another section for it. Let's call it Play Progress so we can see it, and we use a jQuery selector. But this time we can use the video variable, because we are actually going to be binding to an event on video itself. So we use the same bind method from jQuery that we used before but the event we are going to use is the timeupdate event.

And timeupdate is an event that's triggered regularly as the video plays. So the browser will trigger this event a couple of times a second as the video plays. So we want to pass a function to this to tell it what to do when the time update happens and we'll end that. The first thing we need to do is actually calculate how big the bar should be. To do that, let's store the result first in a variable called timePercent and we are going to use two attributes of the video element.

That will give us the information we need. Basically we are going to be dividing the current time of the video by the total duration of the video to get a percentage of how far through the video we are. In this case we are in the scope of the video already so we can use the this variable to represent the video. So I am going to use this.currentTime and that will give us the playback time of the video, and we'll divide that by this, which is the video duration, and that will give us the current time in seconds divided by the total duration in seconds.

That will give us a decimal, which is the percentage time through it. So the last thing we want to do is actually multiply this whole thing by 100 so we can get an integer percentage as opposed to a decimal. All right! So then we need to take this percent and actually change the size of the green bar. So to do that, we can select our progress bar using jQuery and the id of play_progress that we put on the progress bar.

And with jQuery we can change CSS on- the-fly using the css method and we are going to change the width of the bar. For the width, we are going to pass the variable timePercent that we made before and then plus the percent character. So CSS knows that the width is going to be a percent that we just calculated.

Let's save that,and go back to our browser, and load up the page we were on, reload it. Okay, so now we can see that the gray bar is there for our progress element and if we play it, the green bar starts to grow, and goes along with the video. (Music playing) Okay, great! So that is how we build a play 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 · 15250 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 preferences from 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.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

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.