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

Handling media pauses


From:

JavaScript: Events

with Ray Villalobos

Video: Handling media pauses

So, our audio jukebox is working pretty well, but there's obviously a few errors. Now, if we play a song (MUSIC) The song obviously plays. We've got it resetting back to its original style but right now if I want to pause the song I actually have to reload the page. So, if I click on one of these (MUSIC) The only way to pause them is by hitting the refresh page, so I want to make sure I deal with that issue.
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

Handling media pauses

So, our audio jukebox is working pretty well, but there's obviously a few errors. Now, if we play a song (MUSIC) The song obviously plays. We've got it resetting back to its original style but right now if I want to pause the song I actually have to reload the page. So, if I click on one of these (MUSIC) The only way to pause them is by hitting the refresh page, so I want to make sure I deal with that issue.

Now to do that we're going to need to reformat our JavaScript a little bit, so let's get into it. Now the first thing I want to do is create a new variable to make sure that I know whether or not a song is currently playing. So I'll call that variable songPlaying. And, I'm going to set that to a document query selector and I want to find anything that has the ID of player. So I make sure I put a pound sign here. If you remember, in the previous movie, I mentioned that I was creating this ID. And that we weren't using it yet but it was going to be useful later on.

Well, this is later on and this is where you have made your life easier by giving this element an ID. Now we can just check the document for the existence of a player and if it does exist then I can do stuff to it. Now I can say something like if the song is playing, then do something. Otherwise do something else. And really what I want to do is have the something else be everything that I did before.

Because if a song is not playing then I want to start playing the song, which is what I was doing before. So I'm going to grab all this and insert it right here. And since I'm creating the element on this line, I'm going to move that to the top of this L statement right here. That's the first line of creating a new element called audio. And the rest of it is just setting that element's properties. So we haven't really done anything yet. What we wanted to do was make sure that we could create essentially a toggle, something that allows me to pause and continue to play a paused song.

So to do that I need to put all that code right in here. And that's why I separated everything else. So what I need to do is check to see if the song that's currently playing has been paused. And I can do that by using one of the properties of any sound element. So it goes something like this if I say if song playing.paused then do something and otherwise. Do something else. Now, where am I getting this paused from? Well, if you look at the properties available to the sound element you can see that there are methods and properties for audio and video elements.

If you scroll down in the properties section you can see that there's one called paused. And it sets, it sets or returns if the audio, video is paused or not and that sounds a little complicated. But essentially I can check to see whether or not an element is playing by checking to see if its paused, if its paused it means its not playing or it has been paused. So, what I want to do here, is, make sure that I say, if the element has been paused than say, song playing, got played. Because now I want to play it, it's been paused.

And then, I want to set the ID of this element, the target of what I clicked on to playing. Now, if it hasn't been paused, then I want to say song playing. And then, pause and use the method, remember, the method right here to pause a song is this pause method. And then set the target of what I clicked on e.target. ID equal to paused. So this is just gnona set the right style for the element.

If you remember, we set those up in our style sheet. So there is a paused ID as well as a playing ID. Alright, so let's go ahead and save this and go into our jukebox. And refresh and see if we can pause some of these songs. (MUSIC) So excellent! You can see the style is being applied if we take a look at the audio player, it doesn't look like anything's happening. But you can tell that the music stopped playing. And, if you look at the list item, you can see the ID of pause. Let's try some of these other ones. (MUSIC) Now there's a couple of problems that you're probably seeing, I can pause more than one thing.

Let me go ahead and refresh this and play that one. Refresh, And play this one. So, there's still a few problems we need to fix but we've been able to separate whether or not a song is playing or not. And also detect whether or not a song is paused or is currently playing and we've essentially created a toggle. So we'll continue working on this 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.