troubleshooting Question

Control multiple fades on multiple sound objects?

Avatar of Kre8
Kre8 asked on
Adobe Flash
3 Comments1 Solution243 ViewsLast Modified:
Hi,

Im using the following actionscript to add fades to the standard sound object...

/* -------- start Sound Object Fade ActionScript extension --------
Sound Object Fade ActionScript extension v 1.0
Copyright Hayden Porter 2003.

Note: Flash 5 backward compatibility requires a simple looping 2 frame mc in library to execute fades.
MC must have linkage ID "soundfader" and be in the same SWF as the sound object. The
extension attaches this mc for each fade and removes the clip after fade. For Flash 5 player compatibility,
you must save the SWF containing this script in Flash 5 format.

//soundfader frame 1
so.changeSoundValue();
//soundfader frame 2 - loop back
gotoAndPlay(1);
*/

/*
fade Sound parameter up or down by fadeAmount on each loop of movieclip
*/
function changeSoundValue(){
      this.elapsedTime = getTimer() - this.lastTimerCheck; // time from last loop to now

      /* fadeAmount adjusts to varying frame rate */
      this.fadeAmount = this.elapsedTime * this.changePerMs;
      this.fadeAmount = (this.fadeDir == "decrease") ? this.fadeAmount * -1 : this.fadeAmount;
      this.newVal = Math.round(this.getSoundControlValue() + this.fadeAmount);

      /* prevent final newVal from being greater than or less than endVal */
      if(this.fadeDir == "increase" && this.newVal >= this.endVal ||
            this.fadeDir == "decrease" && this.newVal <= this.endVal) {
            this.newVal = this.endVal;
      }

      this.lastTimerCheck = getTimer(); // timestamp this loop
      this.setSoundControlValue(this.newVal); // assign sound value
      this.onFadeChange(this.newVal); // execute callback for each change in fade value, pass newVal to assigned function
      
      if(this.newVal == this.endVal) { // fade complete
            if(this.reverseFade){ // oscilate fade
                  this.setFadeParams(this.endVal,this.startVal,this.fadeTime); // restart fade with values reversed
            } else { // execute fade complete callback and terminate loop
                  if(this.stopSound){
                        this.stop(); // stop the sound for fadeToStop()
                        if(this.soundControl == "volume"){
                              this.setVolume(this.startVal); // <-- reset to volume start value
                        }
                  }
                  this.onFadeComplete(); // execute callback at completion of fade
                  _root[this.mcname].removeMovieClip(); // delete loop
            }
      }  
}

/*
attach movie clip with linkage ID "soundfader"
*/
function attachFaderClip(){
      mcLinkageID = "soundfader";
      // find first empty depth
      var d = 9999; while(typeof _root[mcLinkageID + d] == "movieclip"){d--;}
      this.mcname = mcLinkageID + d;
      if(!_root.createEmptyMovieClip){ // attach mc for Flash 5 compiled SWF and player
            _root.attachMovie(mcLinkageID, mcLinkageID + d,d);
            _root[mcLinkageID + d].so = this; // <--
      } else { // create emptymc for Flash 6+ compiled SWF and player
            _root.createEmptyMovieClip(mcLinkageID + d,d);
            _root[mcLinkageID + d].so = this; // <--
            _root[mcLinkageID + d].onEnterFrame = function(){if(!_root[mcLinkageID + d].so.isFadePaused){_root[mcLinkageID + d].so.changeSoundValue();}}
      }
}

/* ======== extension methods ========= */

/*
start at current volume or pan and fade to given value over time in milliseconds
optional 3rd argument to specify pan or volume fade, defaults to volume.
*/
function fadeTo(endval,time){
      this.soundControl = (arguments[2] == null) ? "volume" : arguments[2].toLowerCase();
      if(this.soundControl == "pan"){
            startval = this.getPan(); // <--
      } else if(this.soundControl == "volume"){
            startval = this.getVolume(); // <--
      } else {
            return; // undefined control
      }
      this.stopSound = false;
      this.reverseFade = false;
      this.setFadeParams(startval,endval,time);
      this.attachFaderClip();
}

