Start learning with our library of video tutorials taught by experts. Get started

HTML5: Video and Audio in Depth

Creating Play and Pause buttons


From:

HTML5: Video and Audio in Depth

with Steve Heffernan

Video: Creating Play and Pause buttons

The first controls we are going to build are simple play and pause buttons and then we will create a play/pause toggle button like you see here in the final version, where we can toggle between them. All right, so let's go over to our code and the first thing we are going to do is add elements for our play and pause buttons. I am going to put them in the video_ controls div, and so I am going to create a div for our play button with the id of play_button and I am going to put some text in it and I will put Play and end the div.
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

Watch this entire course now—plus get access to every course in the library. Each course includes high-quality videos taught by expert instructors.

Become a member
Please wait...
HTML5: Video and Audio in Depth
2h 7m Intermediate Jun 02, 2011

Viewers: in countries Watching now:

In this course, author Steve Heffernan contextualizes the state of HTML5 audio and video and shows how to embed media into a web page using HTML5. The course sheds light on configuring the new HTML5 audio and video tags, ensuring backwards compatibility by setting alternative playback methods, encoding the needed file formats, and building custom controls with JavaScript. Also included are tutorials on troubleshooting known issues and reviewing real-world examples of HTML5 audio and video.

Topics include:
  • 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
Subjects:
Video Web Web Video Web Development
Software:
HTML
Author:
Steve Heffernan

Creating Play and Pause buttons

The first controls we are going to build are simple play and pause buttons and then we will create a play/pause toggle button like you see here in the final version, where we can toggle between them. All right, so let's go over to our code and the first thing we are going to do is add elements for our play and pause buttons. I am going to put them in the video_ controls div, and so I am going to create a div for our play button with the id of play_button and I am going to put some text in it and I will put Play and end the div.

All right, and let's do another one for our Pause button. An id of pause_button, we'll give it a text of Pause, and end the div. Now let's see. Let's save that and go look at our browser, open up the page and reload it. Okay, so we have both Play and Pause there, but they are kind of smooshed together. So let's now go and style them.

I am going to style them in the same way. So I am going to give them both a class of player-button. I am going to copy that class and paste it to the Pause button as well. Okay, so now they both have the same class and let's go to our CSS and we are going to style that. The class was player-button and we will give it a width of 50 pixels so that will be a little bit wider and we want the text to be centered on the button.

So we do text-align: center. And finally these are buttons, so we want the cursor to kind of turn into the pointer when we roll over them. So we can do that with CSS through cursor: pointer. All right, let's save that and then look at it again. Reload it. Good. So now we have a Play and Pause button and they're spaced apart and you can see when we roll over them we get the little pointer. All right, so now let's go and make these work in JavaScript. Go to our JavaScript file.

I am going to stay inside the DOM ready block here. Move a couple down. Let's first make a section for it, just to divide everything up, so a separator and we are going to use the jQuery selector to select our Play button, which had an ID of play_button, and what we can do now is bind to that click event. So jQuery allows us to pretty easily cause things to happen when an event happens.

We will do that by binding to an event and the div has a click event that we can bind to. So I am going to bind to the click event. When that happens we can pass a function to the event that we want to happen when the click happens. So we're in that line. I am going to drop this down and so what we put in here is what we want to happen when the click happens. And so we have a reference to our video element and that is the video variable. So we are going to use that and then the video element has a Play method on it that we can call to play the video.

So to summarize, we are selecting our Play button and saying we want to bind to the click event and when the click event happens, I want the video to play. Okay, so let's go ahead and do the same thing for the Pause button. Acutally, I can go ahead and just copy that whole line and paste it down. We want to change the id now to pause_ button, still binding to the click event, but now we want the video to pause and the video element has a pause function that we can call as well.

Okay, so let's go ahead and save that and go to our browser and reload and try this out. (Music playing) All right. (Music pauses and plays) Both Play and Pause work as expected. Okay, now let's go and build that Play/Pause toggle button from the final version here. (Music plays and pauses again) Where we can kind of toggle between Play and Pause like you might expect from a normal video player. All right, so let's go to our HTML first and I'm going to delete our Pause button, because we are just going to go down to one button.

I am going to modify our Play button. I'm going to just rename it to play_toggle. That's what we are going to call it. I am going to keep it in the class of play_button and keep the text at Play. So let's save that and then we don't have to do anything in CSS. We will go to our JavaScript. Now let's kind of do the same thing and start a new one. I'll use jQuery to reference this new button to play_toggle, and again, we're going to be binding. Say bind to the click event. And we are going to pass a function that we want to happen when the click event happens.

This time we are going to check if the video is currently paused and we can do that with an if statement, and the video element has a paused attribute that we can check just to see if the video is currently playing or if it's paused. And so if the video is paused, what we want to happen is for the video to play. If it's paused and we click the button, we want the video to play; otherwise else we want the video to pause, just like we did in the Pause button.

So if the video is paused we want to play and if the video isn't paused we want it to pause. And now there is one more step we want to do and that's to change the text as we are clicking Play and Pause. We want the text on the button to change. So I am going to use jQuery again to select our button, but we don't need to use the ID again, because in the scope we're actually in the scope of the button already, so we can actually use this. This will refer to the button. So we want to say this, and then we can use the HTML function of jQuery which would change the contents of our button.

We want the contents of our button now to say Pause. So if the video is paused we want it to play and then we want the text on the button to say Pause. All right, now I am going to do the same thing for Play and let me end those lines and save that. Okay, so now if the video isn't paused, we will pause it and change the button text to say Play.

Okay, let's save that and go to our browser and reload and click Play. (Music playing) All right, and the text changed to Pause. Click it again and it pauses the video and changes the text of the button to Play. (Music playing) All right, so that's how you build some basic Play/Pause buttons and now we will build a play progress bar.

There are currently no FAQs about HTML5: Video and Audio in Depth.

Share a link to this course
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.
Upgrade now


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.

Upgrade now

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
Welcome to the redesigned course page.

We’ve moved some things around, and now you can



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.

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