We launched a new IT training category! Check out the 140+ courses now.

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

Changing tracks

From: Flash CS3 Professional Beyond the Basics

Video: Changing tracks

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.

Changing tracks

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.

Show transcript

This video is part of

Image for Flash CS3 Professional Beyond the Basics
Flash CS3 Professional Beyond the Basics

79 video lessons · 25638 viewers

Todd Perkins
Author

 
Expand all | Collapse all
  1. 3m 30s
    1. Welcome
      1m 20s
    2. Getting the most from this training
      1m 20s
    3. Using the exercise files
      50s
  2. 15m 28s
    1. Managing digital assets
      4m 9s
    2. Creating custom keyboard shortcuts
      5m 1s
    3. Creating custom color swatches
      3m 41s
    4. Creating a custom template
      2m 37s
  3. 14m 38s
    1. Working with variables
      3m 41s
    2. Using functions
      3m 30s
    3. Working with events
      2m 11s
    4. Using event listeners
      2m 42s
    5. Working with arrays
      2m 34s
  4. 29m 41s
    1. Understanding dynamic text fields
      2m 37s
    2. Loading text from an external file
      6m 42s
    3. Reading HTML from a text field
      5m 29s
    4. Applying CSS with ActionScript
      7m 0s
    5. Applying external CSS to a text field
      7m 53s
  5. 45m 27s
    1. Masking dynamic text
      4m 59s
    2. Dragging the Scroll Bar
      7m 3s
    3. Calculating a scroll percent
      5m 20s
    4. Setting scroll boundaries
      4m 9s
    5. Connecting the ScrollBar to the text field
      2m 55s
    6. Easing the text with the ScrollBar
      4m 12s
    7. Scrolling the text with buttons
      2m 38s
    8. Animating the scrolling text with the buttons
      4m 26s
    9. Setting boundaries for the scrolling text
      4m 26s
    10. Easing the text using the buttons
      1m 37s
    11. Connecting the ScrollBar to the Scroll buttons
      3m 42s
  6. 20m 46s
    1. Organizing a complex object into layers
      1m 51s
    2. Duplicating MovieClips
      1m 29s
    3. Undocking the timeline
      1m 20s
    4. Animating a complex MovieClip
      4m 36s
    5. Creating an animated Hinge effect
      5m 13s
    6. Creating complex animations
      6m 17s
  7. 28m 1s
    1. Controlling objects with buttons
      4m 6s
    2. Understanding frame labels
      3m 25s
    3. Using buttons to navigate labeled frames
      2m 30s
    4. Nesting code within MovieClips
      5m 8s
    5. Adding sound to the application
      2m 34s
    6. Adding drag interactivity to the robot
      6m 1s
    7. Animating the dragged robot
      4m 17s
  8. 20m 3s
    1. Understanding components
      2m 30s
    2. Using components to setup a form
      3m 45s
    3. Setting up the form to send an email
      3m 38s
    4. Working with form variables
      6m 11s
    5. Skinning components
      3m 59s
  9. 27m 40s
    1. Using transparency in a video
      4m 16s
    2. Working with embedded video
      3m 38s
    3. Animating a VideoClip
      2m 53s
    4. Creating an advanced animation with video
      2m 40s
    5. Working with the bitmap data class
      3m 37s
    6. Working with the bitmap class
      1m 40s
    7. Copying the video to create a reflection
      1m 35s
    8. Reflecting the video
      2m 42s
    9. Enhancing a video reflection with a mask
      4m 39s
  10. 1h 38m
    1. Viewing the finished MP3 player
      1m 45s
    2. Understanding the project flow
      2m 52s
    3. Preparing to build the MP3 player
      5m 11s
    4. Understanding classes and packages
      5m 32s
    5. Creating the MP3 player class
      9m 59s
    6. Loading sounds from external files
      9m 20s
    7. Accessing ID3 tag information
      6m 11s
    8. Controlling sound playback with Play and Pause buttons
      11m 3s
    9. Changing tracks
      12m 1s
    10. Displaying the current volume
      9m 33s
    11. Changing the volume
      6m 40s
    12. Wiring the progress bar
      8m 4s
    13. Building the progress bar slider
      10m 4s
  11. 5m 36s
    1. Making Flash movies accessible
      1m 49s
    2. Adding a name and description to your Flash movie
      52s
    3. Accessibility and text fields
      2m 55s
  12. 12m 2s
    1. Understanding Flash Mobile
      35s
    2. Exploring Device Central
      1m 45s
    3. Updating Device Profiles
      1m 7s
    4. Creating a Flash file from Device Central
      2m 7s
    5. Creating and testing Flash Lite applications
      2m 48s
    6. Downloading the Flash Lite player for Windows Mobile
      1m 44s
    7. Getting a Flash Lite application onto your phone
      1m 56s
  13. 27s
    1. Goodbye
      27s

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 Flash CS3 Professional Beyond the Basics.

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 preferences from 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.