[MAJ]Classe de lecture de son

Une petite classe pour faciliter la lecture du son en actionscript 3 (bien que l’utilisation est très simpliste en as3), mais elle permet de répéter facilement le son, et de récupérer les différents évènements. De plus, deux méthodes statiques permettent de couper tous les sons générés par la classe.

/**
* Classe de gestion du son - Action Script 3 - v1.4.1
*
* Exemple :
* var music_pinkFloyd:SoundManager = new SoundManager("sons/musique/darkSideOfTheMoon.mp3");
* music_pinkFloyd.playSound(true); -> Lecture en boucle
* music_pinkFloyd.playSound(); -> Lecture et arrêt
*
* Désactiver le son non joués : SoundManager.disableSound()
* Arrêter tous les sons joués: SoundManager.stopAll()
*
* Laurent Clavé - 2010 - Licence GNU/GPL
* www.laurent-clave.fr
*/

/**
* Classe de gestion du son - Action Script 3 - v1.3
*
* Exemple :
* var music_pinkFloyd:SoundManager = new SoundManager("sons/musique/darkSideOfTheMoon.mp3");
* music_pinkFloyd.playSound(true); -> Lecture en boucle
* music_pinkFloyd.playSound(); -> Lecture et arrêt
*
* Désactiver le son non joués : SoundManager.disableSound()
* Arrêter tous les sons joués: SoundManager.stopAll()
*
* Laurent Clavé - 2010 - Licence GNU/GPL v1.4
* www.laurent-clave.fr
*/

package {
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.events.IOErrorEvent;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundMixer;
	import flash.net.URLRequest;

	public class SoundManager extends Sprite {

		private var url:String ;
		private var song:SoundChannel;
		private var soundFactory:Sound;
		private static var enable:Boolean = true;	

		public function SoundManager(url) {

			this.url = url;
			var request:URLRequest = new URLRequest(url);
			var soundFactory:Sound = new Sound();
			soundFactory.addEventListener(Event.COMPLETE, completeHandler);
			soundFactory.addEventListener(Event.ID3, id3Handler);
			soundFactory.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			soundFactory.addEventListener(ProgressEvent.PROGRESS, progressHandler);
			soundFactory.load(request);
			this.soundFactory = soundFactory;
		}

		public function playSound(rep=false):void {
			if(enable == true){
				this.song = this.soundFactory.play();
			if(rep == true)
				this.song.addEventListener(Event.SOUND_COMPLETE, repeat );
			}
		}

		public function stopSound():void {
			if(this.song is SoundChannel)this.song.stop();
		}

		public function repeat(event:Event):void {
			playSound();
			this.song.addEventListener(Event.SOUND_COMPLETE, repeat );
		}

		public static function enableSound():void{
			enable = true;
		}

		public static function disableSound():void{
			enable = false;
		}

		public static function stopAll():void{
				SoundMixer.stopAll();
		}

		private function completeHandler(event:Event):void {
			//trace("completeHandler: " + event);
		}

		private function id3Handler(event:Event):void {
			//trace("id3Handler: " + event);
		}

		private function ioErrorHandler(event:Event):void {
			//trace("ioErrorHandler: " + event);
		}

		private function progressHandler(event:ProgressEvent):void {
			//trace("progressHandler: " + event);
		}
	}
}

Leave a Reply