A continuation from the music player tutorial we introduce the Sound Transform class to handle sound volume control. The buttons are created live on stage.
CLICK HERE FOR A COMPLETE CARTOON SMART SOUND AND MUSIC TUTORIAL
HERE IS THE SOURCE CODE!
// by Avraham Saltoun
// solutions at say-web dot com
// FREE to USE, COPY and DISTRIBUTE
var req:URLRequest = new URLRequest (“SmartSound.mp3″);
// this is the location of the sound file
var sound:Sound = new Sound();
// The sound class is the recepient for the
// imported sound file
var controller:SoundChannel;
// the controller sound channel
// will be used to play and stop the music file
var volumeControl:SoundTransform;
/* The volumeControl will handle the volume
level and must have a value between 0 and 1 */
function soundLoaded(event:Event):void {
controller = sound.play();
controller.stop();
// this will stop the file from playing
// right after loaded.
/* we must give a value to volume control variable
to start with */
volumeControl = controller.soundTransform;
play_btn.addEventListener(MouseEvent.CLICK, playSound);
stop_btn.addEventListener(MouseEvent.CLICK, stopSound);
// the volume up and down buttons
up_btn.addEventListener(MouseEvent.CLICK,volumeUp);
down_btn.addEventListener(MouseEvent.CLICK,volumeDown);
}
function playSound(event:MouseEvent):void{
controller = sound.play();
}
function stopSound(event:MouseEvent):void {
controller.stop();
}
function volumeUp(event:MouseEvent):void {
volumeControl.volume = volumeControl.volume+0.1;
//now let’s check and see
//if it does not exceed 1
if (volumeControl.volume >1){volumeControl.volume = 1;}
//now let’s apply the new volume level!
controller.soundTransform = volumeControl;
}
function volumeDown(event:MouseEvent):void {
volumeControl.volume -= .1;
//now let’s check and see
//if it does not lower than 0
if (volumeControl.volume <0){volumeControl.volume = 0;}
//now let's apply the new volume level!
controller.soundTransform = volumeControl;
}
sound.addEventListener(Event.COMPLETE, soundLoaded);
sound.load(req);
/*
I hope it helps
thanks for watching */






18 Responses to “Video Tutorial 30: ActionScript 3.0 Volume Controls for Your Flash Application”
Trackbacks/Pingbacks