Something Wrong with the System

A Flash/Actionscript blog

FadeSound AS3 – A class which fades sounds in and out automatically

Get Adobe Flash player

Recently I was working on a project that required me to fade a sound in and out dynamically. Here is the result:

FadeSound (clever name, huh?) is an AS3 class that extends Flashs built in Sound class and includes fading in and fading out (including auto-fade out on sound completion).

Here is the source:

You can probably just dive in and figure it out based on the examples, but I’ll do a write-up after the break.



This method will start playing your sound, the sound will fade in at the beginning for the length of time specified in the argument.


startTime:Number Set the start time in miliseconds (same as the Sound class)

sndTransform:SoundTransform The initial SoundTransform object assigned to the sound channel.   (same as the Sound class)

setFadeInDurationInSeconds:Number Set the amount of time the initial fade in will take. In seconds, so 2.2 will specify a 2.2 second fade in when your sound starts playing.

setAutoFadeOut:Number Set the length of the fade out at the end of the sound. This will automatically start a fade out at the end of the sound.

setEaseType:Function = null Assign an easing function to the fade in.  fl.motion.easing.Linear .Sine etc.


This method allows you to manually fade out and stop your sound at any point.


setFadeOutDurationInSeconds:Number    The time in seconds it will take your sound to fade out.

setEaseType:Function  Assign an easing function to the fade out.  fl.motion.easing.Linear com.gskinner.motion.easing.Sine etc.


soundVolume (Number)

Set the volume of the currently playing sound. This is a number between 0 and 1

autoFadeOut  (Number)

Set the length of the fade out at the end of the sound. This will automatically start a fade out at the end of the sound. Setting this to 0 will give the sound a hard stop at the end.


Included with the class is the class. This is just a container for the three events dispatched by

SOUND_FADE_IN_COMPLETE:String Dispatched when the sound has faded in completely.
SOUND_FADE_OUT_COMPLETE:String Dispatched when the sound has faded out completely.
SOUND_FADE_OUT_STARTED:String Dispatched when the sound has started to fade out.


import ca.organa.sound.FadeSound;
import fl.motion.easing.Sine;
var soundRequest:URLRequest = new URLRequest("MP3/dust_lane.mp3");
var fadeSound:FadeSound = new FadeSound();
fadeSound.addEventListener(Event.COMPLETE, onMP3Loaded);
function onMP3Loaded(e:Event):void {
 fadeSound.removeEventListener(Event.COMPLETE, onMP3Loaded);
 fadeSound.playWithFadeIn(1, null, 3, 2, Sine.easeOut);

This is a pretty simple example.  All I’m doing is creating an instance of the FadeSound class and loading the external MP3 file.

When the file has loaded successfully I call the the playWithFadeInFunction specifying a 3 second fade in, a 2 second automatic fade out at the end and I point to the Sine.easeOut function for my easing.

That’s pretty much it folks. If you have any problems with the class or have any ideas for additional features please leave me a note in the comments and hopefully I’ll get back to you

December 27th, 2010 by Peter Organa

Comments are closed.