Function to fade audio smoothly from one level to another in AS2
PeteGould Jan 4, 2012 9:40 AMGreetings all.
I'm a newcomer to Actionscript programming and trying to modify a Flash site template downloaded from TemplateMonster.com (which is an AS2 template). I've almost succeeded at what I need to do but have run into a couple of brick walls. One is that I've added a video player to the site and need to make the background music track smoothly fade out when the video starts and fade back in when it ends (or is stopped). I set up a listener object for the video player that works. It's the smooth fade of audio levels that doesn't.
I found an excellent thread from last year (http://forums.adobe.com/message/3236495) in which kglad address the issue of fading audio from one level to another. Since TemplateMonster's templates set up a master movie clip and then load pages to play within it, and I need to call the function from within the pages, I tried setting it up as a global function.
So when the overall site initializes, I have this:
//---------------------------------------------------------------------------------------- -------------
this.createEmptyMovieClip("mcMusictrackHolder", this.getNextHighestDepth());
var sndAudio:Sound = new Sound(mcMusictrackHolder);
var nMaxMusicVolume:Number = new Number(mcMusictrackHolder);
nMaxMusicVolume = 30;
sndAudio.attachSound("MusicTrack");
sndAudio.setVolume(nMaxMusicVolume);
sndAudio.start(0,9999);
// Here is kglad's function converted to a global function
_global.fadeSoundF = function(mc:MovieClip,s:Sound,vol:Number,sec:Number):Void
{
trace("Getting here with sound at " + s.getVolume());
clearInterval(mc.fadeI);
var volumeInc:Number = vol-s.getVolume()/(10*sec);
mc.fadeI=setInterval(fadeF,100,mc,s,volumeInc,vol);
trace("Leaving with sound at " + s.getVolume());
}
function fadeF(mc:MovieClip,s:Sound,inc:Number,endVol:Number):Void
{
s.setVolume(s.getVolume()+inc);
if(Math.abs(s.getVolume-endVol)<inc)
{
clearInterval(mc.fadeI);
}
}
//---------------------------------------------------------------------------------------- -------------
Then within the page that contains the video player (a child of the above), I have this:
//---------------------------------------------------------------------------------------- -------------
var listenerObject:Object = new Object();
var sCurrentState:String;
listenerObject.stateChange = function(eventObject:Object):Void
{
sCurrentState = my_FLVPlybk.state;
if (sCurrentState == "playing")
{
fadeSoundF(_root.mcMusictrackHolder,_root.sndAudio,0,1);
}
else
{
fadeSoundF(_root.mcMusictrackHolder,_root.sndAudio,_root.nMaxMusicVolume,1);
};
};
my_FLVPlybk.addEventListener("stateChange", listenerObject);
//---------------------------------------------------------------------------------------- -------------
The listener object is working fine - so if instead of calling fadeSoundF I simply do _root.sndAudio.setvolume(0) to mute and _root.sndAudio.setvolume(30) to restore it works fine except that the volume changes abruptly instead of ramping.
But obviously there's a problem with my effort to convert kglad's function to global and use it that way, because the trace statements tell me the audio is getting set to random levels. It smoothly ramps all right - but tries to get to -472 or +8212 or other insane numbers.
Any help with where I'm going wrong would be deeply appreciated. Keep in mind that although I have some long-ago programming experience in other languages, this environment is completely alien to me and you can feel free to assume I'm completely ignorant. What I've managed to piece together is largely pulled from online research, which is why there's probably an obvious glaring error in there. Feel free to provide a response that assumes I know nothing.
Best,
Pete


