Start learning with our library of video tutorials taught by experts. Get started
Viewers: in countries Watching now:
Through several hands-on tutorials, instructor Todd Perkins shows how to best build dynamic, streamlined sites using Flash CS3 Professional. Learn how to create custom keyboard shortcuts, apply advanced text techniques such as animating scrolling text with custom easing controls, and using advanced animation techniques. Flash CS3 Professional Beyond the Basics covers many challenging features, including adding complex interactivity to a Flash CS3 project and getting Flash content on a phone. Exercise files accompany the tutorials.
Now we'll talk about using the Next and Previous buttons to change the tracks in our MP3 Player. If you're following along I'm working in 09_Changing_Tracks.fla in the 09 folder in the Chapter 9 folder in the Exercise Files folder. Now let's start by doing our favorite thing, by clicking the Publish Settings button. The settings version for ActionScript version in the Publish Settings window, we'll select our class path, remove the class path, browse to a new class path in the Chapter 9 folder, in the 09 folder, in the Star folder and the classes folder. We'll choose that class.
And now we'll open up the class files we'll be working with in this exercise. Choose File, Open, and in the Chapter 9 folder find the 09 folder then we're navigating into the Start folder, into the Classes folder, com, lynda, audio, mp3 and then we're going to grab the SoundLoader class. I'm going to Cmd+click the SkipButtons class, that's Ctrl+click on the PC, to select both classes and Click the Open button. Let's work in the SoundLoader class first. One of the variables we're going to need to change tracks using our buttons is something you check whether the sound is currently playing. Because if the sound is playing and we'll make it play again the sound will layer on top of itself and we don't want that to happen.
So below our variables we'll create a public variable and this variable we'll call isPlaying with capital P. Also the datatype is Boolean and we'll give it an initial value of false. Now we'll set the value of isPlaying variable. Scroll down in your code and find the pauseSong and playSong functions. At the bottom of the pauseSong function we'll set the value of isPlaying to false so the song is paused it is not playing and at the bottom of the playSong function we'll set isPlaying to true.
Next we'll scroll up into our songLoaded function and right above the the conditional state where you wrote, check to see if the movie has not been initialized. We'll create another conditional statement, and we'll check to see if the sound is playing. So type isPlaying in the parentheses for the if statement and if the sound is playing for example, when the sound is already playing because the user clicked the Play button and then they clicked the Next button to go to the next track. We want the sound to continue playing. So we'll type song= loader.play. The next thing we'll do is scroll down the very bottom of our code and create a new function that's going to be a public function and we'll call this changeSong. This will change the current song.
It will receive one value we'll call that dir, which means short for direction. We're either going to have this value be a negative one or a positive one. We'll set the data type to be integer and it will return a value so type void for the return data type and in the curly braces for this function we'll type songIndex += dir. What that's going to do is change the songIndex value based on the value passed in as the song direction. So it's either going to go one forward or one back. Now there are going to be cases where the user clicks the Next button or the Previous button and the songIndex is going to go out of bounds. For example, there are three songs inside of our song list Array, so we have an Index of 0, 1 and 2.
If the user keeps on clicking the Next button than the songIndex is going to go past 2 and go out of bounds. So what we're going to do is write some conditional statements to keep the songIndex in bounds. Then type if songIndex is greater than songList.length - 1 So if it's going too far, the bounds will just loop the sound around and set the songIndex to zero, loop the songs from the last song back to the first song, we'll set the songIndex to zero, and below that conditional statement create an else if statement that checks to see if a songIndex is less than zero.
If the songIndex is gone below zero, then we'll set songIndex equal to songList.length - 1. Next thing we'll do inside of this function is make the song stop. So type song.stop. So it will stop playing and what we're going to do is load another song. To load another song, we can simply copy the code from the top of this class. In a constructor function, you'll find four lines of code in my code, they are from lines 25 to 28 where we set the value of the Sound Request variable, loaded the external sound and we added EventListeners to the loader.
So what I'm going to do is select that block of code and press Cmd+C or Ctrl+C on the PC to copy the code. I'm going to scroll down inside of our changeSong function at the very bottom, I'm going to paste the code that I copied using Cmd+V on the Mac or Ctrl+V on the PC. So the code that we've just pasted will work exactly the same way as the code that we copied. However we're going to get an error unless we create a new instance of the sound class. The problem that we will run into if we don't do that is that our SoundLoader will continue trying to load sounds within itself. So we need to create a news sound instance every time we load a sound. So right above loader.load in the code that you just pasted, type loader = new sound. So loader is a new instance of the Sound class. The next step is to create a new instance of the skipButtons class so that we can run this changeSong function by clicking the Next and Previous buttons. Let's scroll up in our code and then find the songLoaded function, mine goes from line 31 to line 47, find the conditional statement at the bottom of that function and at the bottom of that conditional statement type musicPlayer.skipButtons, with a capital B, this refers to the skipButtons property of our MP3 Player object and we'll set that equal to a new instance of the skipButtons class.
We're going to pass in some values to this new skipButtons object. The values we're going to be passing in are the nextButton, the previousButton and our musicPlayer object. So type musicPlayer dot nextButton with capital B and the next parameter will be musicPlayer dot , previous button, prevButton and then musicPlayer for the last parameter. Once you've typed that, choose File, Save to save the file.
Now we'll work in the skipButtons file. So now a new instance of this class is created from our soundLoader class. What we're going to do is make the Next and Previous buttons run the chain song function that we just wrote. Right above our constructor function let's create some variables to hold the values passed in through the parameters of the constructor function. So we'll create three private variables. So the first one is going to be called nextButton and the data type is going to be a SimpleButton, and the next one is going to be Previous button, prevButton.
The data type is also going to be SimpleButton and the third variable is going to be called musicPlayer and the data type for that will be Mp3Player. This constructor functional now will need to receive three parameters, because that's how many parameters we passed in when we created an instance of this class from our sounLoader class. The first object is going to be the nextButton, so we'll call that nxt with data type as SimpleButton, and the next one will be the Previous button so we'll call that prev and the data type for that will be SimpleButton, and a third parameter is the musicPlayer, we'll call that player and the data type will be Mp3Player.
Inside the Constructor function we'll set the values of variables that we created to the values passed in the constructor function. So I'm just going to type the code and then I'll explain it. So I set the value of our nextButton equal to the next button past in when an instance of this class is created, and the same thing for the Previous button and the musicPlayer. We're just associating those values passed in whith the properties or variables that we created inside of this class. On the next line, let's go to the Next button, so type nextButton.addEventListener and the event is going to be MouseEvent.CLICK, and when this button is clicked we'll run a function called nextTrack. Now on the next line We'll add EventListener to the Previous button and the event will be MouseEvent.CLICK and the function it will run will be called prevTrack.
Let's define those functions. Below the Constructor function, create a Private function, and call it nextTrack capital T, for Track and will receive an event with the data type of MouseEvent and inside of that function we'll simply communicate to our soundLoader object and run the changed song function. So type musicPlayer.soundLoader.changeSong and we're going to pass in a value to this function, so what I'm going to do is pass the value of 1 to move the song forward to go to the next song in the Array. Let's select this entire function and copy by pressing Cmd+C on the Mac or Ctrl+C on the PC. A few lines down we'll paste the code by pressing Cmd+V on the Mac or Ctrl+V on the PC, change the name of the function to Previous Track or prevTrack and then change the code inside of the function instead of 1 after changeSong to negative 1, so we'll move the track backwards.
So now let's test this to see it in action. Choose File, Save to save the file and then test the movie using Cmd+Return on the Mac or Ctrl+Enter on the PC. Click the Next and Previous buttons to watch the song names change. So the buttons are working great when the song is not playing. Now let's click the Play button and click those buttons while the song is playing and then we can see if its working while the song plays (Music playing) And it's working great. Let's go back to the SoundLoader.as file and talk about one different way that we can change songs. Another way that you can change songs is to change the song once a sound is finished playing in its entirety.
Let's take a look at how to do that. Scroll down to the songLoaded function, right below song.stop, I'm on line 35 in my code, let's go to the next line and we'll type song.addEventListener and the event is going to be Event.sound_COMPLETE, all caps, I'll run a function called soundFinished and I'll just copy this line of code by selecting it and pressing Cmd+C on the keyboard and we're going to paste it in another place where we play the sound. Another place where we play songs in the playSong function. At the bottom of the playSong function let's paste the code that we copied using Cmd+V on the Mac or Ctrl+V on the PC. So whenever a sound starts playing, we immediately add this EventListener to listen for when the sound is complete. Now let's create our its soundFinished function. Scroll down to the bottom of your code and create a new private function called soundFinished. It will receive an event with the data type of Event and inside of the function we'll simply call the changeSong function and pass in a value of 1 to go to the next song.
Now let's test this and see it in action. Let's save the file first by choosing File, Save and then test the movie using Cmd+Return on the Mac or Ctrl+Enter on the PC. Now what we want to do is listen to one of the songs in its entirety. If the song finishes and goes to the next song then we've done our job correctly. So I'm going to push the Play button to test this on. So looks like it works. And there's looking at how to change song tracks.
There are currently no FAQs about Flash CS3 Professional Beyond the Basics.
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.