Inicio > Actionscript 3, Flash > Tutorial actionscript3 – Loading sound and play as loop

Tutorial actionscript3 – Loading sound and play as loop

viernes, 10 de julio de 2009 miguelMoraleda Dejar un comentario Ir a comentarios

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.

DESCARGAR EJEMPLO

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 Tags:
  1. jueves, 23 de julio de 2009 a las 12:50 | #1

    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.

  2. Forrest
    miércoles, 21 de octubre de 2009 a las 17:13 | #2

    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.

*

Spam Protection by WP-SpamFree

Get Adobe Flash playerPlugin by wpburn.com wordpress themes