PageRenderTime 6269ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/js/jPlayer/actionscript/Jplayer.as

https://gitlab.com/alidz1982/php-mp3-jPlayer
ActionScript | 671 lines | 527 code | 106 blank | 38 comment | 77 complexity | 111a10419c99ea733beede04379ebe80 MD5 | raw file
  1. /*
  2. * jPlayer Plugin for jQuery JavaScript Library
  3. * http://www.jplayer.org
  4. *
  5. * Copyright (c) 2009 - 2014 Happyworm Ltd
  6. * Licensed under the MIT license.
  7. * http://opensource.org/licenses/MIT
  8. *
  9. * Author: Mark J Panaghiston
  10. * Version: 2.6.0
  11. * Date: 2nd April 2014
  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; // This class was found to cause problems on OSX with Firefox and Safari where more than 8 instances of the SWF are on a page.
  61. public function Jplayer() {
  62. flash.system.Security.allowDomain("*");
  63. flash.system.Security.allowInsecureDomain("*");
  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-2014 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. traceOut = new TraceOut(); // Instance it only when in debug mode. See comment above at var declaration.
  123. }
  124. }
  125. // Known Flash problem with contextMenu over video player.
  126. // Add a transparent rectangle into the sprite.
  127. contextMenuFix.graphics.beginFill(0x000000, 0); // Transparent black
  128. contextMenuFix.graphics.drawRect(0, 0, 10, 10); // Arbitary rectangle
  129. contextMenuFix.graphics.endFill();
  130. addChild(contextMenuFix); // Put the sprite on the top layer.
  131. if (!securityIssue) {
  132. // Delay init() because Firefox 3.5.7+ developed a bug with local testing in Firebug.
  133. myInitTimer.addEventListener(TimerEvent.TIMER, init);
  134. myInitTimer.start();
  135. }
  136. }
  137. private function switchType(playType:String):void {
  138. switch (playType) {
  139. case "rtmpa":
  140. isRtmp = true;
  141. isMp3 = false;
  142. isMp4 = false;
  143. isVideo = false;
  144. break;
  145. case "rtmpv":
  146. isRtmp = true;
  147. isMp3 = false;
  148. isMp4 = false;
  149. isVideo = true;
  150. break;
  151. case "mp3":
  152. isRtmp = false;
  153. isMp3 = true;
  154. isMp4 = false;
  155. isVideo = false;
  156. break;
  157. case "mp4":
  158. isRtmp = false;
  159. isMp3 = false;
  160. isMp4 = true;
  161. isVideo = false;
  162. break;
  163. case "m4v":
  164. isRtmp = false;
  165. isMp3 = false;
  166. isMp4 = true;
  167. isVideo = true;
  168. break;
  169. }
  170. listenToMp3(isMp3);
  171. listenToMp4(isMp4);
  172. listenToRtmp(isRtmp);
  173. }
  174. private function init(e:TimerEvent):void {
  175. myInitTimer.stop();
  176. if (ExternalInterface.available && !securityIssue) {
  177. ExternalInterface.addCallback("fl_setAudio_mp3", fl_setAudio_mp3);
  178. ExternalInterface.addCallback("fl_setAudio_m4a", fl_setAudio_m4a);
  179. ExternalInterface.addCallback("fl_setVideo_m4v", fl_setVideo_m4v);
  180. ExternalInterface.addCallback("fl_setAudio_rtmp", fl_setAudio_rtmp);
  181. ExternalInterface.addCallback("fl_setVideo_rtmp", fl_setVideo_rtmp);
  182. ExternalInterface.addCallback("fl_clearMedia", fl_clearMedia);
  183. ExternalInterface.addCallback("fl_load", fl_load);
  184. ExternalInterface.addCallback("fl_play", fl_play);
  185. ExternalInterface.addCallback("fl_pause", fl_pause);
  186. ExternalInterface.addCallback("fl_play_head", fl_play_head);
  187. ExternalInterface.addCallback("fl_volume", fl_volume);
  188. ExternalInterface.addCallback("fl_mute", fl_mute);
  189. ExternalInterface.call(jQuery, "jPlayerFlashEvent", JplayerEvent.JPLAYER_READY, extractStatusData(commonStatus)); // See JplayerStatus() class for version number.
  190. }
  191. }
  192. private function checkFlashVars(p:Object):void {
  193. // Check for direct access. Inspired by mediaelement.js - Also added name to HTML object for non-IE browsers.
  194. if (ExternalInterface.objectID != null && ExternalInterface.objectID.toString() != "") {
  195. for each (var s:String in p) {
  196. if (illegalChar(s)) {
  197. securityIssue = true; // Found a security concern.
  198. }
  199. }
  200. if (!securityIssue) {
  201. if (jQueryIllegal(p.jQuery)) {
  202. securityIssue = true; // Found a security concern.
  203. }
  204. }
  205. } else {
  206. securityIssue = true; // Direct access disables the callbacks, which were a security concern.
  207. }
  208. }
  209. private function illegalChar(s:String):Boolean {
  210. // A whitelist of accepted chars.
  211. var validParam:RegExp = /^[-A-Za-z0-9_.]+$/;
  212. return !validParam.test(s);
  213. }
  214. private function jQueryIllegal(s:String):Boolean {
  215. // Check param contains the term jQuery.
  216. var validParam:RegExp = /(jQuery)/;
  217. return !validParam.test(s);
  218. }
  219. // switchType() here
  220. private function listenToMp3(active:Boolean):void {
  221. if (active) {
  222. myMp3Player.addEventListener(JplayerEvent.JPLAYER_ERROR, jPlayerFlashEvent);
  223. myMp3Player.addEventListener(JplayerEvent.JPLAYER_PROGRESS, jPlayerFlashEvent);
  224. myMp3Player.addEventListener(JplayerEvent.JPLAYER_TIMEUPDATE, jPlayerFlashEvent);
  225. myMp3Player.addEventListener(JplayerEvent.JPLAYER_ENDED, jPlayerFlashEvent);
  226. myMp3Player.addEventListener(JplayerEvent.JPLAYER_PLAY, jPlayerFlashEvent);
  227. myMp3Player.addEventListener(JplayerEvent.JPLAYER_PAUSE, jPlayerFlashEvent);
  228. myMp3Player.addEventListener(JplayerEvent.JPLAYER_LOADSTART, jPlayerFlashEvent);
  229. myMp3Player.addEventListener(JplayerEvent.JPLAYER_SEEKING, jPlayerFlashEvent);
  230. myMp3Player.addEventListener(JplayerEvent.JPLAYER_SEEKED, jPlayerFlashEvent);
  231. myMp3Player.addEventListener(JplayerEvent.JPLAYER_WAITING, jPlayerFlashEvent); // only MP3 atm
  232. myMp3Player.addEventListener(JplayerEvent.JPLAYER_PLAYING, jPlayerFlashEvent); // only MP3 atm
  233. myMp3Player.addEventListener(JplayerEvent.JPLAYER_CANPLAY, jPlayerFlashEvent); // only MP3 atm
  234. myMp3Player.addEventListener(JplayerEvent.JPLAYER_CANPLAYTHROUGH, jPlayerFlashEvent); // only MP3 atm
  235. myMp3Player.addEventListener(JplayerEvent.JPLAYER_LOADEDMETADATA, jPlayerFlashEvent);
  236. myMp3Player.addEventListener(JplayerEvent.JPLAYER_DURATIONCHANGE, jPlayerFlashEvent); // only MP3 atm
  237. } else {
  238. myMp3Player.removeEventListener(JplayerEvent.JPLAYER_ERROR, jPlayerFlashEvent);
  239. myMp3Player.removeEventListener(JplayerEvent.JPLAYER_PROGRESS, jPlayerFlashEvent);
  240. myMp3Player.removeEventListener(JplayerEvent.JPLAYER_TIMEUPDATE, jPlayerFlashEvent);
  241. myMp3Player.removeEventListener(JplayerEvent.JPLAYER_ENDED, jPlayerFlashEvent);
  242. myMp3Player.removeEventListener(JplayerEvent.JPLAYER_PLAY, jPlayerFlashEvent);
  243. myMp3Player.removeEventListener(JplayerEvent.JPLAYER_PAUSE, jPlayerFlashEvent);
  244. myMp3Player.removeEventListener(JplayerEvent.JPLAYER_LOADSTART, jPlayerFlashEvent);
  245. myMp3Player.removeEventListener(JplayerEvent.JPLAYER_SEEKING, jPlayerFlashEvent);
  246. myMp3Player.removeEventListener(JplayerEvent.JPLAYER_SEEKED, jPlayerFlashEvent);
  247. myMp3Player.removeEventListener(JplayerEvent.JPLAYER_WAITING, jPlayerFlashEvent); // only MP3 atm
  248. myMp3Player.removeEventListener(JplayerEvent.JPLAYER_PLAYING, jPlayerFlashEvent); // only MP3 atm
  249. myMp3Player.removeEventListener(JplayerEvent.JPLAYER_CANPLAY, jPlayerFlashEvent); // only MP3 atm
  250. myMp3Player.removeEventListener(JplayerEvent.JPLAYER_CANPLAYTHROUGH, jPlayerFlashEvent); // only MP3 atm
  251. myMp3Player.removeEventListener(JplayerEvent.JPLAYER_LOADEDMETADATA, jPlayerFlashEvent);
  252. myMp3Player.removeEventListener(JplayerEvent.JPLAYER_DURATIONCHANGE, jPlayerFlashEvent); // only MP3 atm
  253. }
  254. }
  255. private function listenToMp4(active:Boolean):void {
  256. if (active) {
  257. myMp4Player.addEventListener(JplayerEvent.JPLAYER_ERROR, jPlayerFlashEvent);
  258. myMp4Player.addEventListener(JplayerEvent.JPLAYER_PROGRESS, jPlayerFlashEvent);
  259. myMp4Player.addEventListener(JplayerEvent.JPLAYER_TIMEUPDATE, jPlayerFlashEvent);
  260. myMp4Player.addEventListener(JplayerEvent.JPLAYER_ENDED, jPlayerFlashEvent);
  261. myMp4Player.addEventListener(JplayerEvent.JPLAYER_PLAY, jPlayerFlashEvent);
  262. myMp4Player.addEventListener(JplayerEvent.JPLAYER_PAUSE, jPlayerFlashEvent);
  263. myMp4Player.addEventListener(JplayerEvent.JPLAYER_LOADSTART, jPlayerFlashEvent);
  264. myMp4Player.addEventListener(JplayerEvent.JPLAYER_SEEKING, jPlayerFlashEvent);
  265. myMp4Player.addEventListener(JplayerEvent.JPLAYER_SEEKED, jPlayerFlashEvent);
  266. myMp4Player.addEventListener(JplayerEvent.JPLAYER_LOADEDMETADATA, jPlayerMetaDataHandler); // Note the unique handler
  267. } else {
  268. myMp4Player.removeEventListener(JplayerEvent.JPLAYER_ERROR, jPlayerFlashEvent);
  269. myMp4Player.removeEventListener(JplayerEvent.JPLAYER_PROGRESS, jPlayerFlashEvent);
  270. myMp4Player.removeEventListener(JplayerEvent.JPLAYER_TIMEUPDATE, jPlayerFlashEvent);
  271. myMp4Player.removeEventListener(JplayerEvent.JPLAYER_ENDED, jPlayerFlashEvent);
  272. myMp4Player.removeEventListener(JplayerEvent.JPLAYER_PLAY, jPlayerFlashEvent);
  273. myMp4Player.removeEventListener(JplayerEvent.JPLAYER_PAUSE, jPlayerFlashEvent);
  274. myMp4Player.removeEventListener(JplayerEvent.JPLAYER_LOADSTART, jPlayerFlashEvent);
  275. myMp4Player.removeEventListener(JplayerEvent.JPLAYER_SEEKING, jPlayerFlashEvent);
  276. myMp4Player.removeEventListener(JplayerEvent.JPLAYER_SEEKED, jPlayerFlashEvent);
  277. myMp4Player.removeEventListener(JplayerEvent.JPLAYER_LOADEDMETADATA, jPlayerMetaDataHandler); // Note the unique handler
  278. }
  279. }
  280. private function listenToRtmp(active:Boolean):void {
  281. if (active) {
  282. myRtmpPlayer.addEventListener(JplayerEvent.JPLAYER_ERROR, jPlayerFlashEvent);
  283. myRtmpPlayer.addEventListener(JplayerEvent.JPLAYER_PROGRESS, jPlayerFlashEvent);
  284. myRtmpPlayer.addEventListener(JplayerEvent.JPLAYER_TIMEUPDATE, jPlayerFlashEvent);
  285. myRtmpPlayer.addEventListener(JplayerEvent.JPLAYER_ENDED, jPlayerFlashEvent);
  286. myRtmpPlayer.addEventListener(JplayerEvent.JPLAYER_PLAY, jPlayerFlashEvent);
  287. myRtmpPlayer.addEventListener(JplayerEvent.JPLAYER_PAUSE, jPlayerFlashEvent);
  288. myRtmpPlayer.addEventListener(JplayerEvent.JPLAYER_LOADSTART, jPlayerFlashEvent);
  289. myRtmpPlayer.addEventListener(JplayerEvent.JPLAYER_CANPLAY, jPlayerFlashEvent);
  290. myRtmpPlayer.addEventListener(JplayerEvent.JPLAYER_SEEKING, jPlayerFlashEvent);
  291. myRtmpPlayer.addEventListener(JplayerEvent.JPLAYER_SEEKED, jPlayerFlashEvent);
  292. myRtmpPlayer.addEventListener(JplayerEvent.JPLAYER_LOADEDMETADATA, jPlayerMetaDataHandler); // Note the unique handler
  293. } else {
  294. myRtmpPlayer.removeEventListener(JplayerEvent.JPLAYER_ERROR, jPlayerFlashEvent);
  295. myRtmpPlayer.removeEventListener(JplayerEvent.JPLAYER_PROGRESS, jPlayerFlashEvent);
  296. myRtmpPlayer.removeEventListener(JplayerEvent.JPLAYER_TIMEUPDATE, jPlayerFlashEvent);
  297. myRtmpPlayer.removeEventListener(JplayerEvent.JPLAYER_ENDED, jPlayerFlashEvent);
  298. myRtmpPlayer.removeEventListener(JplayerEvent.JPLAYER_PLAY, jPlayerFlashEvent);
  299. myRtmpPlayer.removeEventListener(JplayerEvent.JPLAYER_PAUSE, jPlayerFlashEvent);
  300. myRtmpPlayer.removeEventListener(JplayerEvent.JPLAYER_LOADSTART, jPlayerFlashEvent);
  301. myRtmpPlayer.addEventListener(JplayerEvent.JPLAYER_CANPLAY, jPlayerFlashEvent);
  302. myRtmpPlayer.removeEventListener(JplayerEvent.JPLAYER_SEEKING, jPlayerFlashEvent);
  303. myRtmpPlayer.removeEventListener(JplayerEvent.JPLAYER_SEEKED, jPlayerFlashEvent);
  304. myRtmpPlayer.removeEventListener(JplayerEvent.JPLAYER_LOADEDMETADATA, jPlayerMetaDataHandler); // Note the unique handler
  305. }
  306. }
  307. private function fl_setAudio_mp3(src:String):Boolean {
  308. if (src != null) {
  309. log("fl_setAudio_mp3: " + src);
  310. switchType("mp3");
  311. myMp4Player.clearFile();
  312. myRtmpPlayer.clearFile();
  313. myMp3Player.setFile(src);
  314. return true;
  315. } else {
  316. log("fl_setAudio_mp3: null");
  317. return false;
  318. }
  319. }
  320. private function fl_setAudio_rtmp(src:String):Boolean {
  321. if (src != null) {
  322. log("fl_setAudio_rtmp: " + src);
  323. switchType("rtmpa");
  324. myMp4Player.clearFile();
  325. myMp3Player.clearFile();
  326. myRtmpPlayer.setFile(src, false);
  327. return true;
  328. } else {
  329. log("fl_setAudio_rtmp: null");
  330. return false;
  331. }
  332. }
  333. private function fl_setVideo_rtmp(src:String):Boolean {
  334. if (src != null) {
  335. log("fl_setVideo_rtmp: " + src);
  336. switchType("rtmpv");
  337. myMp4Player.clearFile();
  338. myMp3Player.clearFile();
  339. myRtmpPlayer.setFile(src, true);
  340. return true;
  341. } else {
  342. log("fl_setVideo_rtmp: null");
  343. return false;
  344. }
  345. }
  346. private function fl_setAudio_m4a(src:String):Boolean {
  347. if (src != null) {
  348. log("fl_setAudio_m4a: " + src);
  349. switchType("mp4")
  350. myMp3Player.clearFile();
  351. myRtmpPlayer.clearFile();
  352. myMp4Player.setFile(src);
  353. return true;
  354. } else {
  355. log("fl_setAudio_m4a: null");
  356. return false;
  357. }
  358. }
  359. private function fl_setVideo_m4v(src:String):Boolean {
  360. if (src != null) {
  361. log("fl_setVideo_m4v: " + src);
  362. switchType("m4v");
  363. myMp3Player.clearFile();
  364. myRtmpPlayer.clearFile();
  365. myMp4Player.setFile(src);
  366. return true;
  367. } else {
  368. log("fl_setVideo_m4v: null");
  369. return false;
  370. }
  371. }
  372. private function fl_clearMedia():void {
  373. log("clearMedia.");
  374. myMp3Player.clearFile();
  375. myMp4Player.clearFile();
  376. myRtmpPlayer.clearFile();
  377. }
  378. private function getType():Object {
  379. var returnType:Object;
  380. if (isMp3) {
  381. returnType = myMp3Player;
  382. }
  383. if (isRtmp) {
  384. returnType = myRtmpPlayer;
  385. }
  386. if (isMp4) {
  387. returnType = myMp4Player;
  388. }
  389. return returnType;
  390. }
  391. private function fl_load():Boolean {
  392. log("load.");
  393. var returnType:Object = getType();
  394. return returnType.load();
  395. }
  396. private function fl_play(time:Number = NaN):Boolean {
  397. log("play: time = " + time);
  398. var returnType:Object = getType();
  399. return returnType.play(time * 1000); // Flash uses milliseconds
  400. }
  401. private function fl_pause(time:Number = NaN):Boolean {
  402. log("pause: time = " + time);
  403. var returnType:Object = getType();
  404. return returnType.pause(time * 1000); // Flash uses milliseconds
  405. }
  406. private function fl_play_head(percent:Number):Boolean {
  407. log("play_head: " + percent + "%");
  408. var returnType:Object = getType();
  409. return returnType.playHead(percent);
  410. }
  411. private function fl_volume(v:Number):void {
  412. log("volume: " + v);
  413. commonStatus.volume = v;
  414. if (!commonStatus.muted) {
  415. myMp3Player.setVolume(v);
  416. myMp4Player.setVolume(v);
  417. myRtmpPlayer.setVolume(v);
  418. }
  419. }
  420. private function fl_mute(mute:Boolean):void {
  421. log("mute: " + mute);
  422. commonStatus.muted = mute;
  423. if (mute) {
  424. myMp3Player.setVolume(0);
  425. myMp4Player.setVolume(0);
  426. myRtmpPlayer.setVolume(0);
  427. } else {
  428. myMp3Player.setVolume(commonStatus.volume);
  429. myMp4Player.setVolume(commonStatus.volume);
  430. myRtmpPlayer.setVolume(commonStatus.volume);
  431. }
  432. }
  433. private function jPlayerFlashEvent(e:JplayerEvent):void {
  434. log("jPlayer Flash Event: " + e.type + ": " + e.target);
  435. if (ExternalInterface.available && !securityIssue) {
  436. ExternalInterface.call(jQuery, "jPlayerFlashEvent", e.type, extractStatusData(e.data));
  437. }
  438. }
  439. private function tracer(msg:String):void {
  440. if (debug) {
  441. traceOut.tracer(msg);
  442. }
  443. }
  444. private function extractStatusData(data:JplayerStatus):Object {
  445. var myStatus:Object = {
  446. version: JplayerStatus.VERSION,
  447. src: data.src,
  448. paused: !data.isPlaying, // Changing this name requires inverting all assignments and conditional statements.
  449. srcSet: data.srcSet,
  450. seekPercent: data.seekPercent,
  451. currentPercentRelative: data.currentPercentRelative,
  452. currentPercentAbsolute: data.currentPercentAbsolute,
  453. currentTime: data.currentTime / 1000, // JavaScript uses seconds
  454. duration: data.duration / 1000, // JavaScript uses seconds
  455. videoWidth: data.videoWidth,
  456. videoHeight: data.videoHeight,
  457. volume: commonStatus.volume,
  458. muted: commonStatus.muted
  459. };
  460. log("extractStatusData: sp=" + myStatus.seekPercent + " cpr=" + myStatus.currentPercentRelative + " cpa=" + myStatus.currentPercentAbsolute + " ct=" + myStatus.currentTime + " d=" + myStatus.duration);
  461. return myStatus;
  462. }
  463. private function jPlayerMetaDataHandler(e:JplayerEvent):void {
  464. log("jPlayerMetaDataHandler:" + e.target);
  465. if (ExternalInterface.available && !securityIssue) {
  466. resizeHandler(new Event(Event.RESIZE));
  467. ExternalInterface.call(jQuery, "jPlayerFlashEvent", e.type, extractStatusData(e.data));
  468. }
  469. }
  470. private function resizeHandler(e:Event):void {
  471. log("resizeHandler: stageWidth = " + stage.stageWidth + " | stageHeight = " + stage.stageHeight);
  472. var mediaX:Number = 0;
  473. var mediaY:Number = 0;
  474. var mediaWidth:Number = 0;
  475. var mediaHeight:Number = 0;
  476. var aspectRatioStage:Number = 0;
  477. var aspectRatioVideo:Number = 0;
  478. var videoItem:*;
  479. if (isRtmp) {
  480. videoItem = myRtmpPlayer;
  481. }
  482. if (isMp4) {
  483. videoItem = myMp4Player;
  484. }
  485. if (videoItem) {
  486. if (stage.stageWidth > 0 && stage.stageHeight > 0 && videoItem.myVideo.width > 0 && videoItem.myVideo.height > 0) {
  487. aspectRatioStage = stage.stageWidth / stage.stageHeight;
  488. aspectRatioVideo = videoItem.myVideo.width / videoItem.myVideo.height;
  489. if (aspectRatioStage < aspectRatioVideo) {
  490. mediaWidth = stage.stageWidth;
  491. mediaHeight = stage.stageWidth / aspectRatioVideo;
  492. mediaX = 0;
  493. mediaY = (stage.stageHeight - mediaHeight) / 2;
  494. } else {
  495. mediaWidth = stage.stageHeight * aspectRatioVideo;
  496. mediaHeight = stage.stageHeight;
  497. mediaX = (stage.stageWidth - mediaWidth) / 2;
  498. mediaY = 0;
  499. }
  500. resizeEntity(videoItem, mediaX, mediaY, mediaWidth, mediaHeight);
  501. }
  502. }
  503. if ((debug || securityIssue) && stage.stageWidth > 20 && stage.stageHeight > 20) {
  504. txLog.width = stage.stageWidth - 10;
  505. txLog.height = stage.stageHeight - 10;
  506. }
  507. // Resize the sprite so it covers the entire stage area
  508. contextMenuFix.x = 0;
  509. contextMenuFix.y = 0;
  510. contextMenuFix.width = stage.stageWidth;
  511. contextMenuFix.height = stage.stageHeight;
  512. }
  513. private function resizeEntity(entity:Sprite, mediaX:Number, mediaY:Number, mediaWidth:Number, mediaHeight:Number):void {
  514. entity.x = mediaX;
  515. entity.y = mediaY;
  516. entity.width = mediaWidth;
  517. entity.height = mediaHeight;
  518. }
  519. private function clickHandler(e:MouseEvent):void {
  520. // This needs to work with RTMP format too!
  521. if (isMp3) {
  522. jPlayerFlashEvent(new JplayerEvent(JplayerEvent.JPLAYER_CLICK, myMp3Player.myStatus, "click"))
  523. } else {
  524. jPlayerFlashEvent(new JplayerEvent(JplayerEvent.JPLAYER_CLICK, myMp4Player.myStatus, "click"))
  525. }
  526. }
  527. // This event is never called. See comments in class constructor.
  528. private function menuSelectHandler_jPlayer(e:ContextMenuEvent):void {
  529. navigateToURL(new URLRequest("http://jplayer.org/"), "_blank");
  530. }
  531. // This event is never called. See comments in class constructor.
  532. private function menuSelectHandler_happyworm(e:ContextMenuEvent):void {
  533. navigateToURL(new URLRequest("http://happyworm.com/"), "_blank");
  534. }
  535. private function log(t:String):void {
  536. if (debug) {
  537. txLog.text = t + "\n" + txLog.text;
  538. localAIRDebug = traceOut.localAIRDebug();
  539. if (localAIRDebug) {
  540. tracer(t);
  541. }
  542. if (ExternalInterface.available && !securityIssue) {
  543. ExternalInterface.call("console.log", t);
  544. }
  545. }
  546. }
  547. private function debugMsgHandler(e:JplayerEvent):void {
  548. log(e.msg);
  549. }
  550. private function keyboardHandler(e:KeyboardEvent):void {
  551. log("keyboardHandler: e.keyCode = " + e.keyCode);
  552. switch (e.keyCode) {
  553. case 68 : // d
  554. txLog.visible = !txLog.visible;
  555. log("Toggled log display: " + txLog.visible);
  556. break;
  557. case 76 : // l
  558. if (e.ctrlKey && e.shiftKey) {
  559. txLog.text = "Cleared log.";
  560. }
  561. break;
  562. }
  563. }
  564. }
  565. }