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:
Actionscript:
-
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:
Actionscript:
-
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 konw what it does. here is how you use it:
Actionscript:
-
var someSound:Sound = new 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:
Actionscript:
-
var someSound:Sound = new 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:
Actionscript:
-
var someSound:Sound = new 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:
Actionscript:
-
var someSound:Sound = new 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.