Viewers: in countries Watching now:
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.
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.
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.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.