Join Todd Perkins for an in-depth discussion in this video Playing audio, part of Flash CS4 Professional Audio Techniques.
Now that our player class is set up, we can take a look at loading an external sound file and playing it in the Flash Player. Now, this may be a review for you but it's good to go over it again just as a refresher. The first thing I am going to do is at the bottom of the constructor method, I am going to type play_mc. addEventListener and we will add a MouseEvent.CLICK listener, so MouseEvent.CLICK and we will run a method called playSong.
And now I'll define a public method called playSongs. So, public function playSong, we will receive an event with the datatype of MouseEvent and I am actually going to give this a default value of null. So, let's set that MouseEvent = null. That way we can run playSong somewhere else if we want to. We don't have to rely on that MouseEvent. So, close out the parenthesis, :void, make sure the V is lower case and some curly braces.
So, in here we will create a variable called soundFile, datatype it to a String, and we will set it equal to the name of the audio file that we are going to load. Now, my file is called 01 Eyes Wide.mp3. So, I have that file there, and now let's set sound equal to a new instance of the sound class using that new constructor, and then I will have the sound load the sound files.
So, sound.Load and in the parenthesis here I am going to create a new URLRequest. And I will pass in soundFile. Now, of course soundFile could have been a URL request and we could have created the URL request right here. But I am just doing it this way, just a matter of personal preference, there's really no advantage to doing it this way. So, on the next line we will set, channel = sound.play.
So, remember when you are working with audio, the channel is what controls all the sound. Even though this sound object has the play method, we put all of the control inside to that channel object. Because basically, all you can do with the sound is load it and play it. But with the channel, we can pause, we can resume, and we can even check the position of the sounds. So, all of that functionality is reserved to the sound channel class. And that's why we are setting the channel equal to the playing sound, or in other words sound.play.
Because that method returns a sound channel instance that we can then control. And finally, inside of this method set, play_mc.visible = false. We don't want the play movie clip showing when the sound is playing, we want the pause movie clip showing. So we will set, pause_mc. visible = true, and that's it. So, save the file, and again we will just test it straight from this class. And we should be able to play this sound when we click the play button. (Music playing.) And looks like the sound is playing just fine.
So, we now have successfully set up playing our external sound file. And by the way, if you load this sound and play it at the same time, then the sound will play before it's done loading. So, this is how you see sites, maybe like YouTube or other sites that have an external file playing before it's finished loading. You just tell it to play early, and as soon as there is enough data to play it, then the sound will just play.
And will continue to load as the sound is playing.
- 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