Posts Tagged Sound

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 intermediate Object.

, , , ,

24 Comments


A little bit about Sound | Using the Sound, SoundChannel, SoundTransform classes

I didn’t really mess up with sounds in ActionScript 3 until recently. I can say that it changed a lot since ActionScript 2, it’s a bit more complex, but I’m pretty sure it gives more possibilities also.

First, you can either load the sound from the library, or you can load it externally. If it is from the library, you first have to click export for ActionScript in the property panel from that sound, and use the class name you gave it. Let say you gave it “MySound” as class name here is how you would simply make it play:

1
2
var someSound:MySound =  new MySound();
someSound.play();

If you want to load your sound from an external file it is a bit different but afterward it’s all the same:

1
2
3
4
5
6
7
var someSound:Sound = new Sound(new URLRequest("MySound.mp3"));
someSound.play();
 
//it could also be done like this
var someSound:Sound = new Sound();
someSound.load(new URLRequest("MySound.mp3"));
someSound.play() ;

In the last example, the file MySound.mp3 has to be in the same folder as the HTML embedding the SWF because that what I indicate in the URLRequest.

If you use only the Sound class the only thing you can do is make the sound play, that’s it, no stop, no pause, no adjustment to the volume. For that you will have to use two other classes SoundChannel and SoundTransform.

SoundChannel

The SoundChannel class possess only one method and it’s the stop method. I guess you know what it does. here is how you use it:

1
2
3
4
var someSound:Soundnew Sound(new URLRequest("MySound.mp3"));
var someChannel:SoundChannel = new SoundChannel();
someChannel = someSound.play();
someChannel.stop();

The last bit of code is actually useless because it will stop the sound right after it started it but it is only to show how to use a SoundChannel. Fine but how do you pause a sound and than make it play again with only a stop() method? The SoundChannel class has a property called position which indicate where the play head is at. You have to save the value of the position property before calling the stop method. If you save to position property value after calling the stop method, it will be of value 0 which is of no interest. What you do with the value of the position property is you pass it as a parameter to the Sound.play() method. The first parameter, startTime, tell the Sound Object at which millisecond to start playing. It’s a bit confusing but this example should make it clearer:

1
2
3
4
5
6
7
8
var someSound:Soundnew Sound(new URLRequest("MySound.mp3"));
var someChannel:SoundChannel = new SoundChannel();
var somePosition:Number;
 
someChannel = someSound.play();//this make the sound play
somePosition = someChannel.position; //saves the value of the position property
someChannel.stop(); //pause or stop the sound
someChannel = someSound.play(somePosition);//this make the sound play but it now start from the position at which it was stopped

The Sound.play() method also takes two other optional parameters. The first of the two is the loops parameter, it indicate how many times you want the sound to play in loop. If you want your sound to loop forever you just pass a really big number as parameter. if you want it to play just once you pass it 0. The second of the two (the third parameter really) is the SoundTransform parameter.

SoundTransform

The SoundTransform class as some properties and no methods, but I will only speak about the volume property here. The volume property is a number ranging from 0 to 1, 0 being silent and 1 being at full volume. You can either set it in the constructor or after the SoundTransform has been created. here is how you would make a sound play at half the maximum volume:

1
2
3
4
5
var someSound:Soundnew Sound(new URLRequest("MySound.mp3"));
var someChannel:SoundChannel = new SoundChannel();
var someTransform:SoundTransfrom = new SoundTransform(0.5); // the 0.5 value here tells it to set volume to half the maximum value
 
someChannel = someSound.play(0, 0, someTransform);//the first 0 is startTime, the second is the number of loop, and finally we pass our SoundTransform

If we wanted to change the volume as the sound is playing we would do it in this way:

1
2
3
4
5
6
7
var someSound:Soundnew Sound(new URLRequest("MySound.mp3"));
var someChannel:SoundChannel = new SoundChannel();
var someTransform:SoundTransfrom = new SoundTransform(0.5); // the 0.5 value here tells it to set volume to half the maximum value
 
someChannel = someSound.play(0, 0, someTransform);
someTransform.volume = 1;
someChannel.soundTransform = someTransform;

This would start playing the the sound at half the volume to set it afterward at maximum volume. Well that is as far as I go with sound, with that you can do most of the common tasks. In a future post I will show how to use TweenLite to fade the volume to zero.

, , ,

18 Comments