/*
start a current volume or pan, fade to given value over time
in milliseconds and stop sound at end of fade.
optional 3rd argument to specify pan or volume fade, defaults to volume.
*/
function fadeToStop(endval,time){
      this.soundControl = (arguments[2] == null) ? "volume" : arguments[2].toLowerCase();
      if(this.soundControl == "pan"){
            startval = this.getPan(); // <--
      } else if(this.soundControl == "volume"){
            startval = this.getVolume(); // <--
      } else {
            return; // undefined control
      }
      this.stopSound = true;
      this.reverseFade = false;
      this.setFadeParams(startval,endval,time);
      this.attachFaderClip();
}

/* return "volume" or "pan" fade type */
function getFadeType(){
      return this.soundControl;
}

/*
automatically oscillate between starting value and end value over
time in milliseconds, starting value defaults to volume or pan at start
of oscilation. optional 3rd argument to specify pan or volume fade, defaults to volume.
*/
function oscillateFade(endval,time){
      this.soundControl = (arguments[2] == null) ? "volume" : arguments[2].toLowerCase();
      if(this.soundControl == "pan"){
            startval = this.getPan(); // <--
      } else if(this.soundControl == "volume"){
            startval = this.getVolume(); // <--
      } else {
            return; // undefined control
      }
      this.stopSound = false;
      this.reverseFade = true;
      time = Math.round(time/2); // 1/2 of oscillation period
      this.setFadeParams(startval,endval,time);
      this.attachFaderClip();
}

/*
intitialize fade parameters: start value, end value and time in milliseconds
*/
function setFadeParams(sv,ev,t){
      this.startVal = parseFloat(sv); this.endVal = parseFloat(ev); this.fadeTime = t;
      this.fadeDir = (this.startVal > this.endVal)? "decrease" : "increase";
      this.changePerMs = Math.abs(this.startVal - this.endVal)/this.fadeTime;
      this.lastTimerCheck = getTimer(); //check the current time.
      if(this.soundControl == "pan"){
            this.getSoundControlValue = this.getPan; // <--
            this.setSoundControlValue = this.setPan; // <--
      } else {
            this.getSoundControlValue = this.getVolume; // <--
            this.setSoundControlValue = this.setVolume; // <--
      }
}

/*
pause, resume and stop a fade. State arugment can be "pause", "resume" or "stop"
*/
function setFadeState(state){
      if(state == "pause"){ // pause fade loop but do not terminate
            _root[this.mcname].stop(); // stop attached mc timeline loop
            this.isFadePaused = true; // stop empty mc enterframe loop
      } else if (state == "resume"){ // resume fade at last paused point
            this.lastTimerCheck = getTimer();
            _root[this.mcname].play(); // play attached mc timeline loop
            this.isFadePaused = false; // play empty mc enterframe loop
      } else if (state == "stop"){ // terminate fade loop
            _root[this.mcname].removeMovieClip(); // delete movie clip
            this.isFadePaused = false; // stop empty mc enterframe loop
      } else {
            return; // unknown state
      }
}

// documented fade control Sound object methods
Sound.prototype.fadeTo = fadeTo;
Sound.prototype.fadeToStop = fadeToStop;
Sound.prototype.getFadeType = getFadeType;
Sound.prototype.onFadeChange = null;
Sound.prototype.onFadeComplete = null;
Sound.prototype.oscillateFade = oscillateFade;
Sound.prototype.setFadeParams = setFadeParams;
Sound.prototype.setFadeState = setFadeState;

// utility methods not generaly exposed
Sound.prototype.changeSoundValue = changeSoundValue;
Sound.prototype.attachFaderClip = attachFaderClip;

/* -------- end Sound Object Fade ActionScript extension -------- */


Which works fine initially, however when i try to fade more than 1 sound at a time or multiple fades close to each other in the timeline then all hell breaks loose, lol. Other sounds that literally have no right ot be playing start playing, sounds that should have been faded out and stopped start up again and basically nothing really works as it should:(

I've uploaded the FLA to http://www.kre8webdesign.com/temp/sound.fla and the SWF to http://www.kre8webdesign.com/temp/sound.swf , if anybody can help, that would be greatly apprecitated:)
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 3 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros