Viewers: in countries Watching now:
So far we've been working with events and dom elements that are pretty closely related to each other. So for example images, and maybe if you clicked the image you'd get a larger version of the image, those things are pretty closely related. So in the next series of movies, I'm going to be showing you how to build an audio jukebox. This is a type of project that has different kinds of events and elements, so. You'll be clicking on a list item, and that's actually going to generate an audio object. Now media elements are super easy to do with HTML5. All you really have to do to play a sound on your page is just create an audio element with a source and maybe some controls.
So that's why I added this tag right here. Now the CSS is also pretty mundane. So if you look down here at the bottom of the css you'll see that there are different states for showing how the different list items should look when they're being played or a song has been paused. Or while the sound is playing, so that's what all these styles are for down here. So let's go ahead and start coding. We'll go into our script file, which right now it's empty. And the first thing we're going to do, as usual, is create a variable.
And we'll call this one jukebox. And it's going to point to our UL with a class of player. So, document.queryselector. And it's going to call ul.player. Next we're going to add an event listener to the jukebox, so we'll say, jukebox.addEventListener. And what we want to check for is a click event when somebody clicks on one of the list items. And then we want to execute a function, pass it along an event and we want to make sure we also pass here the value of false so that it bubbles properly.
So, next we want to create the audio player which is going to be just an audio tag. So, we're going to say variable audio player and that's going to be equal to document and we're going to create this element, and it's going to be an audio tag. Next, we're going to change the ID of the audio element, this is going to be important later on, not right now, it's not really going to make a big difference. But it will be nice to be able to access this element later if we attach an ID element to it. So we're going to say.
Audio player.id, it's going to be player. So my audio player is going to have an ID of player. And then we're going to append this. So, document, we'll attach it to the body. So this is something that you haven't seen before. We're attaching the element, not to a previously existing element, like an overlay. Or not to, say the list item. We're just attaching it to the body. We don't really need it to be related to our list element but we do need it somewhere on the page. I don't want to put it right into a list element because we could be clicking on any one of these songs and I don't want the auto player to be on the first list item I click on.
So, nothing is happening right now, hm, let's take a look at the elements. You can see that my audio players are being generated but no sound is playing. And now that's because we've generated the audio player, and we've appended it. We haven't really told it what to play, so I need to say something like, audio player, and then set the location of the auto player to be. I'm going to set it to something called song name, and I need to create this variable, so I'll do it right up here.
Variable song name. Is to be equal to, the target of what I click on. This time I'm going to use something called get attribute, and I'm going to find that attribute data that SRC and that's going to set our song name, and then we set the location of the sound to the song names. Let's go and save that and let's see whats happening now. Refresh, lets go and press the escape key in our developer tools. To get the console and the elements panel at the same time, and I'm going to click on this and see what's happening, well we're getting the audio with the right id and the right source location.
But it's not playing, and that's because to play a sound, you actually have to execute the play method of the sound. So to do that once I append the child I'm going to say audioPlayer and then execute the play command. And then I'm going to refresh this page. And I'll play this one because I know it's short. (MUSIC) If you get too tired of it just hit refresh and it'll stop playing the song for right now. This is one of the problems with this code right now it doesn't let you stop any of these.
(SOUND) And you can play more than one of them in a row. So we'll need to fix that eventually. But let me talk a little bit about what we did here. Now normally you can access any element or any property of an object by just using dot notations. So you could say something like e.target.src, but the data source attribute as we mentioned before is not a standard attribute. So we can't just say e.target. data.source, It doesn't really exist. And so to get to that attribute we have to use this notation.
Which is e.target and then get an attribute. You can also set an attribute if you want to. But this works pretty well. And the only thing you have to do is remember that you have to play this. So really playing a song isn't too hard. We can create a play event using the play method. And unfortunately this is going to make the song play at the same time as the other songs as you click on another element. And when a song stops playing, our player's not going to update. So, if we go over here and we play this song again. This is the shortest one. (MUSIC).
So, note that the audioplayer is still there, and it should really be gone. Another thing that we're not doing is identifying which song we're currently playing. So let's see, if you look at this is the song that is currently playing, so I should set the ID of this element to something like playing that will attach the proper style. Let's see. We need to do that. So actually we set the ID of the song to player, but we haven't set the ID of the element that we clicked on. So we should do that as well. So e.target.id.
(MUSIC) Alright, so when the song is over, the problem is it's not unplaying or unsetting this item is being played, and we still have the audio element here. This should be gone because the song is no longer playing. And if you open this up, you can see that we've got the ID right here on this element. This ID should be gone. Those are things we'll start to worry about in the next movie.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.