Sunday, August 15, 2010

Koolmoves and Sound


Many times on the Koolmoves forum at Flashkit there are questions relating to sound. There are a number of ways to manipulate sound in Flash and ActionScript 3 has a few classes related to handling sound. But for now we will look at a simple procedure for loading sound and then we'll look at constructing a Mute Button to manipulate that sound. Sounds by themselves can be sound effects, ambient sound or external playlist sound. Sound effects can be used when a button is clicked and these can be applied directly to the button via the GUI. Ambient Sound, such as background music can be added to the Movie either externally or they can be embedded into the movie by way of the Symbol Library. (We are going to use the Symbol Library method). External playlists are beyond the scope of this Tutorial but they fall into the realm of XML and media player controls.
To begin, you will need to save a sound file in the folder that you are saving your Koolmoves project. You can get good demo files here at Necromanthus.com under the Music tab. Just click on one you like and save into your folder. I chose "Daemon".
Now, with Koolmoves open and set for Flash 10 AS3 export, open the symbol library (F11). Under the "Sounds" tab click "New" and navigate to your sound file and select and open it. In the next dialog box give it a class name. In my case I call it "Ambient" even though it is music. It could be birds chirping or the sound of cars or whatever you wanted. Click Ok. You have just saved a sound file, and given it a class name. We will now use ActionScript to load and play this sound.

Next, open the ActionScript editor. I usually right click the stage and select "Edit Frame Action Script".
The first thing we will do is instantiate our sound with the following code.

var ambientSound:Ambient = new Ambient();

where "ambientSound" is the name of our child of the class Ambient which we previously created in our Symbol Library. You can name it whatever you like.

Then we simply tell it to play with:

ambientSound.play();
This will play the sound through one time. Notice that since is not a visual object we don't need to add it to the stage with an addChild(). If you want the sound to loop, you need to pass the play() two arguments such as play(0,3) which tells the sound to play at the beginning and loop 3 times.

Click OK and test your movie, you should hear your sound. Most people like to manipulate the sound, so we will be considering that next time with the creation of a Mute Button which will make use of the soundTransform class and the soundChannel class.

No comments:

Post a Comment