Tutorial actionscript3 – Loading sound and play as loop
Un ejemplito de como cargar un sonido y reproducirlo en loop para los que estan empezando. El codigo es bastante sencillo asi que no voy a detallar mucho.
La clase SoundChannel sirve para controlar sonidos, tiene el metodo stop() para detener el sonido y usa una clase SoundTransform para controlar parametros como el volumen o el pan.
La clase Sound es para almacenar el archivo en si. la clase te permite cargar un mp3 y ponerle play.
Todo lo que comento de estas clases es muy por encima, son mucho mas complejas que eso. Pero bueno para los que comienzan con esto del actionscript les puede ser de ayuda.
package { import flash.display.Sprite; import flash.events.Event; import flash.media.Sound; import flash.media.SoundChannel; import flash.net.URLRequest; /** * This class load a sound and play it in loop. * @author http://www.miguelmoraleda.com */ public class Main extends Sprite { private var soundFactory:Sound; private var song:SoundChannel; public function Main() { super(); loadSound("sound.mp3"); } /** * Load the sound * @param url */ private function loadSound(url:String):void { var request:URLRequest = new URLRequest(url); soundFactory = new Sound(); soundFactory.addEventListener(Event.COMPLETE, completeHandler); soundFactory.load(request); } /** * When sound is loaded, play it. * @param e */ private function completeHandler(e:Event):void { soundFactory.removeEventListener(Event.COMPLETE, completeHandler); playSound(); } /** * Play the sound and add listener to sound complete. */ private function playSound():void { song = soundFactory.play(); song.addEventListener(Event.SOUND_COMPLETE, soundCompleteHandler); } /** * when the sound is finish play again. * @param e */ private function soundCompleteHandler(e:Event):void { song.removeEventListener(Event.SOUND_COMPLETE, soundCompleteHandler); playSound(); } } }
Categories: Actionscript 3, Flash


The problem with this method is that it creates a small interval between each play through of the loop. If you use normal looping you don’t have this problem. I’m facing this “interval” problem myself.
same problem as Indigon’s, I figured out one way, hoping there’s a better way to do the trick, my method is to attachSound from the library instead of loading it from a url.