New Feature: Playlist Center! Pick a topic and let our playlists guide the way.

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

Creating a Current Time and Duration display

From: HTML5: Video and Audio in Depth

Video: Creating a Current Time and Duration display

The next thing we are going to build is the time display, which shows us both how far we are through the video and how long the video is. If I play this video-- (Music playing) You can see the seconds go up as the video continues to play. That's what we are going to build. So, let's go to our code. The first thing we need to do is create our time element. It's going to be similar to our progress element. We are going to have a container element and that's going to be a div that will give an id of time.

Creating a Current Time and Duration display

The next thing we are going to build is the time display, which shows us both how far we are through the video and how long the video is. If I play this video-- (Music playing) You can see the seconds go up as the video continues to play. That's what we are going to build. So, let's go to our code. The first thing we need to do is create our time element. It's going to be similar to our progress element. We are going to have a container element and that's going to be a div that will give an id of time.

That's going to hold the two different times, both the current time and the duration. Then instead of divs, I am going to use the span element, because we just want the two times to sit next to each other. We can give the first one an id of current_time. This is going to be our current time display and that span. I am going to give it a starting value of 00:00. So 0 minutes and 0 seconds.

Then I am going to do another span and we will give that an id of duration and that's going to be our duration display. We will end that span and give it the same default of 00:00. That's what we need for our time display in the HTML. Let's go to our CSS and style it a little bit. So, our display the id was time and I am going to give it a width of 120 pixels and that will make it fit just right into the end of the control bar.

Then we also want to align the text center. So it sits right in the middle of that 120 pixels. That's what we need for the CSS. If we go to our JavaScript... So I am going to create another section like we've done before. Call it Time Display. The first thing I am going to do is actually cheat a little bit. What I am going to have here in another file is a function that's going to allow us to format the time, and I am just going to copy and paste that into our JavaScript.

It's really not worth writing it out step by step and digging into exactly what it's doing. You can feel free to read through it if you want, but basically what it does is we pass it a number in seconds and it creates that 00:00 format for us. So, we can just trust that that's going to work and you can feel free to copy and paste this function as well. So, then we are going to bind another event and that's going to be in our video element again and it's going to be the timeupdate event that we used for the play progress.

So, we are using the same event for the current time that we used for the play progress. That kind of makes sense. We will pass it a function of what we want to happen and end that. So what we want to happen is to change the content of that current time display. So, first we need to reference that current time display, which we called current_time and then we can use the jQuery HTML method to change the inner content of that span tag.

Then here is where we are going to call that formatTime function. So inside here I am going to go and call formatTime. To this we are going to pass the video's current time. So, we are in the video scope, because we bound the time update event to video. So I can use the this variable and call currentTime. So that will give us the current time in the video in seconds. So, we are then passing that number in seconds to the formatTime function, which will format that in the 00:00 format and pass it back as the new content for the currentTime span tag. We can end that.

Let's so ahead and save that and just check to make sure that's actually working. Go to our browser and reload the page we are working on. We get that 00:00. If we play? (Music playing) You can see the current time go up. So I'll pause that. And I notice that I missed one element from our HTML. So let's go back and add that quickly. I just wanted a slash after our first set of numbers so that there's a divider between the two numbers.

So, if I save that and go back and reload the page, we now have that nice slash to make it a little bit more obvious which number is which. Now, let's go back to our JavaScript and we will do the duration display. For that we can basically copy exactly what we just did for the current time and we will paste that and the event that we are going to bind to is the durationchange event, which is another event that's called by the video element and it's triggered anytime the duration changes, including when it first loads the duration.

So there may not be a lot of times when the duration actually changes throughout the video, but we want it to change right when it first loads that duration, the first time. Then the element we want to change is the duration span tag that we created. Then again we want to change the contents. So we are using the HTML method and the formatTime function. We are going to pass the video's duration this time to the formatTime function. So, we are passing the duration in seconds, the format time, which will make it the 00:00 format.

Then I will change the content of our duration span to that number. Let's save that pretty quick and reload the page. We can see as soon as the video loads, it loads that duration and changes the duration time display to the total tome of the video. If we play the video? (Music playing) We can see the current time go up. Okay, great! So, our time display is working now and our player at least for this tutorial is finished.

From here you can continue to build on your player and make it look exactly how you want. There are also a number of open source HTML5 video players out there that you can look to for inspiration if you want to take your player to the next level.

Show transcript

This video is part of

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

37 video lessons · 14499 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

Are you sure you want to delete this note?

No

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.