• i believe in what i do •
sirimalla_savechild_1098

July 2013 Archives

ActionScript 3 - Sound Control

| 0 Comments | 0 TrackBacks
Sample AS3 code to play the audio (sound) file from library for background loop and controlling the audio.
var MySound:Sound = new MySound(); // audio file in Library 
var soundChannel:SoundChannel;
var playLoop:Boolean = true;

function playSound(e:MouseEvent):void
{
	soundChannel = MySound.play();
	if(playLoop){
		soundChannel.addEventListener(Event.SOUND_COMPLETE, onComplete);
	}
}

function onComplete(event:Event):void
{
	SoundChannel(event.target).removeEventListener(event.type, onComplete);
	playSound();
}

function stopSound(e:MouseEvent):void
{
	SoundMixer.stopAll()
}

function setVolume(volume:Number):void
{
	var transform:SoundTransform = soundChannel.soundTransform;
	transform.volume = volume;
	soundChannel.soundTransform = transform;
	
}

function muteSound(e:MouseEvent):void
{
	SoundMixer.soundTransform = new SoundTransform(0);
}

function unMuteSound(e:MouseEvent):void
{
	SoundMixer.soundTransform = new SoundTransform(1);
}