WavPlayer.hx 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. //
  2. // WAV/AU Flash player with resampler
  3. //
  4. // Copyright (c) 2009, Anton Fedorov <datacompboy@call2ru.com>
  5. //
  6. /* This code is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 only, as
  8. * published by the Free Software Foundation.
  9. *
  10. * This code is distributed in the hope that it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. * version 2 for more details (a copy is included in the LICENSE file that
  14. * accompanied this code).
  15. */
  16. class JsEventHandler {
  17. public var Id: Int;
  18. public var Event: String;
  19. public var Handler: String;
  20. public var User: Null<String>;
  21. public inline function new(id:Int, event:String, handler:String, ?user:String) {
  22. Id = id;
  23. Event = event;
  24. Handler = handler;
  25. User = user;
  26. }
  27. }
  28. class WavPlayerGui extends flash.events.EventDispatcher {
  29. var length: Float;
  30. var ready: Float;
  31. var position: Float;
  32. public function drawStopped(): Void { throw("Try to instantiate interface"); }
  33. public function drawBuffering(): Void { throw("Try to instantiate interface"); }
  34. public function drawPlaying(): Void { throw("Try to instantiate interface"); }
  35. public function drawPaused(): Void { throw("Try to instantiate interface"); }
  36. public function setLength(length: Float) { this.length = length; }
  37. public function setReady(ready: Float) { this.ready = ready; }
  38. public function setPosition(pos: Float) { this.position = pos; }
  39. function Sizer(x:Float,y:Float,color=0xFF0000,alpha:Float=0) {
  40. var sprite = new flash.display.Sprite();
  41. Rect(sprite,x,y,color,alpha);
  42. return sprite;
  43. }
  44. function Rect(sprite,x:Float,y:Float,color,alpha:Float=100) {
  45. var g:flash.display.Graphics = sprite.graphics;
  46. g.clear();
  47. g.lineStyle(1, color, alpha, true);
  48. g.beginFill(color, alpha);
  49. g.moveTo(0, 0);
  50. g.lineTo(0, y-1);
  51. g.lineTo(x-1, y-1);
  52. g.lineTo(x-1, 0);
  53. g.endFill();
  54. }
  55. }
  56. class WavPlayerGuiEvent extends flash.events.Event {
  57. static public inline var CLICKED : String = "PLAYERGUI_CLICKED";
  58. static public inline var DBLCLICKED : String = "PLAYERGUI_DOUBLECLICKED";
  59. static public inline var SEEKING : String = "PLAYERGUI_SEEKING";
  60. public var position: Null<Float>;
  61. public function new(type : String, ?position : Float, ?bubbles : Bool, ?cancelable : Bool) {
  62. super(type, bubbles, cancelable);
  63. this.position = position;
  64. }
  65. }
  66. class WavPlayerGui_None extends WavPlayerGui {
  67. var sprite: flash.display.Sprite;
  68. public inline function new(root, myMenu) {
  69. super();
  70. sprite = new flash.display.MovieClip();
  71. sprite.contextMenu = myMenu;
  72. sprite.useHandCursor = false;
  73. sprite.buttonMode = false;
  74. var Sizer = Sizer(1,1);
  75. sprite.addChild(Sizer);
  76. root.addChild(sprite);
  77. }
  78. public override function drawStopped() { }
  79. public override function drawBuffering() { }
  80. public override function drawPlaying() { }
  81. public override function drawPaused() { }
  82. }
  83. class WavPlayerGui_Mini extends WavPlayerGui {
  84. var sprite: flash.display.Sprite;
  85. var color: Int;
  86. public inline function new(root, myMenu, zoom:Float=1, x:Float=0, y:Float=0, color=0x808080) {
  87. super();
  88. this.color = color;
  89. sprite = new flash.display.MovieClip();
  90. sprite.contextMenu = myMenu;
  91. sprite.useHandCursor = true;
  92. sprite.buttonMode = true;
  93. sprite.doubleClickEnabled = true;
  94. sprite.addEventListener(flash.events.MouseEvent.CLICK, handleClicked);
  95. sprite.addEventListener(flash.events.MouseEvent.DOUBLE_CLICK, handleDblClicked);
  96. sprite.scaleX = zoom;
  97. sprite.scaleY = zoom;
  98. sprite.scaleZ = 1;
  99. sprite.x = x;
  100. sprite.y = y;
  101. var Sizer = Sizer(32,32);
  102. sprite.addChild(Sizer);
  103. Sizer.x = 4;
  104. Sizer.y = 4;
  105. root.addChild(sprite);
  106. }
  107. public override function drawStopped() {
  108. var g:flash.display.Graphics = sprite.graphics;
  109. g.clear();
  110. g.lineStyle(4, color, 1, true, flash.display.LineScaleMode.NORMAL,
  111. flash.display.CapsStyle.ROUND, flash.display.JointStyle.ROUND);
  112. g.beginFill(color);
  113. g.moveTo(8, 6);
  114. g.lineTo(30, 20);
  115. g.lineTo(8, 34);
  116. g.lineTo(8, 6);
  117. g.endFill();
  118. }
  119. public override function drawBuffering() {
  120. var g:flash.display.Graphics = sprite.graphics;
  121. g.clear();
  122. g.lineStyle(4, color, 1, true, flash.display.LineScaleMode.NORMAL,
  123. flash.display.CapsStyle.ROUND, flash.display.JointStyle.ROUND);
  124. g.drawCircle(20, 20, 10);
  125. }
  126. public override function drawPlaying() {
  127. var g:flash.display.Graphics = sprite.graphics;
  128. g.clear();
  129. g.lineStyle(6, color, 1, true, flash.display.LineScaleMode.NORMAL,
  130. flash.display.CapsStyle.ROUND, flash.display.JointStyle.ROUND);
  131. g.beginFill(color);
  132. g.moveTo(8, 8);
  133. g.lineTo(32, 8);
  134. g.lineTo(32, 32);
  135. g.lineTo(8, 32);
  136. g.lineTo(8, 8);
  137. g.endFill();
  138. }
  139. public override function drawPaused() {
  140. var g:flash.display.Graphics = sprite.graphics;
  141. g.clear();
  142. g.lineStyle(8, color, 1, true, flash.display.LineScaleMode.NORMAL,
  143. flash.display.CapsStyle.ROUND, flash.display.JointStyle.ROUND);
  144. g.moveTo(12, 8);
  145. g.lineTo(12, 32);
  146. g.moveTo(28, 8);
  147. g.lineTo(28, 32);
  148. }
  149. function handleClicked(event:flash.events.Event) {
  150. dispatchEvent(new WavPlayerGuiEvent(WavPlayerGuiEvent.CLICKED));
  151. }
  152. function handleDblClicked(event:flash.events.Event) {
  153. dispatchEvent(new WavPlayerGuiEvent(WavPlayerGuiEvent.DBLCLICKED));
  154. }
  155. }
  156. class WavPlayerGui_Full extends WavPlayerGui {
  157. var GuiMini: WavPlayerGui;
  158. var sprite: flash.display.Sprite;
  159. var rectFile: flash.display.Sprite;
  160. var rectReady: flash.display.Sprite;
  161. var rectMark: flash.display.Sprite;
  162. var minTicks: flash.display.Sprite;
  163. var width: Float;
  164. var zoom: Float;
  165. var timer: flash.utils.Timer;
  166. var lastTime: Float;
  167. var minor_tick_color: Int;
  168. var major_tick_color: Int;
  169. public inline function new(root : flash.display.Sprite, myMenu, zoom:Float=1, size:Float=10,
  170. bg_color=0x303030, ready_color=0xA0A0A0, cursor_color=0x7FA03F, button_color=0x808080,
  171. minor_tick_color=0x006600, major_tick_color=0x000066) {
  172. super();
  173. this.zoom = zoom;
  174. this.minor_tick_color = minor_tick_color;
  175. this.major_tick_color = major_tick_color;
  176. sprite = new flash.display.MovieClip();
  177. sprite.contextMenu = myMenu;
  178. sprite.scaleX = 1;
  179. sprite.scaleY = 1;
  180. sprite.scaleZ = 1;
  181. sprite.addEventListener(flash.events.MouseEvent.CLICK, handleClicked);
  182. sprite.useHandCursor = true;
  183. sprite.buttonMode = true;
  184. sprite.x = 40*zoom;
  185. sprite.y = 0;
  186. sprite.addChild(Sizer(40.0*size*zoom,40.0*zoom));
  187. GuiMini = new WavPlayerGui_Mini(root, myMenu, zoom, -3, 0, button_color);
  188. GuiMini.addEventListener(WavPlayerGuiEvent.CLICKED, proxyEvent);
  189. GuiMini.addEventListener(WavPlayerGuiEvent.DBLCLICKED, proxyEvent);
  190. rectFile = new flash.display.MovieClip();
  191. rectFile.scaleX = 1;
  192. rectFile.scaleY = 1;
  193. rectFile.scaleZ = 1;
  194. rectFile.addChild(Sizer(40.0*size*zoom+3,26.0*zoom,bg_color,1));
  195. sprite.addChild(rectFile);
  196. rectFile.x = -2;
  197. rectFile.y = 7*zoom;
  198. rectReady = new flash.display.MovieClip();
  199. rectReady.scaleX = 1;
  200. rectReady.scaleY = 1;
  201. rectReady.scaleZ = 1;
  202. rectReady.addChild(Sizer(40.0*size*zoom,10.0*zoom,ready_color,1));
  203. sprite.addChild(rectReady);
  204. rectReady.x = 0;
  205. rectReady.y = 15*zoom;
  206. rectReady.scaleX = 0.0;
  207. minTicks = new flash.display.MovieClip();
  208. minTicks.scaleX = 1;
  209. minTicks.scaleY = 1;
  210. minTicks.scaleZ = 1;
  211. minTicks.x = 0;
  212. minTicks.y = 0;
  213. sprite.addChild(minTicks);
  214. rectMark = new flash.display.MovieClip();
  215. rectMark.scaleX = 1;
  216. rectMark.scaleY = 1;
  217. rectMark.scaleZ = 1;
  218. rectMark.addChild(Sizer(5.0*zoom,40.0*zoom,cursor_color,1));
  219. sprite.addChild(rectMark);
  220. width = 40.0*size*zoom;
  221. rectMark.x = width*0.0-3;
  222. rectMark.y = 0;
  223. rectMark.scaleX = 0.7;
  224. root.addChild(sprite);
  225. timer = new flash.utils.Timer(100);
  226. timer.addEventListener( flash.events.TimerEvent.TIMER, delay );
  227. timer.stop();
  228. }
  229. public override function setLength(length: Float) {
  230. var oldlen = this.length;
  231. super.setLength(length);
  232. if (oldlen != length && length > 0) {
  233. var g:flash.display.Graphics = minTicks.graphics;
  234. g.clear();
  235. g.lineStyle(1, this.minor_tick_color, 1, true, flash.display.LineScaleMode.NONE,
  236. flash.display.CapsStyle.ROUND, flash.display.JointStyle.ROUND);
  237. var i: Int = 0;
  238. while( (i+=10) < length ) if (i%60!=0) {
  239. var x = width*(i/length)-3;
  240. g.moveTo(x, 0);
  241. g.lineTo(x, 10);
  242. }
  243. g.lineStyle(3, this.major_tick_color, 1, true, flash.display.LineScaleMode.NORMAL,
  244. flash.display.CapsStyle.ROUND, flash.display.JointStyle.ROUND);
  245. i = 0;
  246. while( (i+=60) < length ) {
  247. var x = width*(i/length)-3;
  248. g.moveTo(x, 0);
  249. g.lineTo(x, 10);
  250. }
  251. }
  252. }
  253. public override function setReady(ready: Float) {
  254. super.setReady(ready);
  255. if (length > 0) rectReady.scaleX = ready / length;
  256. }
  257. public override function setPosition(pos: Float) {
  258. super.setPosition(pos);
  259. if (length > 0) rectMark.x = width*(position/length)-3;
  260. }
  261. public override function drawStopped() {
  262. timer.stop();
  263. GuiMini.drawStopped();
  264. }
  265. public override function drawBuffering() {
  266. timer.stop();
  267. GuiMini.drawBuffering();
  268. }
  269. public override function drawPlaying() {
  270. lastTime = haxe.Timer.stamp();
  271. timer.reset();
  272. timer.start();
  273. GuiMini.drawPlaying();
  274. }
  275. public override function drawPaused() {
  276. timer.stop();
  277. GuiMini.drawPaused();
  278. }
  279. function delay(evt : flash.events.TimerEvent) {
  280. var ts = haxe.Timer.stamp();
  281. setPosition(position + (ts-lastTime));
  282. lastTime = ts;
  283. }
  284. function proxyEvent(event:flash.events.Event) {
  285. dispatchEvent(event);
  286. }
  287. function handleClicked(event:flash.events.MouseEvent) {
  288. var pos: Float = Math.max(0.0, Math.min(1.0, (event.stageX-sprite.x)/width));
  289. trace("Clicked to "+(event.stageX-sprite.x)+" from "+width+" pos="+pos);
  290. dispatchEvent(new WavPlayerGuiEvent(WavPlayerGuiEvent.SEEKING, pos*length));
  291. }
  292. }
  293. // Main user interface: play / stop buttons & ExternalInterface
  294. class WavPlayer {
  295. static var Version = "1.9.0";
  296. static var player : IPlayer;
  297. static var wavplayer : Player;
  298. static var mp3player : Mp3Player;
  299. static var state : String = PlayerEvent.STOPPED;
  300. static var handlers : List<JsEventHandler>;
  301. static var handlerId : Int;
  302. static var lastNotifyProgress : Float;
  303. static var lastNotifyLoad : Float;
  304. static var iface : WavPlayerGui;
  305. static function main() {
  306. trace("WavPlayer "+Version+" - startup");
  307. var myMenu = new flash.ui.ContextMenu();
  308. var ciVer = new flash.ui.ContextMenuItem("WavPlayer "+Version);
  309. var ciCop = new flash.ui.ContextMenuItem("Licensed under GPL");
  310. myMenu.customItems.push(ciVer);
  311. myMenu.customItems.push(ciCop);
  312. var fvs : Dynamic<String> = flash.Lib.current.loaderInfo.parameters;
  313. handlers = new List<JsEventHandler>();
  314. handlerId = 0;
  315. lastNotifyProgress = 0;
  316. lastNotifyLoad = 0;
  317. var volume: Float = 1.0;
  318. var pan: Float = 0.0;
  319. if (fvs.volume != null) volume = Std.parseFloat(fvs.volume);
  320. if (fvs.pan != null) pan = Std.parseFloat(fvs.pan);
  321. var zoom:Float = Std.parseInt(fvs.h); zoom = (zoom>0?zoom:40.0) / 40.0;
  322. var bg_color: Int = 0x303030;
  323. var ready_color: Int = 0xA0A0A0;
  324. var cursor_color: Int = 0x7FA03F;
  325. var button_color: Int = 0x808080;
  326. var minor_tick_color: Int = 0x006600;
  327. var major_tick_color: Int = 0x000066;
  328. if (fvs.bg_color != null) bg_color = Std.parseInt(fvs.bg_color);
  329. if (fvs.ready_color != null) ready_color = Std.parseInt(fvs.ready_color);
  330. if (fvs.cursor_color != null) cursor_color = Std.parseInt(fvs.cursor_color);
  331. if (fvs.button_color != null) button_color = Std.parseInt(fvs.button_color);
  332. if (fvs.minor_tick_color != null) minor_tick_color = Std.parseInt(fvs.minor_tick_color);
  333. if (fvs.major_tick_color != null) major_tick_color = Std.parseInt(fvs.major_tick_color);
  334. if (fvs.gui == "full") {
  335. var width:Float = Std.parseInt(fvs.w); width = (width>0?width:40.0) / zoom / 40.0;
  336. iface = new WavPlayerGui_Full(flash.Lib.current, myMenu, zoom, width-1, bg_color, ready_color,
  337. cursor_color, button_color,
  338. minor_tick_color, major_tick_color);
  339. } else if(fvs.gui == "none") {
  340. iface = new WavPlayerGui_None(flash.Lib.current, myMenu);
  341. } else {
  342. iface = new WavPlayerGui_Mini(flash.Lib.current, myMenu, zoom, 0, 0, button_color);
  343. }
  344. iface.addEventListener(WavPlayerGuiEvent.CLICKED, handleClicked);
  345. iface.addEventListener(WavPlayerGuiEvent.DBLCLICKED, handleDblClicked);
  346. iface.addEventListener(WavPlayerGuiEvent.SEEKING, handleSeeking);
  347. trace("WavPlayer - gui started " + iface);
  348. iface.drawStopped();
  349. initPlayerForUrl(fvs.sound);
  350. player.volume = volume;
  351. player.pan = pan;
  352. if( !flash.external.ExternalInterface.available )
  353. throw "External Interface not available";
  354. try flash.external.ExternalInterface.addCallback("getVersion",doGetVer) catch( e : Dynamic ) {};
  355. try flash.external.ExternalInterface.addCallback("doPlay",doPlay) catch( e : Dynamic ) {};
  356. try flash.external.ExternalInterface.addCallback("play",doPlay) catch( e : Dynamic ) {};
  357. try flash.external.ExternalInterface.addCallback("doStop",doStop) catch( e : Dynamic ) {};
  358. try flash.external.ExternalInterface.addCallback("stop",doStop) catch( e : Dynamic ) {};
  359. try flash.external.ExternalInterface.addCallback("doPause",doPause) catch( e : Dynamic ) {};
  360. try flash.external.ExternalInterface.addCallback("pause",doPause) catch( e : Dynamic ) {};
  361. try flash.external.ExternalInterface.addCallback("doResume",doResume) catch( e : Dynamic ) {};
  362. try flash.external.ExternalInterface.addCallback("resume",doResume) catch( e : Dynamic ) {};
  363. try flash.external.ExternalInterface.addCallback("doSeek",doSeek) catch( e : Dynamic ) {};
  364. try flash.external.ExternalInterface.addCallback("seek",doSeek) catch( e : Dynamic ) {};
  365. try flash.external.ExternalInterface.addCallback("volume",doVolume) catch ( e : Dynamic ) {};
  366. try flash.external.ExternalInterface.addCallback("setVolume",doVolume) catch ( e : Dynamic ) {};
  367. try flash.external.ExternalInterface.addCallback("getVolume",doVolume) catch ( e : Dynamic ) {};
  368. try flash.external.ExternalInterface.addCallback("pan",doPan) catch ( e : Dynamic ) {};
  369. try flash.external.ExternalInterface.addCallback("setPan",doPan) catch ( e : Dynamic ) {};
  370. try flash.external.ExternalInterface.addCallback("getPan",doPan) catch ( e : Dynamic ) {};
  371. try flash.external.ExternalInterface.addCallback("attachHandler",doAttach) catch ( e : Dynamic ) {};
  372. try flash.external.ExternalInterface.addCallback("detachHandler",doDetach) catch ( e : Dynamic ) {};
  373. try flash.external.ExternalInterface.addCallback("removeHandler",doRemove) catch ( e : Dynamic ) {};
  374. //calls a callback indicating that this player is ready
  375. if(fvs.id != null)
  376. flash.external.ExternalInterface.call("onWavPlayerReady", fvs.id);
  377. else
  378. flash.external.ExternalInterface.call("onWavPlayerReady", flash.external.ExternalInterface.objectID);
  379. }
  380. static function initPlayerForUrl(?path: String) {
  381. if(path == null && player != null) return;
  382. if(path != null && (~/[.]mp3$/i).match(path)) {
  383. if(mp3player == null) {
  384. mp3player = new Mp3Player(path);
  385. AddPlayerEventListeners(mp3player);
  386. }
  387. player = mp3player;
  388. }
  389. else {
  390. if(wavplayer == null) {
  391. wavplayer = new Player(path);
  392. AddPlayerEventListeners(wavplayer);
  393. }
  394. player = wavplayer;
  395. }
  396. }
  397. static function AddPlayerEventListeners(dispatcher: flash.events.EventDispatcher) {
  398. dispatcher.addEventListener(PlayerEvent.BUFFERING, handleBuffering);
  399. dispatcher.addEventListener(PlayerEvent.PLAYING, handlePlaying);
  400. dispatcher.addEventListener(PlayerEvent.STOPPED, handleStopped);
  401. dispatcher.addEventListener(PlayerEvent.PAUSED, handlePaused);
  402. dispatcher.addEventListener(flash.events.ProgressEvent.PROGRESS, handleProgress);
  403. dispatcher.addEventListener(flash.events.IOErrorEvent.IO_ERROR, handleError);
  404. dispatcher.addEventListener(PlayerLoadEvent.LOAD, handleLoad);
  405. }
  406. static function handleSeeking(event:WavPlayerGuiEvent) {
  407. player.seek(event.position);
  408. }
  409. static function handleClicked(event:flash.events.Event) {
  410. trace("Clicked event: "+event);
  411. switch( state ) {
  412. case PlayerEvent.STOPPED: player.play();
  413. case PlayerEvent.BUFFERING: player.stop();
  414. case PlayerEvent.PLAYING: player.pause();
  415. case PlayerEvent.PAUSED: player.resume();
  416. }
  417. }
  418. static function handleDblClicked(event:flash.events.Event) {
  419. trace("DoubleClick event: "+event);
  420. player.stop();
  421. }
  422. static function handleBuffering(event:PlayerEvent) {
  423. trace("Buffering event: "+event);
  424. state = event.type;
  425. if (event.position!=null) iface.setPosition(event.position);
  426. iface.drawBuffering();
  427. fireJsEvent(event.type, event.position);
  428. }
  429. static function handleError(event:flash.events.IOErrorEvent) {
  430. trace("Error event: "+event);
  431. fireJsEvent(event.type);
  432. }
  433. static function handlePlaying(event:PlayerEvent) {
  434. trace("Playing event: "+event);
  435. state = event.type;
  436. if (event.position!=null) iface.setPosition(event.position);
  437. iface.drawPlaying();
  438. fireJsEvent(event.type, event.position);
  439. }
  440. static function handleStopped(event:PlayerEvent) {
  441. trace("Stopped event: "+event);
  442. state = event.type;
  443. if (event.position!=null) iface.setPosition(event.position);
  444. iface.drawStopped();
  445. fireJsEvent(event.type, event.position);
  446. }
  447. static function handlePaused(event:PlayerEvent) {
  448. trace("Paused event: "+event);
  449. state = event.type;
  450. if (event.position!=null) iface.setPosition(event.position);
  451. iface.drawPaused();
  452. fireJsEvent(event.type, event.position);
  453. }
  454. static function handleLoad(event:PlayerLoadEvent) {
  455. trace("Load event: "+event);
  456. var now = Date.now().getTime();
  457. iface.setLength(event.SecondsTotal);
  458. iface.setReady(event.SecondsLoaded);
  459. if (lastNotifyLoad==0 || event.SecondsTotal-event.SecondsLoaded < 1e-4 || now - lastNotifyLoad > 500) {
  460. lastNotifyLoad = now;
  461. fireJsEvent(event.type, event.SecondsLoaded, event.SecondsTotal);
  462. }
  463. }
  464. static function handleProgress(event:flash.events.ProgressEvent) {
  465. trace("Progress event: "+event);
  466. var now = Date.now().getTime();
  467. if (lastNotifyProgress==0 || event.bytesLoaded == event.bytesTotal || now - lastNotifyProgress > 500) {
  468. lastNotifyProgress = now;
  469. fireJsEvent(event.type, event.bytesLoaded, event.bytesTotal);
  470. }
  471. }
  472. static function doGetVer( ) {
  473. return Version;
  474. }
  475. static function doPlay( ?fname: String, ?buffer: Float ) {
  476. player.stop();
  477. lastNotifyProgress = 0;
  478. lastNotifyLoad = 0;
  479. iface.setPosition(0);
  480. initPlayerForUrl(fname);
  481. player.play(fname, buffer);
  482. }
  483. static function doStop( ) {
  484. player.stop();
  485. }
  486. static function doPause( ) {
  487. player.pause();
  488. }
  489. static function doResume( ) {
  490. player.resume();
  491. }
  492. static function doSeek( ?pos: Float ) {
  493. player.seek(pos);
  494. }
  495. static function doVolume( ?volume: Float ): Float {
  496. if (volume != null) {
  497. player.volume = volume;
  498. }
  499. trace("doVolume("+volume+")");
  500. return player.volume;
  501. }
  502. static function doPan( ?pan: Float ): Float {
  503. if (pan != null) {
  504. player.pan = pan;
  505. }
  506. trace("doPan("+pan+")");
  507. return player.pan;
  508. }
  509. static function doAttach( event: String, handler: String, ?user: String ) {
  510. var id = handlerId++;
  511. handlers.push(new JsEventHandler(id, event, handler, user));
  512. return id;
  513. }
  514. static function doDetach( event: String, handler: String, ?user: String ) {
  515. handlers = handlers.filter(function(h: JsEventHandler): Bool {
  516. return !(h.Event==event && h.Handler == handler && h.User==user);
  517. });
  518. }
  519. static function doRemove( handler: Int ) {
  520. handlers = handlers.filter(function(h: JsEventHandler): Bool {
  521. return h.Id != handler;
  522. });
  523. }
  524. static function fireJsEvent( event: String, ?p1: Dynamic, ?p2: Dynamic) {
  525. for (h in handlers) {
  526. if (h.Event == event) {
  527. if (h.User != null) flash.external.ExternalInterface.call(h.Handler, h.User, p1, p2);
  528. else flash.external.ExternalInterface.call(h.Handler, p1, p2);
  529. } else
  530. if (h.Event == '*') {
  531. if (h.User != null) flash.external.ExternalInterface.call(h.Handler, event, h.User, p1, p2);
  532. else flash.external.ExternalInterface.call(h.Handler, event, p1, p2);
  533. }
  534. }
  535. }
  536. }