Dynamic Sound Fade in/out

Dynamic Sound Fade in/out

Hi, I’m trying to loop a sound with a fade-in in the beginning and fade-out when the user turn off the sound but until now I found only one tutorial that show how to achieve this and the code doesn’t work in my Flash 2004 ActionScript 2 http://www.actionscript.org/tutorials/intermediate/Dynamic_Sound_Fade/index.shtml

I make a search on the forum google everywhere and I can get this work anyone can give me a hand with this please?

Thank you for any help :hugegrin:
Lobo

Here is the code from the tutorial


// initiate sound
music = new Sound();
music.attachSound("backsound");
music.start(0, 999999);
// set the volume of the sound to zero
music.setVolume(0);
// set a variable named 'vol'
vol = 0;
// set another variable named 'fade', putting a setInterval function in it
fade = setInterval(fadeIn, 100);
// set the initial fade in function
function fadeIn() {
        // fade the sound in with an increment of 3 in the variable 'vol'
        vol += 3;
        music.setVolume(vol);
        // put an if condition to restrict the increment in volume after it reaches to 100
        if (vol>=100) {
                clearInterval(fade);
                // create the 'step' variable
                step = 1;
                // create the 'fade' variable
                Fade = 0;
        }
}
// create the fade in and out function
// function executed on onEnterFrame
_root.onEnterFrame = function() {
        // set fade out
        if (Fade == 1) {
                vol = vol-step;
                if (vol<0) {
                        vol = 0;
                }
                music.setVolume(vol);
                // set fade in
        } else {
                vol = vol+step;
                if (vol>100) {
                        vol = 100;
                }
                music.setVolume(vol);
        }
};


 
Now, finally, create a button and add this script on it:

on (release) {
        (_root.fade=!_root.fade) ? 0 : 1;
}


hellow :slight_smile: