Sounds




Here i will show you how to make sounds in flash (AS2 and AS3)

Navigation


Getting started

Sounds are very important in flash game. Getting them into the game is easy,  but playing them isnt easy as it seems. Its much easier in AS2 than in AS3.


AS2

To create a sound object in AS2 isnt very hard, and playing it, too:
var my_sound = new Sound(movieclip)
/*The movieclip is the clip where it stored, default is _level0. It causes problems
when the swf is loaded into an other swf.*/

//Attaching sound
my_sound.attachSound("name")

//Playing sound
my_sound.start(offset, loops)
//You need to enter an offset in secods and how often the sound should play.

//Stopping sound
my_sound.stop()

//Setting volume
my_sound.setVolume(volume)
//All sounds which have the same movie clip have the same volume!
This a very simple example for an AS2 preloader. You can add a loading bar or other stuff.


AS3

Now, AS2 vas very easy. The sound in AS3 is harder than in AS2. Making it play is easy,  but stopping not. This time we will need a SoundChannel.
package{
    import flash.media.Sound;
    import flash.media.SoundChannel;
    import flash.display.MovieClip;
    public class Main extends MovieClip{
        private var my_sound_channel:SoundChannel;
        public function Main():void{
            var _sound:Sound = new My_Sound();
            //Playing a sound:
            this.my_sound_channel = _sound.play();

            //Changing volume:
            this.my_sound_channel.soundTransform.volume = 0.5

            //Stopping sound:
            this.my_sound_channel.stop()
            return;
        }
}
That was now hard. We need a SoundChannel and SoundTransform in AS3, more work than in AS2.

You also may look there: Sound, SoundChannel, SoundTransform




This is a short class which i wrote to make the work easier:
package{

    import flash.media.SoundChannel;
    import flash.media.Sound;
    import flash.media.SoundTransform;

    public class Custom_Sound extends Object{

        private var attached_sound:Sound = null;
        private var sound_channel:SoundChannel = null;

        public function Custom_Sound(_sound:Sound=null):void{
            this.attached_sound = _sound;
            return
        }

        public function attach(_sound:Sound):Boolean{
            try{
                this.stop();
                this.attached_sound = _sound;
                return true;
            }catch(e:*){
                trace(e);
            }
            return false;
        }

        public function play(startTime:Number = 0, loops:int = 0, sndTransform:SoundTransform = null):Boolean{
            if(this.sound_channel==null){
                this.sound_channel = this.attached_sound.play(startTime, loops, sndTransform);
                return true;
            }else{
                return false;
            }
        }

        public function stop():Boolean{
            if(this.sound_channel==null){
                return false;
            }else{
               this.sound_channel.stop();
               this.sound_channel = null;
            }
            return true
        }

        public function get Sound_Channel():SoundChannel{
            return this.sound_channel;
        }
        public function get Sound_Transform():SoundTransform {
            if(this.sound_channel == null){
                return null;
            }
            return this.sound_channel.soundTransform;
        }

        public function set Sound_Transform(param:SoundTransform):void {
            if(this.sound_channel == null){
                return;
            }
            this.sound_channel.soundTransform = param;
            return;
        }
    }
}