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

Creating the Audio object dynamically

Until now we created audio directly through our HTML5 new tags. In this video we will learn how to create the same audio object only this time totally dynamically via JavaScript.

Video Transcript

00:00Welcome back, and it's time for us to see how could we do this in JavaScript and how could we create dynamically
00:06Music without using HTML directly, but using the JavaScript API to do that
00:11I'm gonna start ahead and I'm gonna just delete our audio area
00:14And we're actually gonna see this done in two different ways
00:17The first one will be in this video and the second way will be in the next video. How's about that?
00:22How about that?
00:24All right
00:24So the first thing I want to do is I want to set myself a div and I'm just gonna give it some sort of
00:29ID because that's where I want my sound to be contained. So I'm gonna create a new div
00:34I'm just gonna call it sound zone
00:36That's where I'm gonna keep my sound file and next at the end of my document
00:40I'm gonna create a new script tag or we could create obviously a script tag in a separate file
00:45But for our case because we're not gonna have that much code going on
00:47I'm just gonna put it right ahead in our document
00:51Alright, so the first step now that we know what we're in the line 11 then that means our document is loaded really
00:57So what I want to do is I'm gonna create a new variable. That's just gonna fetch that sound zone
01:01I'll call that file sound zone as well, and I'm just gonna get my document
01:08Get Ella lament by ID and I'm gonna grab our sound zone
01:14There we go. So now we have our sound zone. Our next stage is going to be to create a new audio
01:20Tag now that we could create a new audio tag
01:23Just the same way as create any other thing in HTML and the way we would do that
01:27By the way, the same way you could do this also with the video tag
01:30So I'm just gonna create a new sound element and the new sound element
01:34I'm just gonna approach my document and I'm gonna ask to create an element
01:39Then I'm gonna specify what element I want to create and I actually want to create the audio element
01:45Once I've created the audio element, that's it now. I have a new sound element
01:49Now the only thing I really want to do is I want to make sure that my sound is
01:53Automatically playing as soon as we add our sound into it. So I'm going to set a new attribute
02:00Set let me at
02:03Rebute and
02:05I'm going to set that out attribute to auto play and
02:11I'm going to set the value itself to be auto play as well
02:15Basically making sure that whatever sound I put into my file will automatically start playing
02:20All right
02:21So now our problem is we don't know we have two different file formats and instead of building out the full
02:27Different types of tracks we know that we talked earlier that currently there are two different file formats that are popular the mp3
02:34Which is a mp4 format and we have the og format
02:38So how about we'll build out our file and then dynamically append the actual value the actual
02:45Extension based on if that capability is supported and or not lucky for us
02:49It's quite easy for us to get this information
02:52Let's see how it's done. So first of all, I'm going to create the our sound path
02:56Variable and I'm going to first start putting inside of it the actual path of our file
03:02Which is at zero to geek zero one dot mp3, right?
03:06But I'm just going to delete the mp3 part because we're going to dynamically change it between the mp3 and the og
03:12So now that we have our basic sound file path
03:15Our next stage is actually to go to our sound file and actually ask it is the sound supported
03:21The way we do that is very easily
03:23So I'm going to ask inside of an if statement if our sound can play type
03:29And I'll send inside of it the type that I want to test out
03:32So I want to test out an audio file and an mp4 now
03:37I could have put also the full codec
03:38But I'm not because if our browser supports mp4
03:41We're we have a good chance that it's going to support all the various types of mp3
03:46Files that are out there especially because our default mp3 converters will usually use a common denominator mp3 converter
03:54Although if you're worried about it, you could always put inside the actual exact information
03:59alright, so for
04:00When would he ask if the type that comes back then information that will return and before we even do this if statement
04:07How about we use our console dot log and we'll just trace out the information now three possible values could return one is probably
04:17Another is maybe and the third option is just an empty string
04:21So let's see how it looks like in a browser and if I click on refresh
04:24We'll see in our console that we're getting the result
04:27Maybe now the reason why we're getting the result maybe is because we didn't specify the exact codec
04:32But we really are not worried about it for us if we get maybe or probably
04:37If we get any result that's not an empty string, then we're happy now to make sure that we'll get our results back
04:44So well, basically what I'm gonna do in my if statement is
04:47I'm just gonna make sure that I'm gonna ask as long as the value that is returning does not equal to an empty string
04:54Then we're good. If we're not if our value is not does not equal an empty string
05:00Then we're gonna append into our sound path
05:03We're gonna append the value of that mp3
05:08On the other hand if it's not an amp if we don't support mp4 codec if we don't support that capability
05:15Then I'm gonna put inside of our sound file the actual file formats
05:19If we go into ours into our source file here for a second
05:22We'll see that we have this long extension
05:23We could rename it if we want to or we could just grab that whole long name as I'm doing right here and just add it
05:29Inside basically making sure that our sound path will actually change its name
05:34It will change its value based on these this information now
05:38It's probably not right to of me that I set it as an uppercase because uppercase means usually
05:43It's a way for us to indicate that this variable definition will never change now in our in our case
05:49We're actually we are changing it
05:51So probably a better idea would be to set it to be as sound path as a lower case and not an uppercase
05:57Variable name and this is really just a semantic to make sure that anytime you see an uppercase
06:02We're really never gonna change it
06:04Alright, so now that we set our values, and we've done it dynamically now by the way
06:09An even better solution would be maybe keeping it as an uppercase
06:13But then solving this problem automatically within the same lines instead of doing the separate if statement
06:20I could just grab this whole if statement and basically condense it into into basically one line
06:26I could basically add and let me put this in this nice and not a noticeable way
06:31So I'm basically gonna ask if that is true
06:34Well, then we're gonna add mp3 if it's not true, then we're gonna add the other variable
06:40So this is again a nice short way of creating if statements
06:43And this statement is exactly the same as this as this little as this full if statement
06:48So this full if is basically on this question mark really and this is the if area and this is the else area
06:55So if our value comes back, and it's not and not empty then we're gonna set it to be mp3
07:01Well, if not, we're gonna set it to be og
07:04Information and that will be appended into our file format
07:07So we don't need this anymore
07:08But I'll leave it there in case you want to compare and see the and match between the two
07:13The next step for us is to go to our actual to our sound and add a new attribute again
07:17And this time around we want to add the source attribute once we add the source attribute
07:23We're ready to literally send our sound file path into it setting it
07:28Configuring it and last but not least all that's left for us to do is to go to our sound file again
07:33Actually to go to our sound zone div layer that we've created earlier and append
07:40Child making sure we're adding inside of it our sound file once we've done that let's just go through this really quickly again
07:47We've we've basically set a variable so we could get access to our sound zone
07:52Then we created a new sound using the create element
07:56Selecting the audio element
07:58We then set an attribute of autoplay and defined its value to be autoplay as well
08:04We then created a sound path variable and in the sound path variable
08:08We appended the value of our extension based on if our sound can play the type of audio mp4
08:15Now we sort of the value that came back maybe because we're it's kind of like a future proof. Maybe there'll be an input peg
08:22Kodak that is not being supported, but because we're gonna always use a standard in coding. We should be okay
08:28We could always go into the nitty-gritty details, but it's not worth it
08:31So in this case if we do pass it
08:33We'll set it to mp3 and if we're not we're gonna assume that the value of og will work although the it might not
08:38But in our case, we know that the common browsers today do support these two options
08:43So either or one of these two should work. So now that we have our sound path and it's configured
08:48we've set our attribute source to be that sound path making sure we're updating that sound file to have that source value and
08:55Last of it not least we're adding it onto the stage appending it inside of our sound zone div
09:01Now that we've done that all that's left is to click on the refresh and see if it works
09:05Zero to geek and there we go. We have our jingle running in the background and we dynamically
09:12Learning better is better

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!