Monday, August 16, 2010

Mute Button, soundChannel and soundTransform


Every sound that plays in the flash player plays through a unique sound channel. In order to control a sound that we have loaded and are able to play in Koolmoves we need to use this soundChannel Class. We play the sound through the sound Channel and if we want to stop the sound, we stop the sound Channel and not the sound itself. When it comes to controlling a sounds volume we use the soundTransform Class. In this post we are going to make a Mute Button, which is in reality a volume toggle. If we were to stop the sound using the soundChannel we would then have to come up with a way to re-load it. But we want the ability to turn the sound on and off while the sound continues playing. This is done with a conditional statement as we will see.


If you haven't done so, load up the previous file on loading a sound. This is what we will be working from. If you haven't got that project yet, go ahead and review the post on here: EYE-gor's Place: Koolmoves and Sound.
Add a button object from the koolmoves library or make one and name it "muteBtn". Leave this on the stage and open the ActionScript Editor. You should have a sound object in the Symbol Library with a class name of Ambient and your ActionScript on the timeline should look like this:


var ambientSound:Ambient = new Ambient();
ambientSound.play();


add two new variables after the first line of code:


var scChannel:SoundChannel = new SoundChannel();
var stTransform:SoundTransform = new SoundTransform();


and edit the last line of code to look like this:


scChannel = ambientSound.play();


from this point we are setting up our sound to play through a soundChannel defined as "scChannel" and set up a SoundTransform object called "stTransform". We should be able to play this again except this time the sound is playing through the soundChannel we have defined. The SoundTransform object is doing nothing yet. Our code should now look like this:


var ambientSound:Ambient = new Ambient();
var scChannel:SoundChannel = new SoundChannel();
var stTransform:SoundTransform = new SoundTransform();
scChannel = ambientSound.play();


The SoundTransform class contains properties for volume and panning, so this is the means by which we will develop the mute Button. We begin by starting to write an eventListener and a function for the button we have previously placed on the stage and add the code after our list of variables.


var ambientSound:Ambient = new Ambient();
var scChannel:SoundChannel = new SoundChannel();
var stTransform:SoundTransform = new SoundTransform();


muteBtn.addEventListener
    (MouseEvent.CLICK,muteButtonHandler);


scChannel = ambientSound.play();


This event listener tells the player to listen for a mouse button click on our muteBtn. When the button is clicked it will call the function "muteButtonHandler" which we will write next. Add the following code after the event Listener so your code looks like this:


var ambientSound:Ambient = new Ambient();
var scChannel:SoundChannel = new SoundChannel();
var stTransform:SoundTransform = new SoundTransform();


muteBtn.addEventListener
    (MouseEvent.CLICK,muteButtonHandler);


function muteButtonHandler(evt:MouseEvent):void{
    if (stTransform.volume > 0){
        stTransform.volume = 0
    }else{
        stTransform.volume = 1;
    }
    scChannel.soundTransform = stTransform;
}


scChannel = ambientSound.play();


What we have written is a conditional statement that says if the volume is greater than zero, or if the volume is on, turn the volume off. Otherwise turn the volume on. A "0" indicates 0% system volume and a "1" indicates 100% system volume. A".5" would indicate 50% system volume so you can see how this SoundTransform class is used for volume control. Within the function the soundTransform object itself has been passed into the sound Channel. The function for the mute Button will now turn our soundChannel on and off.


These last two Blog posts have explored adding a sound through the Symbol Library, playing it, passing that sound object through a soundChannel, and applying the SoundTransform volume properties to a mute Button function in order to mute the sound.

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.