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

Changing HTML5 Video volume dynamically

in the last video you discovered how to mute my videos. Getting real here... why would you ever want to mute my videos... exactly what you really want to do is put up the volume. Thanks, very kind of you for wanting to know how to do that. In this video you will learn exactly that and more. By the end of this video you will know how to change the volume dynamically and know how to work with the new range input type in HTML5.

Video Transcript

00:00Alright, so it's time for us to start playing with volume. Let's jump right in and as we do that
00:03We're gonna learn how to use a new HTML 5
00:07Attribute or element on the stage. So I'm gonna go right into my HTML and I'm just gonna add a new input element
00:15This input element is going to be a numeric stepper and the way we define that is by setting the type to be number
00:21Now this doesn't work 100% on all browsers currently in the year
00:262013 but if it is adopted completely and finalized by the HTML 5
00:32Consortium or whatever the word is then it will be so let's see how it works
00:36And we won't worry about the fact that this is not going to work in every single browser right now
00:41We're gonna set the minimum and max value to be between 0 and 100 even though the volume is between 0 and 1
00:48It's just a more easily more human friendly kind of numeric system
00:52It's cleaner for a user if we're really gonna show this to use our little we're kind of experimenting here the value
00:59We're gonna set the default value to 80 which is basically 80% of the sound and the last but not least thing is we're gonna set
01:05A step size instead of it being 1 by default. We're gonna set it to be 5
01:09So now that we created the numeric stepper if we jump right into Safari
01:13We'll see that we'll have a nice little numeric stepper waiting here for us to start playing with it at set at 80%
01:19Although currently we did not define the volume. Yeah
01:23The next step after that is we want to go well before we go with the next step
01:27Let me show you what happens in Firefox if you do this in Firefox currently
01:31You'll see that the numeric stepper doesn't even work
01:34So we're not going to worry about Firefox
01:36We're only going to work in Safari for this component as we're working on it with that said let's jump right into our code
01:42And continue into the JavaScript section, but before we do that we're going to give our element an ID
01:47So I'm just going to call it
01:49vol control
01:51For volume control I'm going to save that go right into our JavaScript and literally
01:56I'm just going to copy our mutor functionality and just change the actual event information
02:01So I'm going to paste right inside of our ID just put the new ID of our volume control
02:07We're going to change a click because we don't care about clicking we care about changes
02:11So we're going to hear the change event and on change we want to call maybe the on
02:17volume change
02:18What do you want to do when the volume is changing?
02:21All right, so let's let's create a function for that
02:24I'm just going to find a place for it so function on volume change. It's an event again
02:28So we're going to send it the event object on volume change the first thing that we want to do is we want to
02:33Fetch the volume that got back from the event now the way to do that is very very simple
02:38So I'm going to console that log. I'm going to go to my
02:42Event target which will bring me the object that was clicked on and I'm going to ask for that numeric stepper the value of that
02:49Numeric stepper that's if we go into our browser again and click on refresh every time we click on the numeric stepper
02:56So first of all we have your little error
02:58So it's actually a typing error and I could see because I wrote here target instead of target
03:03So I'm just going to go right ahead and fix that target
03:07There we go and back in to see how it works
03:11And I'm going to click on down and we could see that the numbers are updating perfectly exactly the way we wanted them to as you
03:18Recall we said that the volume value in HTML 5 video component is actually between 0 and 1
03:24So I'm going to go to my vid dot
03:27Volume, I'm going to set the value to be basically with a value that we got back divided by 100
03:33Making sure that the value is between 0 and 100
03:37Now if we play our application if we play our video
03:41I'm just going to play around with the sound quietly so you could see that it works
03:56Perfect so we could see that the volume is working
03:58But obviously there's a few nuances here that we probably would want to solve for one the default in our
04:04Element is 80%
04:05But we're not setting the same
04:07Default to our actual player because our player has whatever of default value it has so we probably want to
04:14Basically set our videos value in advance basically going to our video volume and just starting by default at 80% at 0.8
04:23Alright, so now we got our beginning in the right position
04:25But how about what happens when we actually reach 0% wouldn't it be nice if our mute button would then become?
04:32Its copy would then change to be unmuted, so let's see how we would address that type of issue so on the volume change
04:40Let's ask if video volume
04:42Equals 0 in that case. Let's just do a console here log
04:48There is no sound now
04:51We're going to save that and when there is no sound we're going to just go
04:55But let's just make sure that the console log does work
04:58So we're going to go into Safari again click on refresh
05:00And we're just going to go all the way down to 0 and we're going to see if our
05:05Volume changes and one thing is that we're seeing is that we're never getting that value
05:10So that is obviously a problem so that before we could solve this problem
05:14let's take one step back and maybe instead of that we could go to the value itself and
05:19Trigger the value itself, so let's check that with the target. Oh well
05:22I already found the problem is I
05:25Added a value instead of making a double equation, so I basically automatically made it 0 so that was a big no
05:30No on my end so back into the browser. We're going to click on refresh and
05:35This time around we're going to go down to 0 we should be getting that message telling us that there is no sound now perfect
05:42So now that there is no sound and probably a good idea to change the copy in our mute button
05:46But wouldn't it be even a better idea to actually mute the button like that in case the user goes and clicks on the
05:53Mute button again. It actually would really be muted, and it won't be just changing the button
05:57So our next step would be is if our video is muted
06:01We probably would want to actually if our volume is down we probably would want to make our button muted and
06:07On the other hand we would want to reverse it so basically in this case
06:10We want to actually force a mute if the volume is zero, so I'm going to set here a false value mute
06:17Which would be true?
06:19And I want to force the same way around into our other way around if we are going up, and it's not zero
06:28So if it's not zero we're not muted if it is then we are now we could grab this HTML text again
06:34If we want and put it right underneath and basically update the information only this time
06:39We won't be able to use this that inner HTML
06:41We'll have to actually grab the actual muter and approach it directly because we never saved our
06:47Muter object, and we don't have direct access to it
06:51So if I save this information just walking you through this one more time if our value if our volume is zero
06:56We're forcing a mute if our volume is higher than zero that we are unmuting not only we're doing that
07:03We're also updating the text to reflect the changes that we were doing so let's see this in action
07:08And just make sure I saved it and let's see this in action
07:12So we're gonna play our video
07:14And we're gonna go right ahead to go to take our volume all the way down to zero and you can see that now
07:19We're we are muted and we could click up and maybe put your value of one
07:24but and we would be unmuted now the only problem is is because our
07:29Well the biggest problem that we have right now
07:31Is literally that our mute button ceases to work once we're at zero because if we're at zero and someone clicks on the unmute
07:38It has no actual effect
07:41So what we want to do is add another extra layer of logic and the next layer is really going into our mute button where we
07:48Want to check if a button was just unmuted, but the volume is zero to push up the volume
07:54Maybe back to that default value of 80% so to do that what I'm gonna do is I'm again
07:59I'm gonna go into my button right underneath my mute toggle area, and I'm just gonna ask I'm gonna ask if
08:05video dot muted is
08:07Not true. Well. We're not muted and
08:12Our volume
08:15Equals
08:17zero if
08:19That is the case and we want to make our video volume
08:23Maybe go up to be zero dot eight and we probably also want to update the numeric stepper to reflect this change
08:31So I'm just gonna update that close that and go to our numeric stepper and again
08:38We're gonna have to access it directly because we didn't save a reference to it
08:41and we're gonna go right on the bottom here and
08:45Basically set its value to be 80
08:48So we're basically undoing our muting process and let's see this in action
08:52So basically if I save this and we go into our browser and I'm gonna just click on the play and you'll see this
08:57In action, so if we're playing and I reduce the volume all the way down to zero
09:02And then I click on the unmute button
09:04It will automatically take me back to 80 making sure that our mute button and our numeric stepper are
09:11Continuously working and not not having any issues. So that's cool
09:14So we just solved all the technical issues with sound both and having a volume control and a mute controller at the same time
09:22Now obviously in a real world scenario, you probably would have different graphics
09:26But this core functionality that we just worked on and created a full holistic mute unmute volume controller is
09:34Golden and you could use this logic with whatever real video player skin that you would create
09:39All right. So in the next video, I just want to wrap up this topic with the fun little transitional
09:46video volume
09:48Animator and we'll see that in a next video

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!