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

Jetting through the basic API controls

In this video we will be running through everything we learnt in the first course on Video in HTML5 only doing it really quickly as a recap.

Video Transcript

00:00Welcome back. In our course on videos, which we did, we went through all the basic controls of the media component,
00:07but because this is a standalone course on audio, I wanted to go through those controls again and just as a refresher,
00:14so this is going to be a little bit faster. If you haven't taken our video course yet,
00:18I strongly recommend that you first watch that before you dig much deeper into this,
00:22because it's going to be a much faster paced video.
00:25Alright, with that said, I've set in advance a few controllers.
00:28Some of these are HTML components that don't work on every single browser,
00:32but we're just going to use them now in Safari for testing as we're going to build things out.
00:37So in our sublime text, I just want to go through them really quickly.
00:41I've added a new controls div layer, and I'm going to move our files into control.js,
00:47but let's go through them. So first of all, we created a toggle button that will play and pause our video,
00:53which we are sound in this case. A dimmer doesn't make any sense anymore because there is no dimming capabilities, really, right?
01:03Because there's no view. We have a seeker, which is an HTML 5 range component that enables us to seek through inside of our sound file
01:13just as much as we could have inside of our video file.
01:16We have our muter, which enables us to mute or bring back our sound,
01:21and we have a volume control, which is a number type, which again is an HTML5 input component,
01:26which isn't supported on every single browser, but you just want to make sure that you could use a button
01:31or anything of that case, or make sure you're working with Safari or with a browser that has these capabilities already set up in them.
01:39Alright, so first of all, I'm just going to copy the JavaScript that we have inside of our old script
01:43and just delete it, and then we'll move into this new HTML file.
01:48So I'm just going to browse through it, go right into our control.js file that was just created.
01:54I'm just going to delete it, making sure that we're going to start off from scratch, putting inside our audio file.
02:00So as soon as our application loads, we are setting our application.
02:04We don't even need to make it out of play anymore. We have our sound file all set and configured.
02:09And literally, the next step is, let's start by creating accessible variables for all these different components.
02:17So to do that, I'm just going to copy all these components, and then I'm just going to delete them as I create their variables.
02:24Alright, so just to make sure that I have a reference, I'm going to create a new variable for toggle,
02:28and the variable for toggle will be, we're going to ask our document to get element by ID,
02:36and we're going to make sure that we sent in that toggle ID.
02:39And I'm going to literally just repeat this process and just name our files basically the same thing as their IDs.
02:47So like that, we could do this quickly. So we'll have our seeker.
02:51We're going to get element by ID and get that seeker.
02:54We're going to do for the muter file, the muter ID, the exact same thing.
02:58And we're going to get the muter.
03:00We're going to have the volume control, and we will have a volume control variable as well.
03:06Alright, so now that we've done that, we could delete the HTML because that wouldn't be valid JavaScript.
03:10And now we have our files more easily accessible.
03:13Our sound file is already loaded.
03:15It's not going to be playing because we didn't set it up yet.
03:18We didn't configure a play or any play functionality,
03:21but because we're going to have a toggle and control for play and pause.
03:25So it makes sense that we might maybe wait it out and basically create that toggle
03:31and enable the user to start that control.
03:33So to do that, I'm going to go to my toggle,
03:36and I'm going to ask for my toggle to add an event to it.
03:39So I'm going to add event listener, and I'm going to ask for the event click.
03:45When click happens, I am going to call the function on toggle play state.
03:54Alright, because whenever someone clicks that, we want to toggle the play state.
03:58To do that, again, we're just going to create that function.
04:01It's going to have an event inside of it.
04:04And let's see what we do inside of this event.
04:06So once our toggle state changes, once we get to that scenario where we have a play,
04:11once we get to a position where we clicked on that on click event
04:15and that we're ready to that scenario where we want to start that toggle,
04:19the big question that we have left is to ask basically what is our current state right now.
04:24And to find that out, we'll use our console.log
04:27and we could even ask directly our sound file, is it paused?
04:32Now if it is paused, it will return true.
04:35So we're going to go back into our browser just so we could see this live.
04:38Click on refresh, and we can see that we have an error, failed to load resource.
04:43If we look at the file, we'll see that we're having a problem loading our JS file.
04:47Control, okay, it was supposed to be controls, not control, I think.
04:52Yep, it's controls.
04:54So I'm going to go right into our HTML, update our JavaScript to be controls.
04:59We could save that, go back to our browser, and that should solve that bug.
05:03Now that we click on refresh, we're in a better shape.
05:06We can see our pause and play state.
05:08If I click on the place, our video should, our sound file should start playing.
05:13Oh, actually, we had the trace, right? I completely forgot about it.
05:17So first of all, we could see that we are paused.
05:19So we're staying paused.
05:21So actually, the next step for us is to go back into our controls.
05:24And instead of just asking if it's paused,
05:27is basically to toggle our play and pause state
05:30based on if this value is coming back as paused.
05:32So we could do an if statement.
05:35Basically, if it is paused, then we're going to do one thing.
05:38Otherwise, we're going to do the other thing.
05:40What do we want to do when we're paused?
05:42We want to go to our sound file and we want to play it.
05:45What do we want to do when we are not paused?
05:47We do want to pause.
05:49So we'll set the play, we'll set the pause.
05:51Once we set that in action, now when we go to our application,
05:54click on refresh when we play.
05:57Zero to geek.
05:59And when we pause, it will stop.
06:01Perfect. So now that we got that out of the way
06:03and we got our toggle controller working,
06:06our next step literally is to go for our muter.
06:09So let's go back into our application.
06:11And again, we're going to add the functionality for the muter.
06:14So I'm going to go in line 14.
06:16And I'm just going to copy the same function
06:18that we have here in line 13
06:20because it's really the same functionality.
06:22But I'm going to switch it to our muter.
06:24We'll do the seeker momentarily.
06:27So once the muter has been clicked on mute,
06:31on toggle mute state.
06:34There we go.
06:35So at least we have a consistent functionality
06:38which means that we could copy our function
06:41and we could just rename our function to be on mute state.
06:44And all that's left is to figure out
06:46what needs to be changed inside of the function itself
06:48to get it to work.
06:50And literally the next step is exactly that,
06:52is finding that mute functionality
06:54similar to the one that we did previously.
06:57And really what we want to do
06:59is we just want to toggle the mute state.
07:01And actually, toggling the mute state is much easier
07:03because the mute state itself,
07:05we don't even need an if statement.
07:07All we need to do is approach our sound file
07:09and basically take our muted position
07:11and switch whatever it is.
07:13So I could set my muted to be the opposite of what it is.
07:16It's a boolean value.
07:17So by setting it to be not
07:19and then setting the actual value,
07:21basically if it was true, it will turn into false.
07:23If it was false, it will turn into true.
07:25And we're dynamically automatically switching
07:27the state between the two options.
07:29So we've created the mute
07:31or all that's left for us is to test it out.
07:33So if I refresh Safari again,
07:35click on my play button
07:37and then you go for the mute button,
07:39we shouldn't hear anything,
07:40but if I click on the mute again,
07:43learning better is better.
07:45Alright, so we got our mute controller
07:47down and working, which is fantastic.
07:49That's one thing out of our way.
07:52Let's see what's the next thing that we want to approach.
07:54So we'll go back into our editor again.
07:57And we already approach and we already work through
08:00the toggle and the muter.
08:03So before we go for the secret,
08:04let me organize it based on what we're going to approach first.
08:07Let's move on to the volume controller.
08:10With the volume controller,
08:11although we're doing an event as well,
08:13we're not going to do a click event
08:15because our volume controller is not a button.
08:18Our volume controller is a number controller.
08:21To be able to control our number controller,
08:23we want to listen to a change instead of a click
08:26any time the value changes inside of it.
08:29So to do that, first of all,
08:30let me just update it to be volume control as our button
08:34and we want to change this text to be maybe on volume change.
08:41And again, I'm just going to copy a function
08:43to make sure that we could do this a little bit faster
08:46and I'm going to change the copy to be on volume change.
08:51And all that's left is to figure out what would go
08:53inside of the on volume change.
08:55Now, very similarly to the way that we've did it,
08:58we've done it again in the video course.
09:01If you've been and you've checked our video course,
09:03we're going to basically stick to what we've done previously.
09:07We're going to run through this really quickly.
09:09And now if you want to see it and practice it,
09:12go through our video course.
09:14So basically we're going to set our sound.
09:16And what we want to do is we want to change its volume.
09:18And literally all we have to do is change the value of our volume
09:22based on the value that comes back from our component.
09:25Now the component itself,
09:27we could access it by going to our e.target,
09:30our event.target, not target.
09:33Once we get to our event.target,
09:35aka once we get into our volume controller,
09:38we could literally just ask for the value that's returned.
09:43Now the value that's returned is going to be between 0 and 100.
09:46But actually the value that we actually want,
09:51we want a value between 0 and 1 for our volume.
09:54So to make sure that we get a value between 0 and 1,
09:57I'm just going to divide it by 100,
09:59making sure that the value that's being returned
10:01is between 0 and 100.
10:03So by saving this, if I go back into our application
10:06and I click on refresh,
10:08and this time around I'm going to play,
10:10and if I lower my volume and lower it,
10:12we won't hear anything.
10:14And if we go all the way back up, we'll hear everything.
10:16Learning better is better.
10:18Perfect.
10:19So we got out of the way the play pause,
10:21we got out of the way the mute,
10:22we got out of the way the volume control.
10:25All that's left is for us to make sure that we have our controller,
10:28our seeker actually capable of jumping around inside of our sound.
10:34So let's see how that would work.
10:36So first of all, again, we're going to go back again
10:38into our actual, into our seeker itself,
10:42and what we want to do in our seeker is we want to add another event,
10:45and the event is going to be the same exact event.
10:48So the event that we want to listen to is the change event.
10:52And actually, in our case in Zara, by the way,
10:54I didn't change our volume control to be a change,
10:56and I left it to click,
10:58because by the way, I was clicking and changing.
11:00But actually, this wouldn't work if I would go to my application
11:03and click on refresh and just come here and type 0
11:07and click on my play.
11:10You see that my volume didn't go down.
11:12Only once I clicked on it, did it work.
11:14So even though I set here to 100 and I click out of it,
11:16nothing happened, but only when I click again,
11:18the volume would go up again.
11:20So obviously, that's a problem that we want to solve,
11:23and the way we would solve that was just because
11:25I forgot to set the value here to be change.
11:27If I would set it to be change instead of click,
11:30any time I change my copy,
11:32any time the value has changed even without a click,
11:35even if it was a tab, so if we play here,
11:37and I set the value to 0 and I click out of the component,
11:40it would update the volume.
11:42Same is true if I click to 100.
11:44Learning better is better.
11:45All right, so that change is actually really important,
11:47although the click might work,
11:49but it's not going to be 100% functional and working.
11:52So let's do our secret now.
11:54The next step is the secret, so again,
11:55I'm just going to copy the same change event
11:57that that's what we originally wanted to do anyways.
12:00And I'm going to change on seek.
12:04I'm going to call it on seek,
12:06and we were going to create the on seek function,
12:08so I'm just going to copy that function
12:11and just call it on seek.
12:13Let me scroll a little bit up because we're starting
12:17to have a lot of stuff going on here.
12:19We could delete anything that's inside of our on seek,
12:21and all that's really left for us is to actually
12:24figure out where do we want to go
12:26currently inside of our sound.
12:28So actually we could go into our sound
12:30and basically update the current time,
12:32and we could update that current time again
12:34based on the target.value.
12:38We divide that by 100 because again,
12:40the value that we're going to be getting
12:42is going to be between 0 and 100.
12:45So now that we got a value between 0 and 100,
12:48all that's left for us to do is to duplicate
12:50that value by the actual length of our sound file.
12:54To do that, we could just approach our sound
12:56and ask for its duration.
12:59And that's all we had to do to get our sound
13:01to update automatically when we click on the seek.
13:05So I'm going to go back into our application,
13:07into Safari, click on refresh, play,
13:11and now I could jump around at any point
13:13in our sound itself and hear the sound
13:15from any point that I want.
13:18Now with that said, before we complete this section,
13:23obviously this is not 100% functional
13:25because one of the stuff that we learned
13:27inside of the video course is about
13:29the callback events that come back from our actual videos.
13:33Now we haven't approached and worked
13:37on this functionality inside of this element
13:39because every single one of our controllers,
13:41we probably would want to change them
13:43based on whatever changes that are happening
13:45inside of our sound file.
13:47But in our case, all we're going to do
13:49is just for the sake of this example
13:51to show you how it would be done,
13:53we're going to add one more event,
13:55but this time we're going to add our event
13:57directly to our sound file,
13:59and I'm going to add another event listener.
14:01This event listener, this time around,
14:03is going to be duration change.
14:06Well, actually not duration change, which we could,
14:08but duration change will let us know whenever
14:10the sound file duration changes,
14:12but we actually want to listen to update time.
14:16On update, what we want to do whenever the time is updated,
14:20basically when the sound is changing
14:23and it's not saying at the same position,
14:25what we want to do is we want to update our Seeker,
14:28basically the exact opposite of what we've done so far.
14:32What we want to do is whenever our sound is changing
14:35and updating, we want to update our Seeker
14:37to reflect the new position that it needs to be in.
14:42To do that, we're going to literally,
14:44all we got to do is again, we want to approach our Seeker,
14:47and this time around we want to change
14:49the value of the Seeker to be whatever
14:51the real value should be right now.
14:55How do we know the value?
14:56Again, we're going to approach the sound
14:58in just the same way as we asked for the duration
15:00and we asked for the current time to set it,
15:03this time around we want to take the current time
15:05divided by the duration and then take that value
15:09that's going to be between 0 and 1 now,
15:11which is going to tell us where along the video it is,
15:13duplicated by 100.
15:15Now the reason is, again, is because we set our application
15:18to be between 0 and 1 for our volume control
15:21and also for our Seeker.
15:23So because of that, we want to make sure
15:25that we duplicated by 100 so the value
15:27between 0 and 1 would actually become
15:29between 0 and 100.
15:31Now that we've done that and we go into our application
15:33again and click on Refresh,
15:35so first of all we have an error in line 21,
15:37so let's go right ahead to line 21
15:39and fix the, and we have there a U
15:42that doesn't need to be there,
15:44so let's go back into our browser,
15:46click on Refresh, and this time around
15:48when we click on Play, notice how the Seeker
15:50is moving along as the music is playing.
15:53We can also move our sound and hear it again and again
15:56as many times as we want to,
15:58or we could just let it play out.
16:00Alright, so that's learning better.
16:03Really, really done quickly what we've done
16:05in the video player really broken down
16:07into quite a few segments.
16:09Now to be able to create the fully customized
16:11sound player, I recommend that you go back
16:13into the video player course where we actually,
16:15everything that we learned about controlling
16:17a video player is 100% valid for a sound player as well,
16:21but this course is going to be more advanced
16:23and we're going to step into new topics
16:25that we didn't cover yet for the video player.

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!