Link to home
Start Free TrialLog in
Avatar of clintnash
clintnashFlag for United States of America

asked on

Sound - crossfade between two loops

I have an simple banner flash, It has a loop that plays through the first part 3/4 of the animation, at that point, I want to fade out the first loop while fading in the other.  I have managed to accomplish a fade out using.

firstSound.setVolume(15);

and decreasign the volume in a series of keyframes, however when I try to add get the other loop to start fading in before the other is done, its just doesn't work.

In the first frame of a layer called Sound level 1 I call this.

firstSound=new Sound();
firstSound.attachSound("0274Full");
firstSoundVolume=100;
firstSound.setVolume(firstSoundVolume);

In the second frame of a seperate layer called background I call this...

if (playing!=true) {
_root.firstSound.start(0,999);
playing=true;
}

This works just fine, In a 3rd layer called Sound Level 2 I tried changing the attached sound like so..
firstSound=new Sound();
firstSound.attachSound("0166Full");
firstSoundVolume=0;

And then in the 1st layer bring the volume back up, that didn't work, nor did creating a new layer and creating a new sound object call secondSound...

any advice on smoothly transitioning between loops is greatly appreciated, my experience level is slightly above beginner.

thanks,
clint...

Avatar of sam85281
sam85281

Well you can't really do an actual crossfade because the proccesses argue with each other, need to do one at a time:
This is how to fade the first sound and make the second fade in as soon as the first finishes fading out:

First Frame Actions:

vol1 = 100;
vol2 = 0;
speed = 2; // The higher the number, the faster the transition
firstSound = new Sound();
firstSound.attachSound("0274Full");
secondSound = new Sound();
secondSound.attachSound("0166Full");

function fadeOut() {
      vol1 -= speed;
      firstSound.setVolume(vol1);
      if (vol1 <= 0) {
            done = 1;
            firstSound.setVolume(0);
            firstSound.stop();
            secondSound.start(0, 9999);
            secondSound.setVolume(0);
            clearInterval(fade1);
            
      }
}
function fadeIn() {
      if (done == 1) {
            vol2 += speed;
            secondSound.setVolume(vol2);
            if (vol2 >= 100) {
                  secondSound.setVolume(100);
                  clearInterval(fade2);
            }
      }
}

function startFirstSound() {
      firstSound.start(0, 9999);
      firstSound.setVolume(100);
}
startFirstSound();



Then on the timeline where you want the fade out to begin:

fade1 = setInterval(fadeOut, 100);
fade2 = setInterval(fadeIn, 100);

If the transition is too slow, adjust the speed variable to a higher number.

-Sam
ASKER CERTIFIED SOLUTION
Avatar of Cerf
Cerf

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial