Join Todd Perkins for an in-depth discussion in this video Creating the Player class, part of Flash CS4 Professional Audio Techniques.
- View Offline
In this movie we will look at creating the main ActionScript class for our audio player. The first thing I want you to do is go to the Library, right-click or Ctrl+Click the Player movie clip and choose Properties. Note that I have this movie clip Exported for ActionScript, and the Class I am using is com.lynda.audio.Player. If your player ActionScript file is just a blank ActionScript file, then Flash is going to get mad at you for doing this.
If your player Class file does not have any code in it yet, this may not work. So, you may want to write the code and then come back to Symbol Properties, and type in the name here. Notice it's the fully qualified class name, which includes the package com.lynda.audio, and just as a refresher the package name corresponds to that folder structure. So, there's the com folder, inside of the com folder is lynda, inside of the lynda folder is the audio folder, which contains the file Player. So, I am going to click OK.
So, once you have done that, go over to Player.as. Now, I have already started building this Class file and I am just going to get a little bit more room here by minimizing the other panels. Now, I just wrote the basic skeleton of a Class file. If you don't have the Exercise Files, just copy the code I have here. Make sure to declare the com.lynda.audio package, and import the main classes we will be using. All the display classes, all the event classes, all the geometry classes which is flash.geom, media classes for the sound, and the net classes for the URL request.
I have my class declaration here. I am extending the MovieClip class, and inside of there I have my constructor defined. So, let's start defining a few properties. In order to work with sound, you need a sound object and a sound channel object. So, first I will create a public property. So, public var channel, datatype it to a SoundChannel. Remember the SoundChannel class is used to play the sound, and stop a sound, to get the sounds position and everything.
The sound class basically just loads the sound. Another public property called sound, datatype it to an instance of the Sound class. And one more property, this will also be public and we will call this resumeTime, datatype it to a Number and set it's initial value to 0. This will represent the time in milliseconds that the file will resume at when you click the play button. So, 0 would be the very beginning of the song and a thousand would be 1000 milliseconds or 1 second into the song.
We will look at that in a little more detail later on. Inside of the constructor, we are going to set play_mc.buttonMode = true. So, buttonMode = true, same thing for pause_mc. Remember that button mode is what makes a movie show the hand icon when you roll over it. And then finally what we will do is set the pause_mc to hide at the beginning.
So, we won't have the music playing until the person clicks the play button, and when the play button shows I don't want the pause button to show and vice versa. So, pause_mc.visible = false. And there's the basic setup for our Class. Now, just to make sure everything is working, just save this file and test the movie straight from the class file as long as your Target is set to the proper FLA file you should be good. Test the movie, and you should see the hand cursor when you roll over the play button, and no file should play yet.
All right, looks like everything is working properly. So now our player class is officially set up.
- Playing and pausing audio in a created player class
- Controlling audio progress and showing it in a text field
- Loading XML playlist data into the final player
- Animating a volume slider and using effects to add depth
- Making Flash applications sharable
- Viewing the finished player and taking it for a spin