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.

, , ,

  1. #1 by Adrian Parr - April 7th, 2008 at 04:33

    Hi,

    Just to let you know that you’ve lost a space between ‘new’ and ‘MySound();’ in your first code example.

    var newSound:MySound = newMySound();

    Beginners might get confused if you leave it as it is.

    Kind regards,

    Adrian

  2. #2 by dgelineau - April 7th, 2008 at 21:40

    Thanks for pointing it out, I also changed the name of all my variables to avoid confusion.

    Also thanks for putting me on your blogroll!

  3. #3 by TIMNHE - April 22nd, 2008 at 07:43

    Hi, I´m detect a little bug in the Sound.play method.
    I have a code, that can load, play, stop and pause an external mp3 sound file, but when I want to pause and restart my channel, it never sinchronizes correctly.
    An example:

    My Audio file has this conversation:
    Word1 word2 word3 word4 word5 word6 word7 word8 …

    If I play my sound, and soon after, I pause the sound before de ”word3”, when I press the play button again, I can ear the finish of the ‘word 4′ or the principle of the ‘word 5′ … what happen whit word 3??

    it´s a bug??
    thanks ;)

  4. #4 by Uday - June 25th, 2008 at 06:03

    It’s a best example for play audio file.

    var numOfTime:Number = 10
    var snd:Sound = new Sound();
    snd.load(new URLRequest(“audio/uday.mp3″));
    snd.play(0, numOfTime);

    Thanks,
    Uday
    uday_sgh(et)yahoo.com

(will not be published)
Subscribe to comments feed