Viewers: in countries Watching now:
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.
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.
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.