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

Looping

From: HTML5: Video and Audio in Depth

Video: Looping

At some point you may have a need for the video to loop, meaning, when it finishes it goes back to beginning and plays again. I can only think of one specific use case for this and that's when you're using video as a type of design animation to add to the ambiance of the page, like clouds scrolling in the background. Be careful with that though, because a lot times an element like that can actually be distracting for the user and take away from the experience rather than adding to it. With that in mind though, let's add looping to our video. Going over to our code we're now in the 2-05 folder, if you're following with the exercise files.

Looping

At some point you may have a need for the video to loop, meaning, when it finishes it goes back to beginning and plays again. I can only think of one specific use case for this and that's when you're using video as a type of design animation to add to the ambiance of the page, like clouds scrolling in the background. Be careful with that though, because a lot times an element like that can actually be distracting for the user and take away from the experience rather than adding to it. With that in mind though, let's add looping to our video. Going over to our code we're now in the 2-05 folder, if you're following with the exercise files.

We have our video tag with the controls attribute, all our sources, preload attributes set to auto, and an autoplay attribute so the video starts playing automatically. And what we need to do to add looping is add the loop attribute. This is a Boolean attribute, so it is either true or false. It's true if we include it, and false if we don't. And I'll save that. And there is one more thing, I've added a clouds movie, just like we're talking about using a clouds movie in the background, just to kind of give us a better idea of what that would look like.

So I'm just changing the podcast movie to clouds and I'll save that and then we can go over. I'm going to preview this in Chrome or reload it and we can see the video is playing. It is playing our clouds video and hopefully when it gets to the end it will just go right back to the beginning. Okay, great, so looped as expected. Now one unfortunate thing with loop and the reason I'm using Chrome for this one, is that Firefox does not yet support it, while all other HTML5 browsers do.

So let's go and look at Firefox. If we reload the same page, now I'll jump to the end, you see it just stops so it doesn't go back to the beginning like we would expect it to do with loop. So up to the first release of Firefox version 4, it doesn't support loop. I haven been told that they will support it eventually, but it will take more work for them than it might seem, because the need for a smooth transition from the last frame back to the first. So we don't know exactly when that would be, but they haven't at least decided not support it at all.

So to get around this limitation I'm going to dive into JavaScript and show you a quick way to manually loop the video with JavaScript. I'll spend a little bit more time digging into JavaScript in later movies, so if any of this is really above your level, you might want to skip ahead to those and come back to this later. But really this shouldn't be too difficult to just copy and paste either way. So let's go back to the code and the first thing we're going to do is actually remove our loop attribute, because we're going to loop it manually and we don't want any kind of unexpected behavior from browsers that do support the loop attribute.

And then next we're going to add an id to our video tag, so we can to reference it in JavaScript. We'll give an id of myVideo. Okay, now we're going to add our JavaScript just below the video tag, and we use the script tag to add JavaScript. We don't need to type equals text JavaScript in HTML5, which is nice. Well, all right, and then to reference our video tag, I'm going to use jQuery. jQuery is a JavaScript library that were including in the page, and so if you want to do this as well you want to make sure that you're including jQuery and it is including all the Explore California assets, so if you were following along with the other tutorials, it should work there as well. And so with jquery we can the reference the file like this, myVideo, so that will reference our video tag and then we're going to bind. Bind is the method where we can bind to an event.

So binding to the ended event, so basically the video element has an ended event that fires every time that the video ends, and so we want to bind a function to that event and then we can pass a function to the bind to tell it what we want it to do when the end event happens. We're now kind of in the scope of the video element, so we can use the this variable to reference our video. This represents the video element and then the video element has a play function that will just tell the video to play.

Okay, so we're saying, with my video we want bind to the ended event and any time the video ends, we want the video to play. So pretty straightforward. I'm going to go ahead and save that and then go to Firefox and reload the page. I'm just going to jump ahead to the end and it goes back to the beginning, great! So, we just looped it manually. I'm just going to go over to Chrome real quick to make sure it's still working there. Fast forward it.

All right, now I'm back to beginning too. So great! Now we have manually looped it and we don't need the loop attribute, so it'll work as expected in all the browsers. With that though, you should be able to loop your videos consistently when needed and the exact same thing will work for audio. But again with both video and audio be careful with looping because in more cases than not it could be a distraction or annoying for your users.

Show transcript

This video is part of

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

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