Fade in Sound

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]

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

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]

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]

Untested again, I don’t have Flash installed in this PC. :stuck_out_tongue:
[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]

Thanks!

:stuck_out_tongue:

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?

[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]
??

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);?

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

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]

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 ?

Hoi

hope you don’t mind you anwsering the post kax…:stuck_out_tongue:

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


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 ;