xml version encoding utf-8 mx Application xmlns mx http www adobe com

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?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.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;
private var playBackTime:ArrayCollection = new ArrayCollection([{minutes: 0, seconds: 0}]);
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 {
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 {
var currMinutes:int = playBackTime.getItemIndex(0);
var currSeconds:int = playBackTime.getItemIndex(1);
currSeconds = currSeconds + 1;
if(currSeconds==59) {
currSeconds = 0;
currMinutes = currMinutes + 1;
}
playBackTime.setItemAt({minutes: currMinutes}, 0);
playBackTime.setItemAt({seconds: currMinutes}, 1);
elapse.text = "Elapsed time: " + playBackTime.getItemIndex(0) + ":" + playBackTime.getItemIndex(1);
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;
}
public function stopSound():void {
timer.stop();
channel.stop();
}
]]>
</mx:Script>
</mx:Application>