Fading out volume using TweenLite
I showed in my previous post how to change the volume of a playing sound. In this post I am going to show how to tween that volume using TweenLite.
The class we are going to do the tween on is the SoundTransform class. So we will first have to create our sound objects.
Actionscript:
-
var someSound = new Sound(new URLRequest("MySound.mp3");
-
var someChannel = new SoundChannel();
-
var someTransform = new SoundTransform(1);//volume is set to the max
-
-
someChannel = someSound.play(0, 99999, someTransform);//The sound will start from 0 millisecond, will loop 99999 times and will use the someTransform SoundTransform
-
-
//the next lines are for fading out the volume
-
-
TweenLite.to(someSound, 1, {volume:0, onUpdate:updateChannel, onComplete:stopSound});
-
-
function updateChannel():void{
-
someChannel.soundTransform = someTransform;
-
}
-
-
function stopSound():void{
-
someChannel.stop();
-
}
As you can see, even if you modify the volume parameter of the SoundTranform it won't do a thing. You also have to update the SoundChannel. You can't also tween the SoundChannel.soundTransform.volume directly. It's the same as when you tween colors using Color.interpolateColor, you have to modify an intermidiate Object.
April 7th, 2008 at 11:32 pm
[…] and everything in between « Converting a RGB color and Alpha to ARGB in ActionScript 3 Fading out volume using TweenLite […]
April 8th, 2008 at 12:59 pm
Believe it or not, it’s actually easier than you think to fade the volume using TweenLite. It’ll take care of the intermediate object for you, and it’ll control the volume of any SoundChannel or MovieClip. Your code could be simplified to look like this instead:
var someSound:Sound = new Sound(new URLRequest(”MySound.mp3″));
var someChannel:SoundChannel = someSound.play(0, 99999);
TweenLite.to(someChannel, 1, {volume:0, onComplete:stopSound});
function stopSound():void{
someChannel.stop();
}
stop();
Oh, and by the way, you might want to check out TweenMax - it’s like TweenLite on steroids. I just launched it a few days ago and it includes a bunch of new features. Since it builds on top of TweenLite, it’ll do ANYTHING TweenLite can do, plus a bunch more.
See more at www.TweenMax.com
Thanks again for the great posts on how to more effectively use the TweenLite family of classes.
October 7th, 2008 at 10:05 am
I’m getting a ReferenceError: Error #1056: Cannot create property volume on flash.media.SoundChannel.