11. 11. 2008 [Downloads Flash/Actionscript]
MP3-Player Klassen [AS2/AS3]
Die ersten Klassen stehen zum download bereit! Zunächst gibts die
Audioplayer, jeweils in AS2 und AS3, aus der Klassensammlung.
Die Player verfügen jedoch über verschiedene Eigenschaften und Methoden,
können aber beliebig erweitert werden…
Eine Anleitung zur Verwendung steht natürlich mit im Script.

Hier die Source (AS2):
import caurina.transitions.Tweener; import caurina.transitions.properties.SoundShortcuts; import mx.utils.Delegate; class com.heimatdesign.media.Audioplayer extends Sound { private var acSong :Number; private var mainObj :Object = new Object(); public var vol :Number = 0; public var song_arr :Array = new Array(); //_________________________________________________________________________________________________________________ // Konstruktor public function playSong(songID:Number, auto:Boolean):Void { SoundShortcuts.init(); acSong = songID; super.loadSound(song_arr[songID], true); super.setVolume(vol); fadeVol(100); mainObj = super; if(auto) { this.onSoundComplete = Delegate.create(this,loop); } } //_________________________________________________________________________________________________________________ // Volume public function fadeVol(_vol):Void { Tweener.addTween(super, {_sound_volume:_vol, transition:"easeinoutcubic", time:3}); } //_________________________________________________________________________________________________________________ // stop/start Functions public function stopSound():Void { Tweener.addTween(super, {_sound_volume:0, transition:"easeinoutcubic", time:3, onComplete:stopSnd}); } private function stopSnd():Void { mainObj.stop(); } public function startSound():Void { super.loadSound(song_arr[acSong], true); super.setVolume(vol); fadeVol(100); } //_________________________________________________________________________________________________________________ // skip Song public function skipSong():Void { Tweener.addTween(super, {_sound_volume:0, transition:"easeinoutcubic", time:3, onComplete:nextSong}); } private function nextSong():Void { switch (acSong) { case song_arr.length-1 : acSong = 0; break; default : acSong += 1; break; } mainObj.loadSound(song_arr[acSong], true); } private function loop():Void { nextSong(); } }
Hier die Source (AS3):
package com.heimatdesign.media { import flash.events.Event; import flash.events.EventDispatcher; import flash.events.IOErrorEvent; import flash.events.ProgressEvent; import flash.events.SecurityErrorEvent; import flash.events.TimerEvent; import flash.media.ID3Info; import flash.media.Sound; import flash.media.SoundTransform; import flash.media.SoundChannel; import flash.media.SoundLoaderContext; import flash.media.SoundMixer; import flash.net.URLRequest; import flash.utils.Timer; public class Audioplayer extends EventDispatcher { public var s :Sound; public var sc :SoundChannel; public var idx :uint = 0; public var TRACKLIST_ARRAY :Array = new Array(); public var bufferTime :int = 1000; public var isLoaded :Boolean = false; public var isReadyToPlay :Boolean = false; public var isPlaying :Boolean = false; public var isStreaming :Boolean = true; public var autoPlay :Boolean = true; public var pausePosition :int = 0; public var progressInterval :int = 1000; public static const PLAY_PROGRESS :String = "playProgress"; public var playTimer :Timer; public var transf :SoundTransform; public function Audioplayer(autoPlay:Boolean = true, streaming:Boolean = true, bufferTime:int = -1):void { this.autoPlay = autoPlay; this.isStreaming = streaming; if (bufferTime < 0) { bufferTime = SoundMixer.bufferTime; } this.bufferTime = Math.min(Math.max(0, bufferTime), 30); } public function startPlaying():void { if(this.isReadyToPlay) { this.resume(); } else { this.load(); } } public function play(pos:int = 0):void { if (!this.isPlaying) { if (this.isReadyToPlay) { this.sc = this.s.play(pos); this.sc.addEventListener(Event.SOUND_COMPLETE, onPlayComplete); this.isPlaying = true; this.playTimer = new Timer(this.progressInterval); this.playTimer.addEventListener(TimerEvent.TIMER, onPlayTimer); this.playTimer.start(); } else if (this.isStreaming && !this.isLoaded) { this.load(); return; } } } public function stop(pos:int = 0):void { if (this.isPlaying) { this.pausePosition = pos; this.sc.stop(); this.playTimer.stop(); this.isPlaying = false; } if (this.isStreaming && !this.isLoaded) { this.s.close(); this.isReadyToPlay = false; } } public function pause():void { stop(this.sc.position); } public function resume():void { play(this.pausePosition); } public function skipForward():void { if (idx < (this.TRACKLIST_ARRAY.length - 1)) { idx++; this.load(); } else { idx = 0; this.load(); } } public function skipBackward():void { if (idx > 0) { idx--; this.load(); }else { idx = this.TRACKLIST_ARRAY.length - 1; this.load(); } } public function skipTo(_no:uint):void { idx = _no; this.load(); } public function setVol(_vol:Number):void { if(_vol >= 0 && _vol <= 1) { transf = sc.soundTransform; transf.volume = _vol; sc.soundTransform = transf; } } private function load():void { if (this.isPlaying) { this.stop(); this.pausePosition = 0; } this.isLoaded = false; this.s = new Sound(); this.s.addEventListener(ProgressEvent.PROGRESS, onLoadProgress); this.s.addEventListener(Event.OPEN, onLoadOpen); this.s.addEventListener(Event.COMPLETE, onLoadComplete); this.s.addEventListener(Event.ID3, onID3); this.s.addEventListener(IOErrorEvent.IO_ERROR, onIOError); this.s.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onIOError); var req:URLRequest = new URLRequest(this.TRACKLIST_ARRAY[idx]); trace("::::: " + TRACKLIST_ARRAY[idx] + " wird geladen :::::"); var context:SoundLoaderContext = new SoundLoaderContext(this.bufferTime, true); this.s.load(req, context); } private function onLoadOpen(event:Event):void { if (this.isStreaming) { this.isReadyToPlay = true; if (autoPlay) { this.play(); } } this.dispatchEvent(event.clone()); } private function onLoadProgress(event:ProgressEvent):void { this.dispatchEvent(event.clone()); } private function onLoadComplete(event:Event):void { this.isReadyToPlay = true; this.isLoaded = true; this.dispatchEvent(event.clone()); if (autoPlay && !isPlaying) { play(); } } public function get isPaused():Boolean { return (this.pausePosition > 0) } private function onPlayComplete(event:Event):void { this.pausePosition = 0; this.playTimer.stop(); this.isPlaying = false; this.dispatchEvent(event.clone()); if(idx == TRACKLIST_ARRAY.length - 1) { idx = 0; this.load(); } else { idx += 1; this.load(); } } public function onID3(event:Event):void { try { var id3:ID3Info = event.target.id3; for (var propName:String in id3) { //trace(propName + " = " + id3[propName]); } } catch (err:SecurityError) { trace("Could not retrieve ID3 data."); } } public function get id3():ID3Info { return this.s.id3; } private function onIOError(event:IOErrorEvent):void { trace("SoundFacade.onIOError: " + event.text); this.dispatchEvent(event.clone()); } private function onPlayTimer(event:TimerEvent):void { var estimatedLength:int = Math.ceil(this.s.length / (this.s.bytesLoaded / this.s.bytesTotal)); var progEvent:ProgressEvent = new ProgressEvent(PLAY_PROGRESS, false, false, this.sc.position, estimatedLength); this.dispatchEvent(progEvent); } } }