PageRenderTime 1396ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/backend/web/filemanager/js/jPlayer/actionscript/Jplayer.as

https://gitlab.com/angrydevops/workerpro
ActionScript | 638 lines | 522 code | 77 blank | 39 comment | 74 complexity | 2327f5ccfd55cba2cc9809ec178092c3 MD5 | raw file
  1. /*
  2. * jPlayer Plugin for jQuery JavaScript Library
  3. * http://www.jplayer.org
  4. *
  5. * Copyright (c) 2009 - 2013 Happyworm Ltd
  6. * Licensed under the MIT.
  7. * http://opensource.org/licenses/MIT
  8. *
  9. * Author: Mark J Panaghiston
  10. * Version: 2.4.0
  11. * Date: 5th June 2013
  12. *
  13. * FlashVars expected: (AS3 property of: loaderInfo.parameters)
  14. * id: (URL Encoded: String) Id of jPlayer instance
  15. * vol: (Number) Sets the initial volume
  16. * muted: (Boolean in a String) Sets the initial muted state
  17. * jQuery: (URL Encoded: String) Sets the jQuery var name. Used with: someVar = jQuery.noConflict(true); The someVar name must contain jQuery in it.
  18. *
  19. * Compiled using: Adobe Flex Compiler (mxmlc) Version 4.5.1 build 21328
  20. */
  21. package {
  22. import flash.system.Security;
  23. import flash.external.ExternalInterface;
  24. import flash.utils.Timer;
  25. import flash.events.TimerEvent;
  26. import flash.text.TextField;
  27. import flash.text.TextFormat;
  28. import flash.events.KeyboardEvent;
  29. import flash.display.Sprite;
  30. import happyworm.jPlayer.*;
  31. import flash.display.StageAlign;
  32. import flash.display.StageScaleMode;
  33. import flash.events.Event;
  34. import flash.net.LocalConnection;
  35. import flash.events.StatusEvent;
  36. import flash.events.MouseEvent;
  37. import flash.ui.ContextMenu;
  38. import flash.ui.ContextMenuItem;
  39. import flash.events.ContextMenuEvent;
  40. import flash.net.URLRequest;
  41. import flash.net.navigateToURL;
  42. import flash.media.Video;
  43. public class Jplayer extends Sprite {
  44. private var jQuery:String;
  45. private var sentNumberFractionDigits:uint = 2;
  46. public var commonStatus:JplayerStatus = new JplayerStatus(); // Used for inital ready event so volume is correct.
  47. private var myInitTimer:Timer = new Timer(100, 0);
  48. private var myMp3Player:JplayerMp3;
  49. private var myMp4Player:JplayerMp4;
  50. private var myRtmpPlayer:JplayerRtmp;
  51. private var isRtmp:Boolean = false;
  52. private var isMp4:Boolean = false;
  53. private var isMp3:Boolean = false;
  54. private var isVideo:Boolean = false;
  55. private var securityIssue:Boolean = false; // On direct access and when SWF parameters contain illegal characters
  56. private var contextMenuFix:Sprite = new Sprite(); // A sprite that will be on top and fix the content menu over video bug. (A Flash plugin bug.)
  57. private var txLog:TextField;
  58. private var debug:Boolean = false; // Set debug to false for release compile!
  59. private var localAIRDebug:Boolean = false; // This is autodetermined by AIR app - leave false!
  60. private var traceOut:TraceOut;
  61. public function Jplayer() {
  62. flash.system.Security.allowDomain("*");
  63. traceOut = new TraceOut();
  64. // Fix to the security exploit reported by Jason Calvert http://appsec.ws/
  65. checkFlashVars(loaderInfo.parameters);
  66. stage.scaleMode = StageScaleMode.NO_SCALE;
  67. stage.align = StageAlign.TOP_LEFT;
  68. if(!securityIssue) {
  69. // The jQuery param is the primary cause of security concerns.
  70. jQuery = loaderInfo.parameters.jQuery + "('#" + loaderInfo.parameters.id + "').jPlayer";
  71. commonStatus.volume = Number(loaderInfo.parameters.vol);
  72. commonStatus.muted = loaderInfo.parameters.muted == "true";
  73. stage.addEventListener(Event.RESIZE, resizeHandler);
  74. stage.addEventListener(MouseEvent.CLICK, clickHandler);
  75. var initialVolume:Number = commonStatus.volume;
  76. if(commonStatus.muted) {
  77. initialVolume = 0;
  78. }
  79. myMp3Player = new JplayerMp3(initialVolume);
  80. addChild(myMp3Player);
  81. myMp4Player = new JplayerMp4(initialVolume);
  82. addChild(myMp4Player);
  83. myRtmpPlayer = new JplayerRtmp(initialVolume);
  84. addChild(myRtmpPlayer);
  85. switchType("mp3"); // set default state to mp3
  86. }
  87. // The ContextMenu only partially works. The menu select events never occur.
  88. // Investigated and it is something to do with the way jPlayer inserts the Flash on the page.
  89. // A simple test inserting the Jplayer.swf on a page using: 1) SWFObject 2.2 works. 2) AC_FL_RunContent() works.
  90. // jPlayer Flash insertion is based on SWFObject 2.2 and the resaon behind this failure is not clear. The Flash insertion HTML on the page looks similar.
  91. var myContextMenu:ContextMenu = new ContextMenu();
  92. myContextMenu.hideBuiltInItems();
  93. var menuItem_jPlayer:ContextMenuItem = new ContextMenuItem("jPlayer " + JplayerStatus.VERSION);
  94. var menuItem_happyworm:ContextMenuItem = new ContextMenuItem("© 2009-2013 Happyworm Ltd", true);
  95. menuItem_jPlayer.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, menuSelectHandler_jPlayer);
  96. menuItem_happyworm.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, menuSelectHandler_happyworm);
  97. myContextMenu.customItems.push(menuItem_jPlayer, menuItem_happyworm);
  98. contextMenu = myContextMenu;
  99. // Log console for dev compile option: debug
  100. if(debug || securityIssue) {
  101. txLog = new TextField();
  102. txLog.x = 5;
  103. txLog.y = 5;
  104. txLog.width = stage.stageWidth - 10;
  105. txLog.height = stage.stageHeight - 10;
  106. txLog.backgroundColor = 0xEEEEFF;
  107. txLog.border = true;
  108. txLog.background = true;
  109. txLog.multiline = true;
  110. txLog.text = "jPlayer " + JplayerStatus.VERSION;
  111. if(securityIssue) {
  112. txLog.visible = true;
  113. } else if(debug) {
  114. txLog.visible = false;
  115. }
  116. this.addChild(txLog);
  117. if(debug && !securityIssue) {
  118. this.stage.addEventListener(KeyboardEvent.KEY_UP, keyboardHandler);
  119. myMp3Player.addEventListener(JplayerEvent.DEBUG_MSG, debugMsgHandler);
  120. myMp4Player.addEventListener(JplayerEvent.DEBUG_MSG, debugMsgHandler);
  121. myRtmpPlayer.addEventListener(JplayerEvent.DEBUG_MSG, debugMsgHandler);
  122. }
  123. }
  124. // Known Flash problem with contextMenu over video player.
  125. // Add a transparent rectangle into the sprite.
  126. contextMenuFix.graphics.beginFill(0x000000, 0); // Transparent black
  127. contextMenuFix.graphics.drawRect(0, 0, 10, 10); // Arbitary rectangle
  128. contextMenuFix.graphics.endFill();
  129. addChild(contextMenuFix); // Put the sprite on the top layer.
  130. if(!securityIssue) {
  131. // Delay init() because Firefox 3.5.7+ developed a bug with local testing in Firebug.
  132. myInitTimer.addEventListener(TimerEvent.TIMER, init);
  133. myInitTimer.start();
  134. }
  135. }
  136. private function switchType(playType:String):void {
  137. switch(playType) {
  138. case "rtmpa":
  139. isRtmp=true;
  140. isMp3=false;
  141. isMp4=false;
  142. isVideo=false;
  143. break;
  144. case "rtmpv":
  145. isRtmp=true;
  146. isMp3=false;
  147. isMp4=false;
  148. isVideo=true;
  149. break;
  150. case "mp3":
  151. isRtmp=false;
  152. isMp3=true;
  153. isMp4=false;
  154. isVideo=false;
  155. break;
  156. case "mp4":
  157. isRtmp=false;
  158. isMp3=false;
  159. isMp4=true;
  160. isVideo=false;
  161. break;
  162. case "m4v":
  163. isRtmp=false;
  164. isMp3=false;
  165. isMp4=true;
  166. isVideo=true;
  167. break;
  168. }
  169. listenToMp3(isMp3);
  170. listenToMp4(isMp4);
  171. listenToRtmp(isRtmp);
  172. }
  173. private function init(e:TimerEvent):void {
  174. myInitTimer.stop();
  175. if(ExternalInterface.available && !securityIssue) {
  176. ExternalInterface.addCallback("fl_setAudio_mp3", fl_setAudio_mp3);
  177. ExternalInterface.addCallback("fl_setAudio_m4a", fl_setAudio_m4a);
  178. ExternalInterface.addCallback("fl_setVideo_m4v", fl_setVideo_m4v);
  179. ExternalInterface.addCallback("fl_setAudio_rtmp", fl_setAudio_rtmp);
  180. ExternalInterface.addCallback("fl_setVideo_rtmp", fl_setVideo_rtmp);
  181. ExternalInterface.addCallback("fl_clearMedia", fl_clearMedia);
  182. ExternalInterface.addCallback("fl_load", fl_load);
  183. ExternalInterface.addCallback("fl_play", fl_play);
  184. ExternalInterface.addCallback("fl_pause", fl_pause);
  185. ExternalInterface.addCallback("fl_play_head", fl_play_head);
  186. ExternalInterface.addCallback("fl_volume", fl_volume);
  187. ExternalInterface.addCallback("fl_mute", fl_mute);
  188. ExternalInterface.call(jQuery, "jPlayerFlashEvent", JplayerEvent.JPLAYER_READY, extractStatusData(commonStatus)); // See JplayerStatus() class for version number.
  189. }
  190. }
  191. private function checkFlashVars(p:Object):void {
  192. // Check for direct access. Inspired by mediaelement.js - Also added name to HTML object for non-IE browsers.
  193. if(ExternalInterface.objectID != null && ExternalInterface.objectID.toString() != "") {
  194. for each (var s:String in p) {
  195. if(illegalChar(s)) {
  196. securityIssue = true; // Found a security concern.
  197. }
  198. }
  199. if(!securityIssue) {
  200. if(jQueryIllegal(p.jQuery)) {
  201. securityIssue = true; // Found a security concern.
  202. }
  203. }
  204. } else {
  205. securityIssue = true; // Direct access disables the callbacks, which were a security concern.
  206. }
  207. }
  208. private function illegalChar(s:String):Boolean {
  209. // A whitelist of accepted chars.
  210. var validParam:RegExp = /^[-A-Za-z0-9_.]+$/;
  211. return !validParam.test(s);
  212. }
  213. private function jQueryIllegal(s:String):Boolean {
  214. // Check param contains the term jQuery.
  215. var validParam:RegExp = /(jQuery)/;
  216. return !validParam.test(s);
  217. }
  218. // switchType() here
  219. private function listenToMp3(active:Boolean):void {
  220. if(active) {
  221. myMp3Player.addEventListener(JplayerEvent.JPLAYER_ERROR, jPlayerFlashEvent);
  222. myMp3Player.addEventListener(JplayerEvent.JPLAYER_PROGRESS, jPlayerFlashEvent);
  223. myMp3Player.addEventListener(JplayerEvent.JPLAYER_TIMEUPDATE, jPlayerFlashEvent);
  224. myMp3Player.addEventListener(JplayerEvent.JPLAYER_ENDED, jPlayerFlashEvent);
  225. myMp3Player.addEventListener(JplayerEvent.JPLAYER_PLAY, jPlayerFlashEvent);
  226. myMp3Player.addEventListener(JplayerEvent.JPLAYER_PAUSE, jPlayerFlashEvent);
  227. myMp3Player.addEventListener(JplayerEvent.JPLAYER_LOADSTART, jPlayerFlashEvent);
  228. myMp3Player.addEventListener(JplayerEvent.JPLAYER_SEEKING, jPlayerFlashEvent);
  229. myMp3Player.addEventListener(JplayerEvent.JPLAYER_SEEKED, jPlayerFlashEvent);
  230. myMp3Player.addEventListener(JplayerEvent.JPLAYER_WAITING, jPlayerFlashEvent); // only MP3 atm
  231. myMp3Player.addEventListener(JplayerEvent.JPLAYER_PLAYING, jPlayerFlashEvent); // only MP3 atm
  232. myMp3Player.addEventListener(JplayerEvent.JPLAYER_CANPLAY, jPlayerFlashEvent); // only MP3 atm
  233. myMp3Player.addEventListener(JplayerEvent.JPLAYER_CANPLAYTHROUGH, jPlayerFlashEvent); // only MP3 atm
  234. } else {
  235. myMp3Player.removeEventListener(JplayerEvent.JPLAYER_ERROR, jPlayerFlashEvent);
  236. myMp3Player.removeEventListener(JplayerEvent.JPLAYER_PROGRESS, jPlayerFlashEvent);
  237. myMp3Player.removeEventListener(JplayerEvent.JPLAYER_TIMEUPDATE, jPlayerFlashEvent);
  238. myMp3Player.removeEventListener(JplayerEvent.JPLAYER_ENDED, jPlayerFlashEvent);
  239. myMp3Player.removeEventListener(JplayerEvent.JPLAYER_PLAY, jPlayerFlashEvent);
  240. myMp3Player.removeEventListener(JplayerEvent.JPLAYER_PAUSE, jPlayerFlashEvent);
  241. myMp3Player.removeEventListener(JplayerEvent.JPLAYER_LOADSTART, jPlayerFlashEvent);
  242. myMp3Player.removeEventListener(JplayerEvent.JPLAYER_SEEKING, jPlayerFlashEvent);
  243. myMp3Player.removeEventListener(JplayerEvent.JPLAYER_SEEKED, jPlayerFlashEvent);
  244. myMp3Player.removeEventListener(JplayerEvent.JPLAYER_WAITING, jPlayerFlashEvent); // only MP3 atm
  245. myMp3Player.removeEventListener(JplayerEvent.JPLAYER_PLAYING, jPlayerFlashEvent); // only MP3 atm
  246. myMp3Player.removeEventListener(JplayerEvent.JPLAYER_CANPLAY, jPlayerFlashEvent); // only MP3 atm
  247. myMp3Player.removeEventListener(JplayerEvent.JPLAYER_CANPLAYTHROUGH, jPlayerFlashEvent); // only MP3 atm
  248. }
  249. }
  250. private function listenToMp4(active:Boolean):void {
  251. if(active) {
  252. myMp4Player.addEventListener(JplayerEvent.JPLAYER_ERROR, jPlayerFlashEvent);
  253. myMp4Player.addEventListener(JplayerEvent.JPLAYER_PROGRESS, jPlayerFlashEvent);
  254. myMp4Player.addEventListener(JplayerEvent.JPLAYER_TIMEUPDATE, jPlayerFlashEvent);
  255. myMp4Player.addEventListener(JplayerEvent.JPLAYER_ENDED, jPlayerFlashEvent);
  256. myMp4Player.addEventListener(JplayerEvent.JPLAYER_PLAY, jPlayerFlashEvent);
  257. myMp4Player.addEventListener(JplayerEvent.JPLAYER_PAUSE, jPlayerFlashEvent);
  258. myMp4Player.addEventListener(JplayerEvent.JPLAYER_LOADSTART, jPlayerFlashEvent);
  259. myMp4Player.addEventListener(JplayerEvent.JPLAYER_SEEKING, jPlayerFlashEvent);
  260. myMp4Player.addEventListener(JplayerEvent.JPLAYER_SEEKED, jPlayerFlashEvent);
  261. myMp4Player.addEventListener(JplayerEvent.JPLAYER_LOADEDMETADATA, jPlayerMetaDataHandler); // Note the unique handler
  262. } else {
  263. myMp4Player.removeEventListener(JplayerEvent.JPLAYER_ERROR, jPlayerFlashEvent);
  264. myMp4Player.removeEventListener(JplayerEvent.JPLAYER_PROGRESS, jPlayerFlashEvent);
  265. myMp4Player.removeEventListener(JplayerEvent.JPLAYER_TIMEUPDATE, jPlayerFlashEvent);
  266. myMp4Player.removeEventListener(JplayerEvent.JPLAYER_ENDED, jPlayerFlashEvent);
  267. myMp4Player.removeEventListener(JplayerEvent.JPLAYER_PLAY, jPlayerFlashEvent);
  268. myMp4Player.removeEventListener(JplayerEvent.JPLAYER_PAUSE, jPlayerFlashEvent);
  269. myMp4Player.removeEventListener(JplayerEvent.JPLAYER_LOADSTART, jPlayerFlashEvent);
  270. myMp4Player.removeEventListener(JplayerEvent.JPLAYER_SEEKING, jPlayerFlashEvent);
  271. myMp4Player.removeEventListener(JplayerEvent.JPLAYER_SEEKED, jPlayerFlashEvent);
  272. myMp4Player.removeEventListener(JplayerEvent.JPLAYER_LOADEDMETADATA, jPlayerMetaDataHandler); // Note the unique handler
  273. }
  274. }
  275. private function listenToRtmp(active:Boolean):void {
  276. if(active) {
  277. myRtmpPlayer.addEventListener(JplayerEvent.JPLAYER_ERROR, jPlayerFlashEvent);
  278. myRtmpPlayer.addEventListener(JplayerEvent.JPLAYER_PROGRESS, jPlayerFlashEvent);
  279. myRtmpPlayer.addEventListener(JplayerEvent.JPLAYER_TIMEUPDATE, jPlayerFlashEvent);
  280. myRtmpPlayer.addEventListener(JplayerEvent.JPLAYER_ENDED, jPlayerFlashEvent);
  281. myRtmpPlayer.addEventListener(JplayerEvent.JPLAYER_PLAY, jPlayerFlashEvent);
  282. myRtmpPlayer.addEventListener(JplayerEvent.JPLAYER_PAUSE, jPlayerFlashEvent);
  283. myRtmpPlayer.addEventListener(JplayerEvent.JPLAYER_LOADSTART, jPlayerFlashEvent);
  284. myRtmpPlayer.addEventListener(JplayerEvent.JPLAYER_CANPLAY, jPlayerFlashEvent);
  285. myRtmpPlayer.addEventListener(JplayerEvent.JPLAYER_SEEKING, jPlayerFlashEvent);
  286. myRtmpPlayer.addEventListener(JplayerEvent.JPLAYER_SEEKED, jPlayerFlashEvent);
  287. myRtmpPlayer.addEventListener(JplayerEvent.JPLAYER_LOADEDMETADATA, jPlayerMetaDataHandler); // Note the unique handler
  288. } else {
  289. myRtmpPlayer.removeEventListener(JplayerEvent.JPLAYER_ERROR, jPlayerFlashEvent);
  290. myRtmpPlayer.removeEventListener(JplayerEvent.JPLAYER_PROGRESS, jPlayerFlashEvent);
  291. myRtmpPlayer.removeEventListener(JplayerEvent.JPLAYER_TIMEUPDATE, jPlayerFlashEvent);
  292. myRtmpPlayer.removeEventListener(JplayerEvent.JPLAYER_ENDED, jPlayerFlashEvent);
  293. myRtmpPlayer.removeEventListener(JplayerEvent.JPLAYER_PLAY, jPlayerFlashEvent);
  294. myRtmpPlayer.removeEventListener(JplayerEvent.JPLAYER_PAUSE, jPlayerFlashEvent);
  295. myRtmpPlayer.removeEventListener(JplayerEvent.JPLAYER_LOADSTART, jPlayerFlashEvent);
  296. myRtmpPlayer.addEventListener(JplayerEvent.JPLAYER_CANPLAY, jPlayerFlashEvent);
  297. myRtmpPlayer.removeEventListener(JplayerEvent.JPLAYER_SEEKING, jPlayerFlashEvent);
  298. myRtmpPlayer.removeEventListener(JplayerEvent.JPLAYER_SEEKED, jPlayerFlashEvent);
  299. myRtmpPlayer.removeEventListener(JplayerEvent.JPLAYER_LOADEDMETADATA, jPlayerMetaDataHandler); // Note the unique handler
  300. }
  301. }
  302. private function fl_setAudio_mp3(src:String):Boolean {
  303. if (src != null) {
  304. log("fl_setAudio_mp3: "+src);
  305. switchType("mp3");
  306. myMp4Player.clearFile();
  307. myRtmpPlayer.clearFile();
  308. myMp3Player.setFile(src);
  309. return true;
  310. } else {
  311. log("fl_setAudio_mp3: null");
  312. return false;
  313. }
  314. }
  315. private function fl_setAudio_rtmp(src:String):Boolean {
  316. tracer("SET RTMP: "+src);
  317. if (src != null) {
  318. log("fl_setAudio_rtmp: "+src);
  319. switchType("rtmpa");
  320. myMp4Player.clearFile();
  321. myMp3Player.clearFile();
  322. myRtmpPlayer.setFile(src,false);
  323. return true;
  324. } else {
  325. log("fl_setAudio_rtmp: null");
  326. return false;
  327. }
  328. }
  329. private function fl_setVideo_rtmp(src:String):Boolean {
  330. tracer("SET RTMP: "+src);
  331. if (src != null) {
  332. log("fl_setVideo_rtmp: "+src);
  333. switchType("rtmpv");
  334. myMp4Player.clearFile();
  335. myMp3Player.clearFile();
  336. myRtmpPlayer.setFile(src,true);
  337. return true;
  338. } else {
  339. log("fl_setVideo_rtmp: null");
  340. return false;
  341. }
  342. }
  343. private function fl_setAudio_m4a(src:String):Boolean {
  344. if (src != null) {
  345. log("fl_setAudio_m4a: "+src);
  346. switchType("mp4")
  347. myMp3Player.clearFile();
  348. myRtmpPlayer.clearFile();
  349. myMp4Player.setFile(src);
  350. return true;
  351. } else {
  352. log("fl_setAudio_m4a: null");
  353. return false;
  354. }
  355. }
  356. private function fl_setVideo_m4v(src:String):Boolean {
  357. if (src != null) {
  358. log("fl_setVideo_m4v: "+src);
  359. switchType("m4v");
  360. myMp3Player.clearFile();
  361. myRtmpPlayer.clearFile();
  362. myMp4Player.setFile(src);
  363. return true;
  364. } else {
  365. log("fl_setVideo_m4v: null");
  366. return false;
  367. }
  368. }
  369. private function fl_clearMedia():void {
  370. log("clearMedia.");
  371. myMp3Player.clearFile();
  372. myMp4Player.clearFile();
  373. myRtmpPlayer.clearFile();
  374. }
  375. private function getType():Object {
  376. var returnType:Object;
  377. if(isMp3) {
  378. returnType=myMp3Player;
  379. }
  380. if(isRtmp) {
  381. returnType=myRtmpPlayer;
  382. }
  383. if(isMp4) {
  384. returnType=myMp4Player;
  385. }
  386. return returnType;
  387. }
  388. private function fl_load():Boolean {
  389. log("load.");
  390. var returnType:Object = getType();
  391. return returnType.load();
  392. }
  393. private function fl_play(time:Number = NaN):Boolean {
  394. log("play: time = " + time);
  395. var returnType:Object = getType();
  396. return returnType.play(time * 1000); // Flash uses milliseconds
  397. }
  398. private function fl_pause(time:Number = NaN):Boolean {
  399. log("pause: time = " + time);
  400. var returnType:Object = getType();
  401. return returnType.pause(time * 1000); // Flash uses milliseconds
  402. }
  403. private function fl_play_head(percent:Number):Boolean {
  404. log("play_head: "+percent+"%");
  405. var returnType:Object = getType();
  406. return returnType.playHead(percent);
  407. }
  408. private function fl_volume(v:Number):void {
  409. log("volume: "+v);
  410. commonStatus.volume = v;
  411. if(!commonStatus.muted) {
  412. myMp3Player.setVolume(v);
  413. myMp4Player.setVolume(v);
  414. myRtmpPlayer.setVolume(v);
  415. }
  416. }
  417. private function fl_mute(mute:Boolean):void {
  418. log("mute: "+mute);
  419. commonStatus.muted = mute;
  420. if(mute) {
  421. myMp3Player.setVolume(0);
  422. myMp4Player.setVolume(0);
  423. myRtmpPlayer.setVolume(0);
  424. } else {
  425. myMp3Player.setVolume(commonStatus.volume);
  426. myMp4Player.setVolume(commonStatus.volume);
  427. myRtmpPlayer.setVolume(commonStatus.volume);
  428. }
  429. }
  430. private function jPlayerFlashEvent(e:JplayerEvent):void {
  431. log("jPlayer Flash Event: " + e.type + ": " + e.target);
  432. //tracer("jPlayer Flash Event: " + e.type + ": " + e.target);
  433. if(ExternalInterface.available && !securityIssue) {
  434. ExternalInterface.call(jQuery, "jPlayerFlashEvent", e.type, extractStatusData(e.data));
  435. }
  436. }
  437. private function tracer(msg:String):void {
  438. traceOut.tracer(msg);
  439. }
  440. private function extractStatusData(data:JplayerStatus):Object {
  441. var myStatus:Object = {
  442. version: JplayerStatus.VERSION,
  443. src: data.src,
  444. paused: !data.isPlaying, // Changing this name requires inverting all assignments and conditional statements.
  445. srcSet: data.srcSet,
  446. seekPercent: data.seekPercent,
  447. currentPercentRelative: data.currentPercentRelative,
  448. currentPercentAbsolute: data.currentPercentAbsolute,
  449. currentTime: data.currentTime / 1000, // JavaScript uses seconds
  450. duration: data.duration / 1000, // JavaScript uses seconds
  451. videoWidth: data.videoWidth,
  452. videoHeight: data.videoHeight,
  453. volume: commonStatus.volume,
  454. muted: commonStatus.muted
  455. };
  456. log("extractStatusData: sp="+myStatus.seekPercent+" cpr="+myStatus.currentPercentRelative+" cpa="+myStatus.currentPercentAbsolute+" ct="+myStatus.currentTime+" d="+myStatus.duration);
  457. return myStatus;
  458. }
  459. private function jPlayerMetaDataHandler(e:JplayerEvent):void {
  460. log("jPlayerMetaDataHandler:" + e.target);
  461. if(ExternalInterface.available && !securityIssue) {
  462. resizeHandler(new Event(Event.RESIZE));
  463. ExternalInterface.call(jQuery, "jPlayerFlashEvent", e.type, extractStatusData(e.data));
  464. }
  465. }
  466. private function resizeHandler(e:Event):void {
  467. log("resizeHandler: stageWidth = " + stage.stageWidth + " | stageHeight = " + stage.stageHeight);
  468. var mediaX:Number = 0;
  469. var mediaY:Number = 0;
  470. var mediaWidth:Number = 0;
  471. var mediaHeight:Number = 0;
  472. var aspectRatioStage:Number = 0;
  473. var aspectRatioVideo:Number = 0;
  474. var videoItem:*;
  475. if(isRtmp) {
  476. videoItem = myRtmpPlayer;
  477. }
  478. if(isMp4) {
  479. videoItem = myMp4Player;
  480. }
  481. if(videoItem) {
  482. if(stage.stageWidth > 0 && stage.stageHeight > 0 && videoItem.myVideo.width > 0 && videoItem.myVideo.height > 0) {
  483. aspectRatioStage = stage.stageWidth / stage.stageHeight;
  484. aspectRatioVideo = videoItem.myVideo.width / videoItem.myVideo.height;
  485. if(aspectRatioStage < aspectRatioVideo) {
  486. mediaWidth = stage.stageWidth;
  487. mediaHeight = stage.stageWidth / aspectRatioVideo;
  488. mediaX = 0;
  489. mediaY = (stage.stageHeight - mediaHeight) / 2;
  490. } else {
  491. mediaWidth = stage.stageHeight * aspectRatioVideo;
  492. mediaHeight = stage.stageHeight;
  493. mediaX = (stage.stageWidth - mediaWidth) / 2;
  494. mediaY = 0;
  495. }
  496. resizeEntity(videoItem, mediaX, mediaY, mediaWidth, mediaHeight);
  497. }
  498. }
  499. if((debug || securityIssue) && stage.stageWidth > 20 && stage.stageHeight > 20) {
  500. txLog.width = stage.stageWidth - 10;
  501. txLog.height = stage.stageHeight - 10;
  502. }
  503. // Resize the sprite so it covers the entire stage area
  504. contextMenuFix.x = 0;
  505. contextMenuFix.y = 0;
  506. contextMenuFix.width = stage.stageWidth;
  507. contextMenuFix.height = stage.stageHeight;
  508. }
  509. private function resizeEntity(entity:Sprite, mediaX:Number, mediaY:Number, mediaWidth:Number, mediaHeight:Number):void {
  510. entity.x = mediaX;
  511. entity.y = mediaY;
  512. entity.width = mediaWidth;
  513. entity.height = mediaHeight;
  514. }
  515. private function clickHandler(e:MouseEvent):void {
  516. // This needs to work with RTMP format too!
  517. if(isMp3) {
  518. jPlayerFlashEvent(new JplayerEvent(JplayerEvent.JPLAYER_CLICK, myMp3Player.myStatus, "click"))
  519. } else {
  520. jPlayerFlashEvent(new JplayerEvent(JplayerEvent.JPLAYER_CLICK, myMp4Player.myStatus, "click"))
  521. }
  522. }
  523. // This event is never called. See comments in class constructor.
  524. private function menuSelectHandler_jPlayer(e:ContextMenuEvent):void {
  525. navigateToURL(new URLRequest("http://jplayer.org/"), "_blank");
  526. }
  527. // This event is never called. See comments in class constructor.
  528. private function menuSelectHandler_happyworm(e:ContextMenuEvent):void {
  529. navigateToURL(new URLRequest("http://happyworm.com/"), "_blank");
  530. }
  531. private function log(t:String):void {
  532. if(debug) {
  533. txLog.text = t + "\n" + txLog.text;
  534. localAIRDebug = traceOut.localAIRDebug();
  535. if(localAIRDebug) {
  536. tracer(t);
  537. }
  538. if(ExternalInterface.available && !securityIssue) {
  539. ExternalInterface.call("console.log", t);
  540. }
  541. }
  542. }
  543. private function debugMsgHandler(e:JplayerEvent):void {
  544. log(e.msg);
  545. }
  546. private function keyboardHandler(e:KeyboardEvent):void {
  547. log("keyboardHandler: e.keyCode = " + e.keyCode);
  548. switch(e.keyCode) {
  549. case 68 : // d
  550. txLog.visible = !txLog.visible;
  551. log("Toggled log display: " + txLog.visible);
  552. break;
  553. case 76 : // l
  554. if(e.ctrlKey && e.shiftKey) {
  555. txLog.text = "Cleared log.";
  556. }
  557. break;
  558. }
  559. }
  560. }
  561. }