View Full Version : Fading sound using AS3
seven
11-02-2007, 11:30 PM
Ok, I knew how to do this in AS2, when I did a search in this site for this topic..all that came up was as2 tuts...
I already have my streaming audio initialized using:
var sAmbience:Sound = new Sound();
sAmbience.load(new URLRequest("ambience.mp3"));
var sAmbienceVol:SoundTransform = new SoundTransform(1, 0);
However, I want to write a function that will fade the sound in and one that will fade it out...
Any ideas?
Thank youi -D
Rossman
11-02-2007, 11:42 PM
Run a tween, and on each tween update, turn the volume down by the tweened volume amount?
seven
11-02-2007, 11:46 PM
Rossman,
That is an interesting idea, but a tween has only so many arguments, where would I place the sound decrement?
How would I syntax it?
matbury
11-03-2007, 02:30 AM
For something as simple as a single number decrement, it'd be easy just to use a Timer. Look up the Timer class in the documentation, it isn't hard to set up.
One thing, when you want to turn off your timer, make sure you don't define the Timer i.e. var volumeTimer:Timer = new Timer(100, 10); from inside any functions. Otherwise you won't be able to turn it off!
seven
11-03-2007, 02:39 AM
Mat,
Thank you for your response. I already know how to set up a timer object and have it run a function.
It is the "looping" aspect that has got me confused.
here is my timer:
var soundFadeInTimer:Timer = new Timer(1000,1);
soundFadeInTimer.addEventListener("timer", soundFadeIn);
soundFadeInTimer.start()
// now for the function//
function soundFadeIn(e:TimerEvent){
this is where I need help
}
Thanks again
-D
Rossman
11-03-2007, 02:50 AM
In this line:
var soundFadeInTimer:Timer = new Timer(1000,1);
You need to increase the number of times the timer will execute, ie:
var soundFadeInTimer:Timer = new Timer(100,30);
(I've also decreased the timing, as well)
Now it will run 30 times, and you can reduce the volume by 1/30 each time the listener fires.
Like:
var x:Number = 1;
function soundFadeIn(e:TimerEvent){
// this is where I need help
x -= 1/30;
var sAmbienceVol:SoundTransform = new SoundTransform(x, 0);
}
That probably won't actually work, but should give you then idea...
seven
11-03-2007, 04:31 AM
Thank you Ross:
It doesn't quite work yet, but here is my code:
var soundFadeInTimer:Timer = new Timer(100,30);
soundFadeInTimer.addEventListener("timer", soundFadeIn);
soundFadeInTimer.start()
var fadeInIncr:Number = 0.1;
function soundFadeIn(e:TimerEvent){
fadeInIncr += 1/30;
var sAmbienceVol:SoundTransform = new SoundTransform(fadeInIncr, 0);
trace(fadeInIncr)
}
sAmbience.play(0,20,sAmbienceVol)
There was a problem with namespace conflict using "x" as variable, so I changed your "x" to fadInIncr.
Here is the output per trace:
0.13333333333333333
0.16666666666666666
0.19999999999999998
0.2333333333333333
0.26666666666666666
0.3
0.3333333333333333
0.36666666666666664
0.39999999999999997
0.4333333333333333
0.4666666666666666
0.49999999999999994
0.5333333333333333
0.5666666666666667
0.6
0.6333333333333333
0.6666666666666666
0.7
0.7333333333333333
0.7666666666666666
0.7999999999999999
0.8333333333333333
0.8666666666666666
0.8999999999999999
0.9333333333333332
0.9666666666666666
0.9999999999999999
1.0333333333333332
1.0666666666666667
1.1
So we can see that the counter is working, but my volume is not increasing. Any ideas?
seven
11-03-2007, 08:27 AM
Ok, Here is my latest code:
var soundFadeInTimer:Timer = new Timer(100,30);
soundFadeInTimer.addEventListener("timer", soundFadeIn);
soundFadeInTimer.start()
var fadeInIncr = 0.1;
function soundFadeIn(e:TimerEvent){
fadeInIncr += 1/30;
trace(fadeInIncr)
}
var snd:Sound = new Sound(new URLRequest("myLoop.mp3"));
var trans:SoundTransform = new SoundTransform(fadeInIncr, 0); // 1=vol, 0=pan
var channel:SoundChannel = snd.play(0, 1, trans);
However, when the sound plays, it just starts off at the 0.4 initial volume.
Trace Output:
0.13333333333333333
0.16666666666666666
0.19999999999999998
0.2333333333333333
0.26666666666666666
0.3
0.3333333333333333
0.36666666666666664
0.39999999999999997
0.4333333333333333
0.4666666666666666
0.49999999999999994
0.5333333333333333
0.5666666666666667
0.6
0.6333333333333333
0.6666666666666666
0.7
0.7333333333333333
0.7666666666666666
0.7999999999999999
0.8333333333333333
0.8666666666666666
0.8999999999999999
0.9333333333333332
0.9666666666666666
0.9999999999999999
1.0333333333333332
1.0666666666666667
1.1
0.13333333333333333
0.16666666666666666
0.19999999999999998
0.2333333333333333
0.26666666666666666
0.3
0.3333333333333333
0.36666666666666664
0.39999999999999997
0.4333333333333333
0.4666666666666666
0.49999999999999994
0.5333333333333333
0.5666666666666667
0.6
0.6333333333333333
0.6666666666666666
0.7
0.7333333333333333
0.7666666666666666
0.7999999999999999
0.8333333333333333
0.8666666666666666
0.8999999999999999
0.9333333333333332
0.9666666666666666
0.9999999999999999
1.0333333333333332
1.0666666666666667
1.1
The sound does not fade in. Any Ideas??
Rossman
11-03-2007, 01:23 PM
You aren't assigning the SoundTransform to your SoundChannel object?
ie:
function soundFadeIn(e:TimerEvent){
fadeInIncr += 1/30;
var sAmbienceVol:SoundTransform = new SoundTransform(fadeInIncr, 0);
channel.soundTransform = sAmbienceVol;
trace(fadeInIncr);
}
seven
11-03-2007, 03:36 PM
Thanks Ross! That worked
RealitysFugitive
08-01-2008, 03:43 PM
I need a little help. I used your code with my animation, (changing all the variable names so it would work) and it works initially. However, there's a point in the animation where I pause the sound for a few frames and then restart it. The code I have for the frame it starts up again is this:
myChannel = myAudio.play(myPausePosition, 1, trans)
(Where "trans" refers to the same variable as before) But it doesn't fade the sound in again. It just resumes it, but with a much quieter volume. I'm pretty new to actionscript, so I can use some help figuring out how to fix this.
Also, I want to fade out the music later, so I just copied your code and changed things around a bit, but it's not working. If someone could tell me how they would do it, that would be a big help too.
vBulletin® v3.7.1, Copyright ©2000-2008, Jelsoft Enterprises Ltd.