Thursday, January 21, 2010

Play sound as a LOOP in AS3

//In AS3 an event Event.SOUND_COMPLETE that fires when the sound finishes, so when that fires you should start the sound again, and your sound will be play as a loop.


var sndReq:URLRequest=new URLRequest("Assets/Audio/drums01.mp3");
var snd:Sound = new Sound();
var channel:SoundChannel = new SoundChannel();
snd.load(sndReq);
soundOn.addEventListener(MouseEvent.CLICK, playSound);
soundOff.addEventListener(MouseEvent.CLICK, stopSound);
function playSound(event:MouseEvent):void {
channel=snd.play();
channel.addEventListener(Event.SOUND_COMPLETE,loopSound);
}
function loopSound(event:Event) {
playSound();
}
function stopSound(event:MouseEvent):void {
channel.stop();
}

No comments:

Post a Comment