/** * SoundPlayer * Loads/attachs and play a sound file. * * @author Zeh Fernando * @version 1.0.0 - 14/may/2005 - First version * @version 1.1.0 - 7/jun/2005 - New name, was "MP3Player" */ class zeh.components.SoundPlayer extends MovieClip { private static var globalSound:Sound = new Sound(); public var isPlaying:Boolean; // Whether or not the sound's being played private var mainSound:Sound; // Sound object used for playing private var lastKnownPosition:Number; // Last known sound position (used to resume sound playback at the same point) private var isSoundAttached:Boolean; // Whether or not a file is actually attached or loaded private var currentVolume:Number; // Actual volume (0-100) // Configuration public var shouldLoop:Boolean; // Whether it should loop or not at the end of the playback private var soundFile:String; // URL of the source file (when streamed) private var soundIdentifier:String; // URL of the source file (when attached) private var isStreaming:Boolean; // Whether it's an attached or a streaming sound private var sourceType:String; // "Attached", "Streamed" private var autoPlay:Boolean; // Wthether to automatically start or not private var defaultVolume:Number; // Default starting volume (0-100) // Events public var onSoundEnded:Function; // When the sound ends // ================================================================================================================ // CLASS functions ------------------------------------------------------------------------------------------------ /** * Sets the volume globally */ public static function setGlobalVolume(p_vol:Number):Void { globalSound.setVolume(p_vol); } /** * Returns the global volume * * @return Number The current global volume */ public static function getGlobalVolume():Number { return globalSound.getVolume(); } // ================================================================================================================ // INSTANCE functions --------------------------------------------------------------------------------------------- /** * Constructor. * */ public function SoundPlayer () { isPlaying = false; lastKnownPosition = 0; isSoundAttached = false; isStreaming = sourceType == "Streamed"; var obj = this; _visible = false; // Creates main sound object mainSound = new Sound(this); mainSound.onSoundComplete = function() { if (obj.onSoundEnded != undefined) obj.onSoundEnded(); if (obj.shouldLoop && obj.isStreaming) this.start(0); } setVolume(defaultVolume); if (autoPlay) playSound(); } public function setSoundFile(p_file:String): Void { soundFile = p_file; }; /** * Loads the sound for streaming */ private function loadStreamingSound(p_url:String): Void { mainSound.loadSound(soundFile, true); mainSound.setVolume(currentVolume); isSoundAttached = true; } /** * Attaches the sound for playing */ private function loadAttachableSound(p_url:String): Void { mainSound.attachSound(soundIdentifier); mainSound.setVolume(currentVolume); isSoundAttached = true; } /** * Starts playing the sound from the beginning of the file */ public function playSound(p_seconds:Number):Void { lastKnownPosition = p_seconds == undefined ? 0 : p_seconds; resumeSound(); } /** * Starts playing the sound, resuming from the last known position */ public function resumeSound():Void { if (!isSoundAttached) { if (isStreaming) { loadStreamingSound(); } else { loadAttachableSound(); } } isPlaying = true; mainSound.start(lastKnownPosition, shouldLoop && !isStreaming ? 31337 : undefined); } /** * Stop playing the sound */ public function stopSound():Void { isPlaying = false; lastKnownPosition = mainSound.position / 1000; mainSound.stop(); } /** * Sets the volume */ public function setVolume(p_vol:Number):Void { if (p_vol == undefined || p_vol == null) p_vol = 100; currentVolume = p_vol; mainSound.setVolume(currentVolume); } /** * Returns the volume * * @return Number The current volume */ public function getVolume():Number { return currentVolume; } }