Groups | Blog | Home
all groups > flash actionscript > april 2004 >

flash actionscript : Audio Actionscript


tralfaz
4/28/2004 3:27:50 PM
[quoted text, click to view]

Here is some code for fade up/down using two buttons
tralfaz

// frame 1 code
onLoad = function()
{
stop();
music = new Sound();
music.attachSound("backsound");
music.start(0, 999999);
music.setVolume(0);
vol = 0;
step = 3;
_root.fade = setInterval(fadeSound, 100);
}

function fadeSound()
{
vol += step;
music.setVolume(vol);
_root.volume.text = vol;

if ((vol > 99) || (vol < 1) )
clearInterval(_root.fade);
}
// end of frame 1 code

// code for fade up button
on (release)
{
step = 3; // increase volume
_root.fade = setInterval(fadeSound, 100);
}

// code for fade down button
on (release)
{
step = -3; // increase volume
_root.fade = setInterval(fadeSound, 100);
}

Peter Fettes
4/28/2004 8:01:22 PM
Please Help !

I'm wanting to create a audio feature to my movie, which fades-in the audio at
the start of my movie.

But is also controlled by two buttons.

1. One which fades the audio-out.

2. And another which fades the audio-in.

The Actionscript I'm currently using works as such:
One layer contains the actionscript below.

// initiate sound
stop();
music = new Sound();
music.attachSound("backsound");
music.start(0, 999999);
// set the volume of the sound to zero
music.setVolume(0);
// set a variable named 'vol'
vol = 0;
// set another variable named 'fade', putting a setInterval function in it
fade = setInterval(fadeIn, 100);
// set the initial fade in function
function fadeIn() {
// fade the sound in with an increment of 3 in the variable 'vol'
vol += 3;
music.setVolume(vol);
// put an if condition to restrict the increment in volume after it reaches
to 100
if (vol>=100) {
clearInterval(fade);
// create the 'step' variable
step = 1;
// create the 'fade' variable
Fade = 0;
}
}
// create the fade in and out function
// function executed on onEnterFrame
_root.onEnterFrame = function() {
// set fade out
if (Fade == 1) {
vol = vol-step;
if (vol<0) {
vol = 0;
}
music.setVolume(vol);
// set fade in
} else {
vol = vol+step;
if (vol>100) {
vol = 100;
}
music.setVolume(vol);
}
};

While a single button, controls the fading-in / and the fading-out of the
Audio.

It's actionscript works as so:
And on the other layer a button is placed which holds the following
actionscript.

on (release) {
// set the function for variable value toggle
(_root.fade=!_root.fade) ? 0 : 1;
}

My question is this, how could I adapt the Actionscript two include two
buttons (1.FADE-IN) (2.FADE-OUT)
in place of a single button?

Or does anyone have an alternative, way of controlling audio via two buttons?.
One which fades the audio-in , the other which fades it out.

NOTE: The audio must fade-in on the start of the movie though.

Kind Regards,
Peter


Peter Fettes
4/29/2004 8:55:24 AM
Thank You

Kind Regards,
alreadytaken
5/20/2004 4:37:13 PM
I used your script exactly as you have it... and it works... kinda. If you
click the up and down buttons too rapidly, say a 3 seconds apart or less... it
fails! The sound starts increasing beyond 100%. Also, if I want to change the
step from -3 to -10, the music never completely fades out, and still encounters
the problem that it looses itself. I believe this is because this is an
interpreted language and if you interupt the script in a previous loop to begin
running in another loop, if fails.

Can you modify and add some traps for this? Thanks. BTW, this is the only code
that worked out of several examples from other sites that I tried. Thanks for
your help.
tralfaz
5/21/2004 11:35:58 AM
[quoted text, click to view]


Yes, you are right. I didn't test it enough. We can fix it with just one
line on each of the two buttons to clear the interval (whether it needs it
or not) before setting the interval again. It makes sure that two timer
intervals are not running at the same time.
tralfaz

// code for fade up button
on (release)
{
clearInterval(_root.fade); // add this line
step = 3; // increase volume
_root.fade = setInterval(fadeSound, 100);
}

// code for fade down button
on (release)
{
clearInterval(_root.fade); // add this line
step = -3; // increase volume
_root.fade = setInterval(fadeSound, 100);
}

tralfaz
5/29/2004 4:40:55 PM
[quoted text, click to view]

No problema,
tf

alreadytaken
5/29/2004 9:51:26 PM
tralfaz,

Thanks for the fix. I'll add it in the next few days and post back the results.

AddThis Social Bookmark Button