Start your free trial now, and begin learning software, business and creative skills—anytime, anywhere—with video instruction from recognized industry experts.

Start Your Free Trial Now

Monitoring media-ended events


From:

JavaScript: Events

with Ray Villalobos

Video: Monitoring media-ended events

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.
please wait ...
Watch the Online Video Course JavaScript: Events
2h 11m Intermediate Sep 17, 2013

Viewers: in countries Watching now:

Learn about the different types of JavaScript events available to use in your applications, including traditional events like mouse, form, and window events, plus events that allow you to manage the DOM and HTML5 events that deal with media. Author Ray Villalobos shows how to propagate and register events during the capturing and bubbling phases, cancel an event or default behavior, and respond to clicks, mouse-overs, right-clicks, following the mouse, and loading images. Plus, Ray demos an audio jukebox project and a drag-and-drop game that shows JavaScript desktop and touch events in action.

Topics include:
  • 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
Subjects:
Developer Web
Software:
JavaScript
Author:
Ray Villalobos

Monitoring media-ended events

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.

There are currently no FAQs about JavaScript: Events.

 
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now Already a member? Log in

* Estimated file size

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed JavaScript: Events.

Return to your organization's learning portal to continue training, or close this page.


OK

Upgrade to View Courses Offline

login

With our new Desktop App, Annual Premium Members can download courses for Internet-free viewing.

Upgrade Now

After upgrading, download Desktop App Here.

Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

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.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Start your FREE 10-day trial

Begin learning software, business, and creative skills—anytime,
anywhere—with video instruction from recognized industry experts.
lynda.com provides
Unlimited access to over 4,000 courses—more than 100,000 video tutorials
Expert-led instruction
On-the-go learning. Watch from your computer, tablet, or mobile device. Switch back and forth as you choose.
Start Your FREE Trial Now
 

A trusted source for knowledge.

 

We provide training to more than 4 million people, and our members tell us that lynda.com helps them stay ahead of software updates, pick up brand-new skills, switch careers, land promotions, and explore new hobbies. What can we help you do?

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.