Solved

Control multiple fades on multiple sound objects?

Posted on 2004-03-29
4
214 Views
Last Modified: 2010-05-18
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:)
0
Comment
Question by:Kre8
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 

Expert Comment

by:scotman23
ID: 10715771
Hello,

I had a similar problem trying to get different sounds to fade and overlap but the problem seemed to be trying to control the sound levels of individual movies. I couldn't get it to work. As soon as I started to increase the sound on one clip, the other ones went up as well. The only way I could get one to stop completely was to load it on to its own level and unload the movie once I wanted it to stop.

Not sure if its any use?
0
 

Author Comment

by:Kre8
ID: 10723247
Hi,

I tried the on levels way and couldnt get it to work either:( Though i did finally figure the problem out, each sound objects needs to be constructed with the target to a movie clip for the sound. eg.

s = new sound("sHolder");

Otherwise using simply s = new sound(); makes any volume adjustments effect every other sound in the movie:)

0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 12266607
PAQed, with points refunded (500)

Computer101
E-E Admin
0

Featured Post

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are times in your Flash CS4 application when you want more than a simple pointer or a hand, and it's hard to find an ideal walk-through to tell you what to do.  I spent a few days recently learning my way around making custom cursors in Flash,…
Here are some practices and techniques that can be adopted into your Flash/Flex application development process. Note: Not all "performance tips" provide an immediately-recognizable benefit.   This article does not include timing validation data,…
In this tutorial viewers will learn how to create a basic motion tween animation in Flash Open a new document in Flash: Draw/import an image: Press CTRL + F8 to convert it into a graphic symbol: Select a frame (how long you want the tween to last): …
This Micro Tutorial will teach to how to utilize bit rate in Adobe Flash Media Live Encoder.
Suggested Courses

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question