Sounds fade in/out with ActionScript 3
I am trying to make a fade in/out in a music in a Flash (CS5) project. I imported the sound to library, set a classname for "Export for ActionScript", and I was trying to fade with TweenLite/TweenMax, like this:
var sound = new MySound();
sT = new SoundT开发者_运维问答ransform(0.1);
sound.play(0,99999, c_sndEnvironment);
TweenLite.to(sound, 1, {volume: 1.0});
But it just doesn't work. I tried to import the volume plugin on TweenLite, and still nothing. I got no error at all though.
Am I doing anything wrong?
Plus, is there any good (complete) AS3 library for music?
Thank you. :)
I use TweenMax for this , it's pretty straightforward
var someSound:Sound = new Sound(new URLRequest(“MySound.mp3″)); var someChannel:SoundChannel = someSound.play(0, 99999); TweenMax.to(someChannel, 1, {volume:0, onComplete:stopSound});
http://www.greensock.com/tweenmax/
PatrickS is correct about the fact that you should tween the volume of the SoundChannel, not the Sound itself. TweenMax automatically activates the VolumePlugin (along with several others), but you can do that manually for TweenLite like:
import com.greensock.*;
import com.greensock.plugins.*;
TweenPlugin.activate([VolumePlugin]); //only necessary once
var someChannel:SoundChannel = someSound.play(0, 99999);
TweenLite.from(someChannel, 1, {volume:0});
For what it's worth, you might also want to check out LoaderMax which has an MP3Loader class that makes working with external sounds easier. It has its own "volume" property that you can tween too. http://www.greensock.com/loadermax/
sorry, I kind kind of a strange behaviour from these lines of code. My sound fades out, and yoyos back. after the soundchannel is at the same volume as before, onComplete is executed normally. Any ideas?
themeChannel = sndTheme.play(0, 99999);
TweenLite.from(themeChannel, 2, {volume:0,onComplete:stopTheme});
//edit: I got it working by Tweening a SoundTransform Object:
var themeTransform:SoundTransform = new SoundTransform(1);
themeChannel = sndTheme.play(0, 99999, themeTransform);
TweenLite.from(themeTransform, 3, {volume:0,onUpdate:updateSound,onComplete:stopTheme});
function updateSound():void{
themeChannel.soundTransform = themeTransform;
}
thanks to: http://www.zedia.net/2008/fading-out-volume-using-tweenlite/
精彩评论