Ready to watch this entire course?
Become a member and get unlimited access to the entire skills library of over 4,900 courses, including more Developer and personalized recommendations.Start Your Free Trial Now
- View Offline
- Understanding event registration and propagation
- Analyzing event properties
- Creating and removing DOM elements with events
- Removing an event
- Canceling and preventing default events
- Playing media events
- Handling media pauses
- Dragging and dropping
- Adding touch events
Skill Level Intermediate
In the last movie, I showed you how to start to build a audio jukebox by showing you the different kinds of events and how to generate a play event for any one of these songs. So there's a couple problems with this. Number one, the UI is not updating when a song has finished playing. And you can also play more than one song at the same time. So let's go ahead and refresh. And I'll show you. You can play this one, and then I'll play this other one. (MUSIC) And as I mentioned, I'm just hitting Refresh to stop them right now. So we should not allow the scripting going on here, so we need to fix that.
Now if you look at this page, you can see that there's a lot of methods that we can call. So for example, there's a pause method. We've already used the play method and then there are several properties that we can look for, so for example, we could look for the pass property to see if a song has pause playing. We can also look for an ended property to see if a song is finished playing and the nice thing about media is that audio and video elements work almost exactly the same way. You call them exactly the same way. They have the same properties, the same methods.
That's why all these say audiovideo methods, and audio video properties, right here. Let's go take a look at how we would detect the ended event which fires when a song has finished playing and intigrate that into our application. After I create this element right here, I'm going to give it a few spaces. And I'm going to say audio player, and I'm going to add a new event listener here, addeventlistener, and this time I'm looking for the event ended. That fires whenever the song is finished playing. Then I'm going to execute a function, I don't really need to pass an event object here, but I do need to set this to False to make sure that it bubbles.
We'll need to put this right here. So now we need to remove the Audio Player from existence. So to do that, we'll say Audio Player, and remember when we remove a child we have to call the parent node. Of the element and then we call the Remove child function, and into this remove child function we pass what we want to remove. Which would be our audio player. So what that should do is get rid of the sound elements. So let's go back into our jukebox, and I'm going to pull up the.
Developer tools, and hit Esc to pull up the console. And let's go ahead and play this. This is the shortest song, so. (MUSIC) If you were looking at this area, you may have noticed that the audio tag Was automatically generated, and as soon as the song finished playing, the audio tag got deleted from the dom GUESS. So let's go ahead and refresh and do that again. And just take a look right here in between the body and the script tag, and you'll see it happen.
(MUSIC) Now, what's not happening, is visually, it looks like this song is still playing. That's because, in the unordered list, list item, we still have this element right here, with an ID of playing. So obviously, we need to tell it not to have any ID so that it looks like the other ones. So we'll need to do that next. All we gotta do for that is set the event target.
And then change the ID to nothing. So, let's go ahead and go back to our song, we'll refresh, and let's go ahead and play that again. You can take a look over here if you want. Actually I'm going to hit Esc and get rid of the console, I don't think I need it right now. So you can see the whole dong GUESS with everything expanded. And we'll play the song again. (MUSIC) Right, so now it looks normal and you can notice that the audio element is gone from right here.
But more importantly, we have reset the ID of this element so it looks like all the other elements and that's why you have to work with these type of event, these type of events generate their own object. And sometimes that object is not directly associated with the element that's started the event, in this case clicking on this image right here. You've got to fix a couple more things about this file. So we'll pick things up in the next movie.