# Dynamic Sound Fade in/out

**URL:** <https://forum.kirupa.com/t/dynamic-sound-fade-in-out/116647>\
**Category:** Uncategorized\
**Created:** [July 20, 2004, 8:26am UTC](https://forum.kirupa.com/t/dynamic-sound-fade-in-out/116647 "2004-07-20T08:26:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lobo\_](https://avatars.discourse-cdn.com/v4/letter/l/bb73d2/32.png) [@Lobo\_](https://forum.kirupa.com/u/Lobo_)\
**Post date:** [July 20, 2004, 8:26am UTC](https://forum.kirupa.com/t/dynamic-sound-fade-in-out/116647/1 "2004-07-20T08:26:50Z")

</div>

**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](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

```php

// 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);
        }
};

```

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

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

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 21, 2004, 6:30am UTC](https://forum.kirupa.com/t/dynamic-sound-fade-in-out/116647/2 "2004-07-21T06:30:14Z")

</div>

hellow 🙂
