<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="initApp()" textAlign="left" horizontalAlign="left">
<mx:Panel width="604" height="229" layout="absolute" title="Mp3 Player">
<mx:Text id="load" width="229" text="" textAlign="left" x="10" y="15"/>
<mx:Text id="elapse" width="229" text="" textAlign="left" x="10" y="43"/>
<mx:Text id="total" width="229" text="" textAlign="left" x="10" y="71"/>
<mx:Button id="play" label="Play" click="playSound(10)" textAlign="left" enabled="true" x="10" y="103"/>
<mx:Button x="69" y="103" label="stop" id="stop" click="stopSound()"/>
</mx:Panel>
<mx:Script>
<![CDATA[
import flash.utils.setTimeout;
import flash.utils.getTimer;
import mx.collections.ArrayCollection;
import mx.controls.ProgressBar;
import flash.events.Event;
import flash.media.Sound;
import flash.net.URLRequest;
private var sound:Sound;
private var channel:SoundChannel;
private var timer:Timer;
private var precentLoaded:int;
private var req:URLRequest;
private var endPoint:uint;
public function initApp():void {
addEventListener(Event.ENTER_FRAME, onEnterFrame);
req = new URLRequest("http://muzx.net/files/mmp/2/Alt anesis feat mike a - radiant ablaze.mp3");
sound = new Sound();
sound.addEventListener(ProgressEvent.PROGRESS, calculateLoadedPrecentage);
sound.load(req);
}
public function playSound(start:uint=0, end:uint=0):void {
if(!channel) {
channel = sound.play();
}
if(end == 0) {
endPoint = sound.length;
} else {
endPoint = end;
}
enableTimer();
}
private function enableTimer():void {
timer = new Timer(1000);
timer.addEventListener(TimerEvent.TIMER, timerHandler);
timer.start();
}
public function timerHandler(event:TimerEvent):void {
if(uint(channel.position.toFixed(2)) >= endPoint) {
timer.stop();
channel.stop();
}
}
public function calculateLoadedPrecentage(event:ProgressEvent):void {
precentLoaded = Math.round(100 * (event.bytesLoaded / event.bytesTotal));
load.text = "Loaded " + precentLoaded + "%";
}
public function onEnterFrame(event:Event):void {
var estimatedLength:int = Math.ceil(sound.length / (sound.bytesLoaded / sound.bytesTotal));
var playbackPercent:uint = Math.round(100 * (channel.position / estimatedLength));
var timeMillis:int = sound.length;
var time:int = timeMillis / 1000;
var seconds:int = time % 60;
var minutes:int = (time % 3600) / 60;
var hours:int = time / 3600;
total.text = "Total time: " + minutes + ":" + seconds;
var currSec:int = timer.currentCount % 60;
var currMin:int = (timer.currentCount % 3600) / 60;
elapse.text = "Elapsed time: " + currMin + ":" + currSec;
}
public function stopSound():void {
timer.stop();
timer.reset();
channel.stop();
channel = null;
var currSec:int = timer.currentCount % 60;
var currMin:int = (timer.currentCount % 3600) / 60;
elapse.text = "Elapsed time: " + currMin + ":" + currSec;
}
]]>
</mx:Script>
</mx:Application>