Thursday, August 12, 2010

Playing and Muting an Audio File in Flash with Actionscript 3.0

var audioLink01:URLRequest = new URLRequest("DanceOfTheMist.mp3");
var sound01:Sound = new Sound();
var soundChannel01:SoundChannel;
var soundPausedPos:int;
var soundIsPlaying:Boolean;

this.loaderInfo.addEventListener(Event.COMPLETE, init);

function init(e:Event) {
//trace("The init function has run");
sound01.load(audioLink01);
mute_mc.addEventListener(MouseEvent.CLICK, muteSounds);
play_mc.addEventListener(MouseEvent.CLICK, playSounds);

}

function playSounds(e:MouseEvent) {
if(soundIsPlaying == true) {

}else{
soundChannel01 = sound01.play(soundPausedPos);
soundIsPlaying = true;
soundChannel01.addEventListener(Event.SOUND_COMPLETE, updateSoundStatus);
}
}

function muteSounds(e:MouseEvent) {
soundPausedPos = soundChannel01.position;
soundChannel01.stop();
soundIsPlaying = false;
}
function updateSoundStatus(e:Event) {
soundIsPlaying = false;
}