Adding Sound Effects to Your Flex/AIR Application


It’s easy to add sounds to a Flex/Air application. Here we’ll see how to add whirrs, chirps, and bloops to your application’s button clicks and mouseovers. We’ll also see how to use Air’s EncryptedLocalStore to add mute and volume controls.

The first step is to create a sound manager for playing the sounds. The sound manager will make it possible to centrally apply user preferences like volume to all application sounds.



    import flash.utils.ByteArray;

    import mx.core.SoundAsset;

    public class SoundManager


        public static const DEFAULT_VOLUME:Number = 3;

        public static function play(sound:SoundAsset):void {

            var sndXfrm:SoundTransform;

            var sndXfrmVal:Number = 0;

            var muteBytes:ByteArray = EncryptedLocalStore.getItem(“muted”);

            var volBytes:ByteArray = EncryptedLocalStore.getItem(“volume”);

            //handle mute condition

            if (muteBytes && muteBytes.readUTFBytes(muteBytes.length) == “true”) {

                //don’t play a sound



            //handle volume condition (should be a val between 1 and 10)

            if (volBytes) {

                sndXfrmVal = parseInt(volBytes.readUTFBytes(volBytes.length));

                if(sndXfrmVal == 0 || isNaN(sndXfrmVal)) {

                    //couldn’t find a valid value

    //in the ELS (the user has not set prefs yet)

                    sndXfrmVal = DEFAULT_VOLUME;


            } else {

                sndXfrmVal = DEFAULT_VOLUME;


            sndXfrm = new SoundTransform(0.1 * sndXfrmVal);

  , 0, sndXfrm);



The SoundManager takes a SoundAsset as an argument to the play function. This object references an embedded sound file.

Now that we have a sound manager, let’s create a special Button for our application that will play sounds. To do this we’ll extend the flex Button component. Users will hear sound.mp3 when they click the button. We could easily expand this class to play sounds for mouseovers as well.


    import mx.controls.Button;

    import mx.core.SoundAsset;

    public class SoundEffectButton extends Button



        private var mouseClickSoundClass:Class;


        public var mouseClickSound:SoundAsset =

   SoundAsset(new mouseClickSoundClass());

        public function SoundEffectButton()




        override protected function clickHandler(event:MouseEvent):void {





Now that we have a button, let’s use it! Note that we are overriding the default sound.mp3 referred to in the SoundEffectButton class with a short burst of ACDC rock and roll.



            import mx.controls.Alert;

            import mx.core.SoundAsset;


            private var acdcClass:Class;

            private var acdc:SoundAsset = SoundAsset(new acdcClass());



    <local:SoundEffectButton label=”Test” mouseClickSound=”{acdc}/>

Finally, we want to salute our esteemed users by giving them the option to not be about to rock out to ACDC. The following code lets users turn down the volume by providing a nifty slider. The SoundManager (remember her?) will now know to turn down the volume. We could also add a couple of radio buttons in order to let the user toggle between mute and unmute states.

As you can see, ELS provides a simple means of communicating user preference changes without the trouble of dealing with controllers and explicit file reads.

    <mx:HSlider id=”volume










                var bytes:ByteArray = new ByteArray();


                EncryptedLocalStore.setItem(“volume”, bytes);




1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked