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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
<?xml version="1.0"?>
<!-- -->
<!-- Adobe Flex 4 source file of APlayer.swf, -->
<!-- a FlashPlayer-10 compatible component for playing -->
<!-- MP3 audio files and streams. -->
<!-- -->
<!-- version 20120425 -->
<!-- -->
<!-- -->
<!-- The free Adobe Flex 4 SDK is required to compile -->
<!-- this file. Get it from -->
<!-- -->
<!-- http://www.adobe.com/products/flex/ -->
<!-- -->
<!-- and run -->
<!-- -->
<!-- mxmlc -static-link-runtime-shared-libraries APlayer.mxml -->
<!-- -->
<!-- on the command line. -->
<!-- -->
<!-- -->
<!-- Copyright (C) 2012 Alexander Grahn -->
<!-- -->
<!-- This work may be distributed and/or modified under the -->
<!-- conditions of the LaTeX Project Public License, either -->
<!-- version 1.3 of this license or (at your option) any later -->
<!-- version. -->
<!-- -->
<!-- The latest version of this license is in -->
<!-- http://www.latex-project.org/lppl.txt -->
<!-- and version 1.3 or later is part of all distributions of -->
<!-- LaTeX version 2005/12/01 or later. -->
<!-- -->
<!-- This work has the LPPL maintenance status `maintained'. -->
<!-- -->
<!-- The current maintainer of this work is A. Grahn. -->
<!-- -->
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
preinitialize="initialise(FlexGlobals.topLevelApplication.parameters);"
applicationComplete="initSound();addEventListener(Event.ENTER_FRAME, onEnterFrame);"
creationComplete="initCallBacks();"
mouseDown="pause();"
mouseUp="play();"
>
<fx:Script>
<![CDATA[
[Bindable] private var source:String;
[Bindable] private var autoPlay:Boolean=false;
[Bindable] private var loop:Boolean=false;
[Bindable] private var vol:Number=0.75;
[Bindable] private var balance:Number=0;
private var snd:Sound;
private var sndCh:SoundChannel;
private var sndTr:SoundTransform;
private var playResumePosition:Number = 0;
private var playing:Boolean = false;
private var muted:Boolean = false;
private var lastVol:Number;
import mx.core.FlexGlobals;
private function initialise(flashVars:Object):void {
source=flashVars.source;
if(flashVars.autoPlay){autoPlay=(flashVars.autoPlay=='true')}
if(flashVars.loop){loop=(flashVars.loop=='true')}
if(flashVars.volume){vol=Number(flashVars.volume)}
if(flashVars.balance){balance=Number(flashVars.balance)}
}
import mx.controls.Alert;
private function initSound():void {
snd = new Sound(new URLRequest(source));
sndTr = new SoundTransform(vol, balance);
snd.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
snd.addEventListener(ProgressEvent.PROGRESS, progressHandler);
snd.addEventListener(Event.COMPLETE, completeHandler);
if(autoPlay) play();
}
private function setSource(src:String):void {
pause();
snd = new Sound(new URLRequest(src));
if(autoPlay) play();
}
private function play():void {
if (!playing) {
try{sndCh = snd.play(playResumePosition, 0, sndTr);}
catch(err:Error){Alert.show(err.message,'Error');}
sndCh.addEventListener(Event.SOUND_COMPLETE, sndCompleteHandler);
playing = true;
}
}
private function pause():void {
if (playing) {
playResumePosition = sndCh.position;
sndCh.stop();
playing = false;
}
}
private function playPause():void {
if (playing) pause(); else play();
}
private function seek(p:Number):void {
playResumePosition = p*1000;
if (playing) {
sndCh.stop();
playing = false;
play();
}
}
private function rewind():void {
seek(0);
}
private function volume(v:Number):void {
sndTr.volume = v;
if (playing) {
pause();
play();
}
}
private function mute():void {
if(muted) {
if (lastVol==0) lastVol=0.75;
volume(lastVol);
muted=false;
}
else {
lastVol = sndTr.volume
volume(0);
muted=true;
}
}
private function initCallBacks():void {
ExternalInterface.addCallback("play", play);
ExternalInterface.addCallback("pause", pause);
ExternalInterface.addCallback("playPause", playPause);
ExternalInterface.addCallback("seek", seek);
ExternalInterface.addCallback("rewind", rewind);
ExternalInterface.addCallback("volume", volume);
ExternalInterface.addCallback("mute", mute);
ExternalInterface.addCallback("setSource", setSource);
}
private function sndCompleteHandler(event:Event):void {
playing = false;
playResumePosition = 0;
if(loop) play();
}
private function completeHandler(event:Event):void {
playProgress.indeterminate=false;
}
import flash.events.ProgressEvent;
private function progressHandler(event:ProgressEvent):void {
playProgress.indeterminate=true;
if(playing) {
playProgress.setProgress(0.5,1);
}
}
private function onEnterFrame(event:Event):void {
if(playProgress.indeterminate==false && playing) {
playProgress.setProgress(sndCh.position,snd.length);
}
}
import flash.events.IOErrorEvent;
private function errorHandler(errorEvent:IOErrorEvent):void {
Alert.show(errorEvent.text + '\ncould not be loaded','Error');
}
]]>
</fx:Script>
<mx:ProgressBar width="100%" mode="manual"
horizontalCenter="0" verticalCenter="0" labelPlacement="center"
label="" id="playProgress"
/>
</s:Application>
|