# Fade in Sound

**URL:** <https://forum.kirupa.com/t/fade-in-sound/26493>\
**Category:** flash\
**Created:** [July 23, 2003, 4:52pm UTC](https://forum.kirupa.com/t/fade-in-sound/26493 "2003-07-23T16:52:08Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![wyclef](https://avatars.discourse-cdn.com/v4/letter/w/65b543/32.png) [@wyclef](https://forum.kirupa.com/u/wyclef)\
**Post date:** [July 23, 2003, 4:52pm UTC](https://forum.kirupa.com/t/fade-in-sound/26493/1 "2003-07-23T16:52:08Z")

</div>

Hello,

I want to have one sound object, and one short sound looping forever. I want that sound to fade in and I want a simple sound on, sound off button. I don’t want to use onEnterFrame either. Is there a way to do this without onEnterFrame?  
This is the code I found but it uses onEnterFrame.

[AS]  
//\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
// create Sound obj and set volume at 0  
//\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
sndObj = new Sound();  
sndObj.attachSound(“loop”);  
sndObj.setVolume(0);  
//\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
// function to fade in the sound  
//\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
function fadeSnd(speed) {  
this.onEnterFrame = function() {  
vol += speed;  
if (sndObj.getVolume() == 100) {  
//trace(“sound at max!”);  
delete this.onEnterFrame;  
} else {  
sndObj.setVolume(vol);  
// show the fade  
display = sndObj.getVolume();  
}  
};  
}  
//\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
// button to activate snd and fadeSnd function  
//\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
sndBtnOn\_mc.onPress = function() {  
sndObj.start(0, 9999);  
fadeSnd(1);  
};  
//\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

[/AS]

---

<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 23, 2003, 5:12pm UTC](https://forum.kirupa.com/t/fade-in-sound/26493/2 "2003-07-23T17:12:45Z")

</div>

well if you don’t want to use onEneterFrame the you could always makeit a 2-frame-loop!

---

<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 23, 2003, 5:43pm UTC](https://forum.kirupa.com/t/fade-in-sound/26493/3 "2003-07-23T17:43:25Z")

</div>

**Untested.**  
[AS]sndObj = new Sound();  
sndObj.attachSound(“loop”);  
sndObj.setVolume(0);  
sndObj.fade = function(speed) {  
var vol = this.getVolume()+speed;  
this.setVolume(vol);  
display = this.getVolume();  
if (this.getVolume()\>100) {  
this.setVolume(100);  
clearInterval(this.interval);  
}  
};  
sndBtnOn\_mc.onPress = function() {  
sndObj.start(0, 9999);  
sndObj.interval = setInterval(sndObj, “fade”, 1000/12, 1);  
};[/AS]

---

<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 23, 2003, 7:30pm UTC](https://forum.kirupa.com/t/fade-in-sound/26493/4 "2003-07-23T19:30:35Z")

</div>

Cool! :cyborg:

So how do I have it fade out when another button is pressed? Do I need to clear interval? Also, I tried adding this code, so that you can’t keep clicking the button when it’s already playing to have a bunch of loops playing at once but it didn’t work…

[AS]  
if (!this.loop.alreadyPlaying) {  
this.loop.alreadyPlaying = true;  
this.loop.start(0, 999);  
}  
[/AS]

---

<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 23, 2003, 8:15pm UTC](https://forum.kirupa.com/t/fade-in-sound/26493/5 "2003-07-23T20:15:17Z")

</div>

Untested again, I don’t have Flash installed in this PC. 😛  
[AS]sndObj = new Sound();  
sndObj.attachSound(“loop”);  
sndObj.setVolume(0);  
sndObj.fadeIn = function(speed) {  
var vol = this.getVolume()+speed;  
this.setVolume(vol);  
display = this.getVolume();  
if (this.getVolume()\>100) {  
this.setVolume(100);  
clearInterval(this.interval);  
}  
};  
sndObj.fadeOut = function(speed) {  
var vol = this.getVolume()-speed;  
this.setVolume(vol);  
display = this.getVolume();  
if (this.getVolume()\<0) {  
this.setVolume(0);  
this.stop();  
this.playing = false;  
clearInterval(this.interval);  
}  
};  
sndBtnOn\_mc.onPress = function() {  
if (!sndObj.playing) {  
sndObj.start(0, 9999);  
clearInterval(sndObj.interval);  
sndObj.interval = setInterval(sndObj, “fadeIn”, 1000/12, 1);  
sndObj.playing = true;  
}  
};  
sndBtnOff\_mc.onPress = function() {  
if (sndObj.playing) {  
clearInterval(sndObj.interval);  
sndObj.interval = setInterval(sndObj, “fadeOut”, 1000/12, 1);  
}  
};[/AS]

---

<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 23, 2003, 8:19pm UTC](https://forum.kirupa.com/t/fade-in-sound/26493/6 "2003-07-23T20:19:17Z")

</div>

Thanks!

😛

Do you know how to fade out a sound after a specific number of loops? (If this is complicated don’t worry about it)

Oh yeah, and say I want to have the sound fade in be the default when you first come to the page?

---

<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 23, 2003, 8:22pm UTC](https://forum.kirupa.com/t/fade-in-sound/26493/7 "2003-07-23T20:22:23Z")

</div>

[AS]sndObj.totalLoops = 5;  
sndObj.loop = 0;  
sndObj.start();  
sndObj.onSoundComplete = function() {  
if (++this.loop\>this.totalLoops) {  
this.interval = setInterval(this, “fadeOut”, 1000/12, 1);  
delete this.onSoundComplete;  
}  
this.start();  
};[/AS]  
??

---

<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 24, 2003, 9:37pm UTC](https://forum.kirupa.com/t/fade-in-sound/26493/8 "2003-07-24T21:37:38Z")

</div>

Hey,

When I just add sndObj.start(); to the bottom of the script it doesn’t fade in as default. Should it be sndObj.start(fadeIn);?

---

<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 25, 2003, 2:37am UTC](https://forum.kirupa.com/t/fade-in-sound/26493/9 "2003-07-25T02:37:37Z")

</div>

[AS]sndObj.start();  
sndObj.interval = setInterval(sndObj, “fadeIn”, 1000/12, 1);[/AS]  
:sigh:

---

<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:** [August 4, 2003, 1:18pm UTC](https://forum.kirupa.com/t/fade-in-sound/26493/10 "2003-08-04T13:18:40Z")

</div>

Cool, so is the sound object “outDoors” indepenent from the setVolume of 17? If not how can I make that sound object have it’s own volume setting?

[AS]  
fairyTailz = new Sound();  
fairyTailz.attachSound(“loop”);  
fairyTailz.setVolume(0);  
outDoors = new Sound();  
outDoors.attachSound(“nature”);  
// --------------------------------------------------------------------------------------  
fairyTailz.fadeIn = function(speed) {  
var vol = this.getVolume()+speed;  
this.setVolume(vol);  
display = this.getVolume();  
if (this.getVolume()\>17) {  
this.setVolume(17);  
clearInterval(this.interval);  
}  
};  
fairyTailz.fadeOut = function(speed) {  
var vol = this.getVolume()-speed;  
this.setVolume(vol);  
display = this.getVolume();  
if (this.getVolume()\<0) {  
this.setVolume(0);  
this.stop();  
this.playing = false;  
clearInterval(this.interval);  
}  
};  
sndBtnOn\_mc.onPress = function() {  
if (!fairyTailz.playing) {  
fairyTailz.start(0, 999);  
clearInterval(fairyTailz.interval);  
fairyTailz.interval = setInterval(fairyTailz, “fadeIn”, 500/12, 1);  
fairyTailz.playing = true;  
}  
};  
sndBtnOff\_mc.onPress = function() {  
if (fairyTailz.playing) {  
clearInterval(sndObj.interval);  
fairyTailz.interval = setInterval(fairyTailz, “fadeOut”, 200/12, 1);  
}  
};  
// --------------------------------------------------------------------------------------  
fairyTailz.start(0, 999);  
fairyTailz.interval = setInterval(fairyTailz, “fadeIn”, 500/12, 1);  
if (!fairyTailz.playing) {  
fairyTailz.start(0, 999);  
clearInterval(fairyTailz.interval);  
fairyTailz.interval = setInterval(fairyTailz, “fadeIn”, 500/12, 1);  
fairyTailz.playing = true;  
}  
outDoors.start(0, 2);  
[/AS]

---

<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:** [August 15, 2003, 10:51am UTC](https://forum.kirupa.com/t/fade-in-sound/26493/11 "2003-08-15T10:51:43Z")

</div>

I have one another question about this very  
interesting thread.

Im using the script kax wrote in reply number “5”.

I have two mcs to start/stop the sound and everything works fine.

But I dont know how to make the sound start (fade in) when  
you enter the page. As it is now I have to click the  
"sndBtn\_mc" to make it start ?

---

<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:** [August 15, 2003, 11:47am UTC](https://forum.kirupa.com/t/fade-in-sound/26493/12 "2003-08-15T11:47:36Z")

</div>

Hoi

hope you don’t mind you anwsering the post kax…😛

here i have make the start and stop function los from the onPress of the the buttons so now you can stop and start the sound with the  
startSndObj() and stopSndObj() functions

```auto

sndObj = new Sound();
sndObj.attachSound("loop");
sndObj.setVolume(0);
sndObj.fadeIn = function(speed) {
        var vol = this.getVolume()+speed;
        this.setVolume(vol);
        display = this.getVolume();
        if (this.getVolume()>100) {
                this.setVolume(100);
                clearInterval(this.interval);
        }
};
sndObj.fadeOut = function(speed) {
        var vol = this.getVolume()-speed;
        this.setVolume(vol);
        display = this.getVolume();
        if (this.getVolume()<0) {
                this.setVolume(0);
                this.stop();
                this.playing = false;
                clearInterval(this.interval);
        }
};
startSndObj = function(){
        if (!sndObj.playing) {
                sndObj.start(0, 9999);
                clearInterval(sndObj.interval);
                sndObj.interval = setInterval(sndObj, "fadeIn", 1000/12, 1);
                sndObj.playing = true;
        }
};
stopSndObj = function(){
        if (sndObj.playing) {
                clearInterval(sndObj.interval);
                sndObj.interval = setInterval(sndObj, "fadeOut", 1000/12, 1);
        }
};
//------------------------------------------------------
startSndObj();//calls the start function
//asigns the onPress's to thair function
sndBtnOn_mc.onPress = startSndObj; 
sndBtnOff_mc.onPress = stopSndObj ;

```
