Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Handling media pauses

From: JavaScript: Events

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.

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.

Show transcript

This video is part of

Image for JavaScript: Events
JavaScript: Events

27 video lessons · 7012 viewers

Ray Villalobos
Author

 

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
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

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
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?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

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:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
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.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

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.