| 1 |
<?xml version="1.0"?> |
| 2 |
<!-- --> |
| 3 |
<!-- Apache Flex 4 source file of APlayer.swf, --> |
| 4 |
<!-- a FlashPlayer-10 compatible component for playing --> |
| 5 |
<!-- MP3 audio files and streams. --> |
| 6 |
<!-- --> |
| 7 |
<!-- version 20150601 --> |
| 8 |
<!-- --> |
| 9 |
<!-- --> |
| 10 |
<!-- The free Apache Flex 4 SDK is required to compile --> |
| 11 |
<!-- this file. Get it from --> |
| 12 |
<!-- --> |
| 13 |
<!-- http://flex.apache.org/download-binaries.html --> |
| 14 |
<!-- --> |
| 15 |
<!-- and run --> |
| 16 |
<!-- --> |
| 17 |
<!-- mxmlc -static-link-runtime-shared-libraries APlayer.mxml --> |
| 18 |
<!-- --> |
| 19 |
<!-- on the command line. --> |
| 20 |
<!-- --> |
| 21 |
<!-- --> |
| 22 |
<!-- Copyright (C) 2012-today Alexander Grahn --> |
| 23 |
<!-- --> |
| 24 |
<!-- This work may be distributed and/or modified under the --> |
| 25 |
<!-- conditions of the LaTeX Project Public License. --> |
| 26 |
<!-- --> |
| 27 |
<!-- The latest version of this license is in --> |
| 28 |
<!-- http://www.latex-project.org/lppl.txt --> |
| 29 |
<!-- --> |
| 30 |
<!-- This work has the LPPL maintenance status `maintained'. --> |
| 31 |
<!-- --> |
| 32 |
<!-- The current maintainer of this work is A. Grahn. --> |
| 33 |
<!-- --> |
| 34 |
|
| 35 |
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" |
| 36 |
xmlns:s="library://ns.adobe.com/flex/spark" |
| 37 |
xmlns:mx="library://ns.adobe.com/flex/mx" |
| 38 |
preinitialize="initialise(this.parameters);" |
| 39 |
applicationComplete="initSound();addEventListeners(); |
| 40 |
fadeTargets=new Array(playProgress, caption);" |
| 41 |
creationComplete="initCallBacks();initContext();" |
| 42 |
mouseDown="pause();setFocus();" |
| 43 |
mouseUp="play();" |
| 44 |
backgroundAlpha="0" |
| 45 |
> |
| 46 |
|
| 47 |
<fx:Script> |
| 48 |
<![CDATA[ |
| 49 |
[Bindable] private var source:String; |
| 50 |
[Bindable] private var policy:String=null; |
| 51 |
[Bindable] private var autoPlay:Boolean=false; |
| 52 |
[Bindable] private var loop:Boolean=false; |
| 53 |
[Bindable] private var vol:Number=0.75; |
| 54 |
[Bindable] private var pan:Number=0; |
| 55 |
[Bindable] private var hideBar:Boolean=false; |
| 56 |
[Bindable] private var fadeTargets:Array; |
| 57 |
[Bindable] private var txtAlign:String; |
| 58 |
|
| 59 |
private var snd:Sound; |
| 60 |
private var id3:ID3Info; |
| 61 |
private var sndCh:SoundChannel; |
| 62 |
private var sndTr:SoundTransform; |
| 63 |
private var playResumePos:Number = 0; |
| 64 |
private var pauseAtPos:Number = -1; |
| 65 |
private var toBePaused:Boolean = false; |
| 66 |
private var plyng:Boolean = false; |
| 67 |
private var muted:Boolean = false; |
| 68 |
private var lastVol:Number; |
| 69 |
|
| 70 |
private var deltaSeek:Number; |
| 71 |
private var curTime:Number; |
| 72 |
private var keyPressed:Boolean=false; |
| 73 |
private var mouseIsOver:Boolean=false; |
| 74 |
|
| 75 |
private function initialise(flashVars:Object):void { |
| 76 |
source=flashVars.source; |
| 77 |
if(flashVars.policy) policy=flashVars.policy; |
| 78 |
if(flashVars.autoPlay=='true') autoPlay=true; |
| 79 |
if(flashVars.loop=='true') loop=true; |
| 80 |
if(flashVars.volume) vol=Number(flashVars.volume); |
| 81 |
if(flashVars.balance) pan=Number(flashVars.balance); |
| 82 |
if(flashVars.hideBar=='true') hideBar=true; |
| 83 |
} |
| 84 |
|
| 85 |
import mx.controls.Alert; |
| 86 |
|
| 87 |
private function initSound():void { |
| 88 |
snd = new Sound(); id3=null; |
| 89 |
sndTr = new SoundTransform(vol, pan); |
| 90 |
snd.addEventListener(Event.ID3, onID3Info); |
| 91 |
snd.addEventListener(IOErrorEvent.IO_ERROR, onError); |
| 92 |
snd.addEventListener(ProgressEvent.PROGRESS, onProgress); |
| 93 |
snd.addEventListener(Event.COMPLETE, onComplete); |
| 94 |
if(policy) Security.loadPolicyFile(policy); |
| 95 |
snd.load(new URLRequest(source), new SoundLoaderContext(1000, true)); |
| 96 |
if(autoPlay) play(); |
| 97 |
} |
| 98 |
|
| 99 |
private function onMouseOver(e:MouseEvent):void { |
| 100 |
mouseIsOver=true; |
| 101 |
fadeEffect.end(); |
| 102 |
playProgress.alpha=caption.alpha=1.0; |
| 103 |
} |
| 104 |
|
| 105 |
private function onMouseOut(e:MouseEvent):void { |
| 106 |
mouseIsOver=false; |
| 107 |
fadeEffect.play(); |
| 108 |
} |
| 109 |
|
| 110 |
private function onKeyDown(e:KeyboardEvent):void { |
| 111 |
switch(e.keyCode) { |
| 112 |
case 32: //space bar |
| 113 |
playPause(); |
| 114 |
break; |
| 115 |
case 36: //home |
| 116 |
pause(); |
| 117 |
playResumePos=(0); |
| 118 |
break; |
| 119 |
case 35: //end |
| 120 |
if(playProgress.indeterminate) break; |
| 121 |
pause(); |
| 122 |
playResumePos=(snd.length); |
| 123 |
break; |
| 124 |
case 37: //<-- |
| 125 |
fadeEffect.end(); |
| 126 |
playProgress.alpha=caption.alpha=1.0; |
| 127 |
if(e.ctrlKey){ |
| 128 |
pan=Math.max(-1,pan-0.025); |
| 129 |
balance(pan); |
| 130 |
break; |
| 131 |
} |
| 132 |
if(!keyPressed){ |
| 133 |
deltaSeek=Math.max(100,snd.length/10000); |
| 134 |
if(plyng) |
| 135 |
curTime=sndCh.position; |
| 136 |
else |
| 137 |
curTime=playResumePos; |
| 138 |
} |
| 139 |
keyPressed=true; |
| 140 |
playResumePos=Math.max(0,curTime-deltaSeek); |
| 141 |
if(plyng) seek(playResumePos/1000); |
| 142 |
deltaSeek*=1.1; |
| 143 |
break; |
| 144 |
case 39: //--> |
| 145 |
fadeEffect.end(); |
| 146 |
playProgress.alpha=caption.alpha=1.0; |
| 147 |
if(e.ctrlKey){ |
| 148 |
pan=Math.min(1,pan+0.025); |
| 149 |
balance(pan); |
| 150 |
break; |
| 151 |
} |
| 152 |
if(!keyPressed){ |
| 153 |
deltaSeek=Math.max(100,snd.length/10000); |
| 154 |
if(plyng) |
| 155 |
curTime=sndCh.position; |
| 156 |
else |
| 157 |
curTime=playResumePos; |
| 158 |
} |
| 159 |
keyPressed=true; |
| 160 |
playResumePos=Math.min(snd.length-10,curTime+deltaSeek); |
| 161 |
seek(playResumePos/1000); |
| 162 |
deltaSeek*=1.1; |
| 163 |
break; |
| 164 |
case 38: |
| 165 |
vol=Math.min(1,vol+0.025); |
| 166 |
volume(vol); |
| 167 |
break; |
| 168 |
case 40: |
| 169 |
if(e.ctrlKey){ |
| 170 |
pan=0; |
| 171 |
balance(pan); |
| 172 |
break; |
| 173 |
} |
| 174 |
vol=Math.max(0,vol-0.025); |
| 175 |
volume(vol); |
| 176 |
break; |
| 177 |
default: |
| 178 |
if(e.charCode==109) mute(); //`m' |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
private function onKeyUp(e:KeyboardEvent):void { |
| 183 |
switch(e.keyCode) { |
| 184 |
case 37: //<-- |
| 185 |
case 39: //--> |
| 186 |
deltaSeek=Math.max(100,snd.length/10000); |
| 187 |
keyPressed=false; |
| 188 |
if (!mouseIsOver) fadeEffect.play(); |
| 189 |
break; |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
private function formatLabel(s:Number):String { |
| 194 |
s/=1000; |
| 195 |
var hrs:Number = Math.floor(s / 3600); |
| 196 |
var min:Number = Math.floor(s / 60 % 60); |
| 197 |
var sec:Number = Math.floor(s % 60); |
| 198 |
var fmtd:String=''; |
| 199 |
if(hrs>0) fmtd = String(hrs)+':'; |
| 200 |
if(hrs>0 && min <10) fmtd+='0'; |
| 201 |
fmtd += String(min)+':'; |
| 202 |
if(sec<10) fmtd+='0'; |
| 203 |
fmtd += String(sec); |
| 204 |
txtAlign="center"; |
| 205 |
try{ |
| 206 |
if(id3.songName) |
| 207 |
fmtd += ' '+ String.fromCharCode(0x2014) + ' ' + id3.songName; |
| 208 |
if(id3.artist) fmtd += ' | ' + id3.artist; |
| 209 |
if(id3.album) fmtd += ' | ' + id3.album; |
| 210 |
if(id3) txtAlign="start"; |
| 211 |
}catch(e:Object){} |
| 212 |
return fmtd; |
| 213 |
} |
| 214 |
|
| 215 |
private function addEventListeners():void { |
| 216 |
this.addEventListener(Event.ENTER_FRAME, onEnterFrame); |
| 217 |
this.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown); |
| 218 |
this.addEventListener(KeyboardEvent.KEY_UP, onKeyUp); |
| 219 |
this.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut); |
| 220 |
this.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver); |
| 221 |
} |
| 222 |
|
| 223 |
private function setSource(s:String):void { |
| 224 |
pause(); |
| 225 |
source=s; |
| 226 |
initSound(); |
| 227 |
pause();seek(0); |
| 228 |
} |
| 229 |
|
| 230 |
private function loadPolicy(p:String):void {policy=p;} |
| 231 |
|
| 232 |
private function play(p:Number=-1):void { |
| 233 |
if(p>=0) seek(p); |
| 234 |
if(plyng) return; |
| 235 |
try{sndCh = snd.play(playResumePos, 0, sndTr);} |
| 236 |
catch(e:Error){Alert.show(e.message,'Error');} |
| 237 |
sndCh.addEventListener(Event.SOUND_COMPLETE, onSoundComplete); |
| 238 |
plyng = true; |
| 239 |
} |
| 240 |
|
| 241 |
private function pause(p:Number=-1):void { |
| 242 |
if(p>=0){pauseAtPos=p*1000;return;} |
| 243 |
if(!plyng) return; |
| 244 |
playResumePos = sndCh.position; |
| 245 |
sndCh.stop(); |
| 246 |
plyng = false; |
| 247 |
} |
| 248 |
|
| 249 |
private function playPause():void { |
| 250 |
if(plyng) pause(); else play(); |
| 251 |
} |
| 252 |
|
| 253 |
private function seek(p:Number):void { |
| 254 |
playResumePos = p*1000; |
| 255 |
if(!plyng) return; |
| 256 |
sndCh.stop(); |
| 257 |
plyng = false; |
| 258 |
play(); |
| 259 |
} |
| 260 |
|
| 261 |
private function rewind():void { |
| 262 |
seek(0); |
| 263 |
} |
| 264 |
|
| 265 |
private function volume(v:Number):void { |
| 266 |
sndTr.volume = v; |
| 267 |
sndCh.soundTransform=sndTr; |
| 268 |
} |
| 269 |
|
| 270 |
private function mute():void { |
| 271 |
if(muted) { |
| 272 |
if(lastVol==0) lastVol=0.75; |
| 273 |
volume(lastVol); |
| 274 |
muted=false; |
| 275 |
} |
| 276 |
else { |
| 277 |
lastVol = sndTr.volume |
| 278 |
volume(0); |
| 279 |
muted=true; |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
private function balance(p:Number):void { |
| 284 |
sndTr.pan = p; |
| 285 |
sndCh.soundTransform=sndTr; |
| 286 |
} |
| 287 |
|
| 288 |
private function currentTime():Number { |
| 289 |
return sndCh.position/1000; |
| 290 |
} |
| 291 |
|
| 292 |
private function playing():Boolean { |
| 293 |
return plyng; |
| 294 |
} |
| 295 |
|
| 296 |
private function duration():Number { |
| 297 |
return snd.length/1000; |
| 298 |
} |
| 299 |
|
| 300 |
private function ismuted():Boolean { |
| 301 |
return muted; |
| 302 |
} |
| 303 |
|
| 304 |
private function initCallBacks():void { |
| 305 |
ExternalInterface.addCallback("play", play); |
| 306 |
ExternalInterface.addCallback("pause", pause); |
| 307 |
ExternalInterface.addCallback("playPause", playPause); |
| 308 |
ExternalInterface.addCallback("seek", seek); |
| 309 |
ExternalInterface.addCallback("rewind", rewind); |
| 310 |
ExternalInterface.addCallback("volume", volume); |
| 311 |
ExternalInterface.addCallback("balance", balance); |
| 312 |
ExternalInterface.addCallback("mute", mute); |
| 313 |
ExternalInterface.addCallback("setSource", setSource); |
| 314 |
ExternalInterface.addCallback("loadPolicy", loadPolicy); |
| 315 |
ExternalInterface.addCallback("currentTime", currentTime); |
| 316 |
ExternalInterface.addCallback("duration", duration); |
| 317 |
ExternalInterface.addCallback("playing", playing); |
| 318 |
ExternalInterface.addCallback("muted", ismuted); |
| 319 |
} |
| 320 |
|
| 321 |
private function initContext():void { |
| 322 |
this.contextMenu.hideBuiltInItems(); |
| 323 |
|
| 324 |
var itemPlayPause:ContextMenuItem = new ContextMenuItem("N.N."); |
| 325 |
this.contextMenu.customItems.push(itemPlayPause); |
| 326 |
itemPlayPause.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, |
| 327 |
function(e:ContextMenuEvent):void{playPause();}); |
| 328 |
|
| 329 |
var itemRewind:ContextMenuItem = new ContextMenuItem("Rewind, [Home]"); |
| 330 |
this.contextMenu.customItems.push(itemRewind); |
| 331 |
itemRewind.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, |
| 332 |
function(e:ContextMenuEvent):void{pause();playResumePos=(0);}); |
| 333 |
|
| 334 |
var itemGotoEnd:ContextMenuItem=new ContextMenuItem("Goto End, [End]"); |
| 335 |
this.contextMenu.customItems.push(itemGotoEnd); |
| 336 |
itemGotoEnd.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, |
| 337 |
function(e:ContextMenuEvent):void{ |
| 338 |
if(playProgress.indeterminate) return; |
| 339 |
pause();playResumePos=(snd.length);}); |
| 340 |
|
| 341 |
var itemMute:ContextMenuItem = new ContextMenuItem("N.N."); |
| 342 |
this.contextMenu.customItems.push(itemMute); |
| 343 |
itemMute.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, |
| 344 |
function(e:ContextMenuEvent):void{mute();}); |
| 345 |
|
| 346 |
this.contextMenu.customItems.push( |
| 347 |
new ContextMenuItem("via keyboard:", true, false, true)); |
| 348 |
this.contextMenu.customItems.push( |
| 349 |
new ContextMenuItem("Seek, [\u2190]/[\u2192]", false, false, true)); |
| 350 |
this.contextMenu.customItems.push( |
| 351 |
new ContextMenuItem("Volume, [\u2191]/[\u2193]", false, false, true)); |
| 352 |
this.contextMenu.customItems.push( |
| 353 |
new ContextMenuItem("Balance, [Ctrl]+[\u2190]/[\u2193]/[\u2192]", |
| 354 |
false, false, true)); |
| 355 |
|
| 356 |
this.contextMenu.addEventListener(ContextMenuEvent.MENU_SELECT, |
| 357 |
function(e:ContextMenuEvent):void{ |
| 358 |
itemPlayPause.caption=(plyng ? "Pause" : "Play")+", [Space]"; |
| 359 |
itemMute.caption=(muted ? "Unmute" : "Mute")+", [m]";} |
| 360 |
); |
| 361 |
} |
| 362 |
|
| 363 |
private function onSoundComplete(e:Event):void { |
| 364 |
plyng = false; |
| 365 |
playResumePos = 0; |
| 366 |
if(loop) play(); |
| 367 |
} |
| 368 |
|
| 369 |
private function onComplete(e:Event):void { |
| 370 |
playProgress.indeterminate=false; |
| 371 |
} |
| 372 |
|
| 373 |
private function onProgress(e:ProgressEvent):void { |
| 374 |
playProgress.indeterminate=true; |
| 375 |
} |
| 376 |
|
| 377 |
private function onEnterFrame(e:Event):void { |
| 378 |
if(!playProgress.indeterminate) { |
| 379 |
if(plyng) { |
| 380 |
playProgress.setProgress(sndCh.position,snd.length); |
| 381 |
caption.text=formatLabel(sndCh.position); |
| 382 |
} else { |
| 383 |
playProgress.setProgress(playResumePos,snd.length); |
| 384 |
caption.text=formatLabel(playResumePos); |
| 385 |
} |
| 386 |
}else{ |
| 387 |
if(plyng) { |
| 388 |
playProgress.setProgress(Math.random(),1); |
| 389 |
caption.text=formatLabel(sndCh.position); |
| 390 |
} else { |
| 391 |
playProgress.setProgress(snd.bytesLoaded,snd.bytesTotal); |
| 392 |
caption.text=formatLabel(playResumePos); |
| 393 |
} |
| 394 |
} |
| 395 |
if(plyng&&pauseAtPos>=0&&sndCh.position<pauseAtPos) |
| 396 |
toBePaused=true; |
| 397 |
if( |
| 398 |
plyng&&pauseAtPos>=0&& |
| 399 |
sndCh.position>=pauseAtPos&&toBePaused |
| 400 |
){ |
| 401 |
pause(); |
| 402 |
pauseAtPos=-1; |
| 403 |
toBePaused=false; |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
private function onID3Info(e:Event):void {id3 = e.target.id3;} |
| 408 |
|
| 409 |
private function onError(e:ErrorEvent):void { |
| 410 |
Alert.show(e.type, e.text); |
| 411 |
} |
| 412 |
]]> |
| 413 |
</fx:Script> |
| 414 |
|
| 415 |
<fx:Declarations> |
| 416 |
<s:Fade id="fadeEffect" targets="{fadeTargets}" alphaFrom="1.0" alphaTo="0" |
| 417 |
duration="2000"/> |
| 418 |
</fx:Declarations> |
| 419 |
|
| 420 |
<mx:ProgressBar width="100%" height="100%" mode="manual" |
| 421 |
labelPlacement="center" label="" id="playProgress" |
| 422 |
visible="{!hideBar}" |
| 423 |
/> |
| 424 |
|
| 425 |
<s:Label id="caption" width="100%" height="100%" verticalCenter="0" |
| 426 |
paddingTop="2" fontWeight="bold" verticalAlign="middle" visible="{!hideBar}" |
| 427 |
textAlign="{txtAlign}" paddingLeft="5" |
| 428 |
/> |
| 429 |
</s:Application> |