Muting HTML5 Video API with JavaScript
Stop talking ben... well if you really want to make me stop you need to know how to click on that mute button or even better how about I teach you how to use the JavaScript video API to mute the volume when ever you feel like it.
Video Transcript
00:00All right, so while you were gone, and let me go back in Safari, which is our main browser
00:04I've updated the video to actually have a video with sound so let's hear it just for a second
00:09So we'll see what we're working with. It's our jingle
00:14All right, so we have a video that has sound now and I've updated all the videos you could download them again in our source
00:20Files that are freely available to our zero to geek comm members
00:24All right, the next step is I want to add a dimmer. Excuse me already have a dimmer
00:30We want to add in a mute button. So let's jump right into it and see how do we add a mute button?
00:34It's quite fun. Very easy. So well because it's easy
00:37We're gonna learn a couple of new stuff and a reminder whoever didn't take our JavaScript basics courses
00:43Then this is gonna be a could be a little bit more difficult
00:46And again, I do encourage you to take our course is about JavaScript before you take this course
00:51All right. So I just want to give I just paste it a new button and I'm just gonna call it muter and
00:57I'm gonna call I'm gonna give it a name here of mute the label of mute
01:01I'm gonna save that we could check in our browser, but no need so let's just go right ahead into our
01:08JavaScript and basically trigger an event that would happen whenever the muter is called
01:13So whenever the muter is called what I want to do is I'm just gonna grab the same control that we had for our dimmer
01:19But this time around I'm gonna add a real click event and not a call back
01:23So to add the click event
01:24I'm just gonna delete from the point that we got the dimmer itself and I'm gonna change the copy here to be muter and
01:30With the idea of muter or the document element ID muter
01:33I'm gonna add an add event listener and the vet add event listener works is very similar to how
01:40On click would work only the way it works is that we set it at actually a string that's we say to it
01:46We want to get a click in the same way that we've done with the play and the pause and again
01:51If you don't really know what events are I strongly encourage you to go to our earlier courses on JavaScript in general because they're gonna
01:57Give you the foundation that you need to really really excel in
02:01development
02:02So on click what do we want to do? We want to call a function. We're gonna call it on mute
02:08Toggle because we want to toggle our mute button whenever it is being called
02:14So I'm just gonna scroll a little bit down and go close to our toggle video because it's a very similar
02:19Functionality and I'm just gonna create a new on mute toggle
02:23Function now in this function, which is an event before we do anything. I want to talk first about basically
02:30How do you do we need to now go through and write document element ID and toggle and so on so forth?
02:36Or could we do this in a relative way?
02:39Just the way that we set in our toggle video in a very relative way where we spoke directly to the buttons that were clicked on
02:45That's the exact same mythology and the exact same action that we want to do when we're working with the toggle button
02:52Now to make this work and to make it happen
02:55What we want to do is basically first figure out are we really approaching the button and maybe you don't know
03:01So to figure it out your best bet is always to ask your console
03:04So don't you know don't even continue working before you ask your console. What is this?
03:10Now if you're this is your button then you're set and I'm gonna also trace out the actual event itself
03:15Just so we could see how the two ways we could approach us or by working with the object directly if we're getting it
03:21And are this and if not getting it through our event because the event object itself has a property that's called target
03:28That gives us basically what was clicked on and we're gonna see that right away
03:33So I'm going to save this
03:34I'm gonna save my HTML and we're gonna go right into the HTML into our browser click on refresh
03:40And I'm gonna go right ahead into the mute button and click on it as soon as I click on it
03:45You'll see that first of all we get our button immediately
03:47That's our this and we get a mouse event in the mouse event properties
03:52Some of the properties are included here and one of the most important ones that you'll probably be working with the most is
03:58Our target, so let's find our target and here go with target
04:02Which is our HTML button element, which is exactly what we pressed on
04:05so there's basically two ways for you to access your button or through to this if you have access to it or
04:12Through the target itself which will give you the target that was clicked on
04:15All right, and that's all you have to know really about the propagation of events at this stage
04:20We have a whole course about event propagation on zero to geek calm
04:23Which is really really really advanced and very very
04:27Useful for you if you want to get deeper into the world of really really understanding how to code really well
04:33All right now that we got that out of the way and we know how to approach our object
04:36Let's get back into our application and figure out what we want to do next
04:40So first of all what I want to do is I want to change
04:43Immediately the first thing that I want to do is basically I want to ask if our video is muted
04:49And that's basically how I ask it is is our video muted. I'm asking
04:53If our video is muted, then I'll do one action and if not, I'll do another action
04:59So the first action if our video is muted then we want to unmute it
05:02So I'm gonna click your video that muted equals false if our video wasn't muted
05:08we want to make it muted and if we log back right back into our browser and
05:16refresh and play our video and
05:19And click on our mute button our music will stop but the video will continue we'll click on the mute button again
05:27And we could see that the mute button is working and functional only problem
05:31We have right now is the mute word itself is not changing
05:33So we want to change it from mute to unmute whenever the mute changes
05:37So we'll go right in and immediately do it and now that we know that we could access our element just by typing this
05:43We could type this dot inner
05:45HTML and set the inner HTML whenever it's not muted or in this case when it's not muted then we want to set it to mute and
05:54Whenever the meat whenever the video is muted then we will want to unmute it
06:01Now obviously well elementary that you could as well create different images and swap the images
06:07Based on if it's muted or if it's not muted
06:10So let me just say that go to my browser and again
06:13just look at it one more time and refresh and make sure that when I click on my mute button and
06:18When it's muted if I want to unmute the copy is now
06:22Talking exactly about what we wanted to talk about and we're extremely happy now
06:27This really covers the mute and unmute, but I really wanted to show you just another way of basically
06:32Coding this because this is very long for something that is relatively simple
06:37And I want to show you just another way of how you could write all this code basically in two lines instead of or three
06:42Lines instead of six lines now the way you would do this
06:45And I'm just gonna comment this out just in case you want to look at it and refer to it
06:49I want to show you how you would do this in the cleaner or faster way
06:53So first of all I would go to my vid dot muted and because I don't really care if it's muted or not
06:59I just want to toggle whatever it was
07:01I could literally just go to my muted button and say from now on you're not muted so I could go to my video
07:07And say you're not muted
07:09Once I've done that and that's it my video now is unmuted so literally just by
07:15By putting these this one line we avoided the need for it if else in the mute process
07:23All that's left is to figure out how to change the copy now to change a copy
07:27We could do something very similar and very smart. I could say is go and approach the inner HTML immediately and
07:35Create something that's called a
07:37Ternary operator and it's a ternary if statement that basically works in this way you put your your
07:43Condition and my condition is if it's muted so and it's as it's a Boolean value
07:48The value that's going to come back here is or true or false if the value is true
07:53We're going to do one thing if the value is false
07:56we're going to do another thing and that's basically the structure of a condensed if statement where if
08:03The value is right
08:04Versus the value is not so if the value was right what I want to do is if we are muted
08:10Then I want to put the word on mute
08:14So I'm just going to put here the word on well if it is muted
08:18I don't want to do anything I want to just take this and just add regular all this together
08:24This doesn't really change and I want to add the word mute at the end of it
08:29So no matter what we're adding mute into our text if we are muted
08:34We're adding the words on and if we aren't we're not all right, so let's see this in action
08:39See if this actually does work, so we'll go back into our browser click on refresh
08:43And this time around when we're playing our video when we click on the mute button
08:48We'll see that now it's on mute and back to mute mute and unmute and our functionality is exactly the same as it was before
08:55But this time around we wrote it literally in two lines of code instead of writing it in six now
09:00Don't worry if this is a little bit too sophisticated
09:03And you're encouraged to use the other more detailed way if this is a little bit too hard for you
09:08but I just wanted to
09:10Kind of like encourage you to explore avenues and explore ways of writing more
09:14Condensed code because condensed code and many times is much more effective and in this case in this era
09:20It once you get used to it. It's also much more legible
09:23All right, so that's it for this one and let's move on to the next topic
