02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

HTML5 Video Play and Pause Event

To create a real video player we need to be able to not only using the HTML5 video API to play and pause our video but we want to know when the video pauses or plays without using our controller so we can update it to reflect the changing states. In this video we learn how to listen to these changes.

Video Transcript

00:00All right, so so far we've been controlling the functionality of the video player externally
00:05But we didn't really know what was going on
00:07So we want to take it one step further and now maybe take our play pause button to actually say what's actually happening in
00:13The video at any given moment to do that
00:15We're gonna start first by changing the copy to be play slash pause and change it to be play or pause
00:22So let's start our way would first of all when we initially start our application in this scenario
00:25Our play button is basically our value should be play
00:30So I'm gonna go right into that button. I'm just gonna change the value to be by default play
00:35Now the next thing that I could immediately do is as soon as our document basically
00:39As soon as we pause our video I could go to this element because the toggle is connected directly to our button
00:45So it's this and directly to this I could change the inner
00:50HTML and just replace the inner HTML with play. Oh
00:54In this case pause
00:57And we want to do the exact same thing for the play area only in the play area
01:02We want to add a pause. We want to add a play because it's paused
01:06Okay, so we have the exact opposites if we go into our browser just to double check and make sure that everything works
01:13We'll see that our play button initially is there once we click on the play button
01:17It will turn into pause when we click on pause it will turn back into play and vice-versa
01:22Now our only problem is is that if we click on the play button it directly in the player
01:26We're not getting any updates to reflect it to our button
01:29And that's going to be our next stage where we start working with events and we're going to ask from our
01:35Video player to tell us once it's playing and when it's paused to do that
01:40I'm going to go right back into our editor
01:41And I'm going to come right up here into the on window load and once the video is loaded in my video itself
01:47I'm going to add a new add event listener
01:51Now the add event listener has quite a lot of different events and every div sorry
01:55We wrote here div, but every div element has these events and every event has a different set of functionalities in this case
02:03We want to ask our video to call us whenever it starts playing whenever it starts playing. We're going to call the on
02:10play
02:11callback
02:13And to be honest
02:14We don't even have to say callback because we said you're on and on is kind of like a synonymous to put so to that
02:20Call back and we could do the same thing for pausing. So I'm going to create here a callback for pause
02:26So once our video pauses and once our video plays we're going to have a specific playback for that
02:32So it's going to be pause on play on pause and we're ready to create two functions to callback functions on play
02:39The on play is an event which is going to get an event L object
02:43And so is the pause so I'm going to create a function on pause now again because this we're not teaching
02:50JavaScript what we're teaching how to use videos in JavaScript
02:53I'm not going to get into the midst of what events are if you don't know what events are I strongly recommend that you join
02:59Our JavaScript course where we explore and learn
03:02How to work with events and what events are in general and how to work with them all they'll say in the scenario is basically
03:09We're telling the video player to trigger this function once it starts playing and we'd say so video player to start trigger to trigger
03:16The pause function on pause function once a pause event happens
03:21So what we want to do when an on play happens, we want to basically capture that
03:26Button so to do that
03:27We know our button has an ID of toggle so I could just fetch that toggle
03:31Document toggle button and just approach it directly and maybe even just immediately in line change the inner
03:38HTML and again set it to the value that it should be so when it's playing we want the copy to be paused and
03:45when the video is paused we want the copy to be play and
03:52Play all right, so it's very very similar to what we've done before
03:55But now when we click on our video once we start our video
03:59although initially our play value will be set to be the copy of play as soon as we press on the play button our
04:06Pause value will get updated and once we click on the place and now there's a direct relationship a two-way
04:13relationship between our play and pause that our button is controlling externally and we could have different controls controlling our video playback at
04:21The same time alright, so that's the first step in working with events
04:26And we're going to continue exploring the video API and see what other things we could do with it in the following videos

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!