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.

2 comments:

  1. thank you very much, I was very useful to combine
    var ambientSound: Ambient Ambient = new ();
    var scChannel: SoundChannel SoundChannel = new ();
    var stTransform: SoundTransform SoundTransform = new ();


    muteBtn.addEventListener
    (MouseEvent.CLICK, muteButtonHandler);


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


    ambientSound.play scChannel = ();

    I was already desperate, but you gave me the solution thanks

    ReplyDelete
  2. Glad it was helpful. Sorry to respond so late. I haven't been here for some time.

    ReplyDelete