Home Tutorials Forums Articles Blogs Movies Library Employment Press Buy templates

Go Back   ActionScript.org Forums > ActionScript Forums Group > ActionScript 3.0

Reply
 
Thread Tools Rate Thread Display Modes
Old 11-03-2007, 12:30 AM   #1
seven
Member
 
Join Date: Sep 2007
Posts: 71
Default Fading sound using AS3

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:

Code:
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
seven is offline   Reply With Quote
Old 11-03-2007, 12:42 AM   #2
Rossman
Senior Member
 
Rossman's Avatar
 
Join Date: Feb 2004
Location: Toronto, Ontario
Posts: 1,299
Send a message via ICQ to Rossman
Default

Run a tween, and on each tween update, turn the volume down by the tweened volume amount?
__________________
the beers just sit there, silently staring, and mocking the mistakes my animal mind makes sometimes - Spyre
Rossman is offline   Reply With Quote
Old 11-03-2007, 12:46 AM   #3
seven
Member
 
Join Date: Sep 2007
Posts: 71
Default

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?
seven is offline   Reply With Quote
Old 11-03-2007, 03:30 AM   #4
matbury
What's up pussycat?
 
matbury's Avatar
 
Join Date: Dec 2006
Location: Barcelona, Spain
Posts: 1,486
Send a message via Skype™ to matbury
Default

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!
__________________
http://matbury.com/
matbury is offline   Reply With Quote
Old 11-03-2007, 03:39 AM   #5
seven
Member
 
Join Date: Sep 2007
Posts: 71
Default

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:

Code:
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
seven is offline   Reply With Quote
Old 11-03-2007, 03:50 AM   #6
Rossman
Senior Member
 
Rossman's Avatar
 
Join Date: Feb 2004
Location: Toronto, Ontario
Posts: 1,299
Send a message via ICQ to Rossman
Default

In this line:

ActionScript Code:
var soundFadeInTimer:Timer = new Timer(1000,1);

You need to increase the number of times the timer will execute, ie:

ActionScript Code:
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:

ActionScript Code:
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...
__________________
the beers just sit there, silently staring, and mocking the mistakes my animal mind makes sometimes - Spyre

Last edited by Rossman; 11-03-2007 at 03:53 AM..
Rossman is offline   Reply With Quote
Old 11-03-2007, 05:31 AM   #7
seven
Member
 
Join Date: Sep 2007
Posts: 71
Red face

Thank you Ross:

It doesn't quite work yet, but here is my code:

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 is offline   Reply With Quote
Old 11-03-2007, 09:27 AM   #8
seven
Member
 
Join Date: Sep 2007
Posts: 71
Default Newest Code...still doesn't work

Ok, Here is my latest code:

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??

Last edited by seven; 11-03-2007 at 09:29 AM.. Reason: added info
seven is offline   Reply With Quote
Old 11-03-2007, 02:23 PM   #9
Rossman
Senior Member
 
Rossman's Avatar
 
Join Date: Feb 2004
Location: Toronto, Ontario
Posts: 1,299
Send a message via ICQ to Rossman
Default

You aren't assigning the SoundTransform to your SoundChannel object?

ie:
ActionScript Code:
function soundFadeIn(e:TimerEvent){     fadeInIncr += 1/30;         var sAmbienceVol:SoundTransform = new SoundTransform(fadeInIncr, 0);         channel.soundTransform = sAmbienceVol;     trace(fadeInIncr); }
__________________
the beers just sit there, silently staring, and mocking the mistakes my animal mind makes sometimes - Spyre
Rossman is offline   Reply With Quote
Old 11-03-2007, 04:36 PM   #10
seven
Member
 
Join Date: Sep 2007
Posts: 71
Default It works!

Thanks Ross! That worked
seven is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic Sound Fading greenpear ActionScript 2.0 3 10-26-2007 02:38 PM
Kenny Bellew Sound Object Tutorial Updated Kennyb ActionScript 2.0 23 05-09-2007 09:38 PM
[stuff] as3 sound visualising efx arrFoo ActionScript 3.0 0 02-01-2007 10:53 AM
[F8] Fading out specific sound while keeping others active Trinidad ActionScript 2.0 2 07-14-2006 06:26 PM
Control Sound Object puthisorn ActionScript 2.0 9 05-06-2004 05:20 AM


All times are GMT. The time now is 03:12 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2009 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.