PageRenderTime 242ms CodeModel.GetById 69ms RepoModel.GetById 1ms app.codeStats 0ms

/assets/filemanager/js/jPlayer/actionscript/happyworm/jPlayer/JplayerMp4.as

https://gitlab.com/agustus/wisata-web
ActionScript | 412 lines | 364 code | 25 blank | 23 comment | 100 complexity | 64dd164134f9d01ef5cd28e0a96d6d21 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 license.
  7. * http://opensource.org/licenses/MIT
  8. *
  9. * Author: Mark J Panaghiston
  10. * Date: 29th January 2013
  11. */
  12. package happyworm.jPlayer {
  13. import flash.display.Sprite;
  14. import flash.media.Video;
  15. import flash.media.SoundTransform;
  16. import flash.net.NetConnection;
  17. import flash.net.NetStream;
  18. import flash.utils.Timer;
  19. import flash.events.NetStatusEvent;
  20. import flash.events.SecurityErrorEvent;
  21. import flash.events.TimerEvent;
  22. public class JplayerMp4 extends Sprite {
  23. public var myVideo:Video = new Video();
  24. private var myConnection:NetConnection;
  25. private var myStream:NetStream;
  26. private var myTransform:SoundTransform = new SoundTransform();
  27. public var myStatus:JplayerStatus = new JplayerStatus();
  28. private var timeUpdateTimer:Timer = new Timer(250, 0); // Matched to HTML event freq
  29. private var progressTimer:Timer = new Timer(250, 0); // Matched to HTML event freq
  30. private var seekingTimer:Timer = new Timer(100, 0); // Internal: How often seeking is checked to see if it is over.
  31. public function JplayerMp4(volume:Number) {
  32. myConnection = new NetConnection();
  33. myConnection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
  34. myConnection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
  35. myVideo.smoothing = true;
  36. this.addChild(myVideo);
  37. timeUpdateTimer.addEventListener(TimerEvent.TIMER, timeUpdateHandler);
  38. progressTimer.addEventListener(TimerEvent.TIMER, progressHandler);
  39. seekingTimer.addEventListener(TimerEvent.TIMER, seekingHandler);
  40. myStatus.volume = volume;
  41. }
  42. private function progressUpdates(active:Boolean):void {
  43. if(active) {
  44. progressTimer.start();
  45. } else {
  46. progressTimer.stop();
  47. }
  48. }
  49. private function progressHandler(e:TimerEvent):void {
  50. if(myStatus.isLoading) {
  51. if(getLoadRatio() == 1) { // Close as can get to a loadComplete event since client.onPlayStatus only works with FMS
  52. this.dispatchEvent(new JplayerEvent(JplayerEvent.DEBUG_MSG, myStatus, "progressHandler: loadComplete"));
  53. myStatus.loaded();
  54. progressUpdates(false);
  55. }
  56. }
  57. progressEvent();
  58. }
  59. private function progressEvent():void {
  60. this.dispatchEvent(new JplayerEvent(JplayerEvent.DEBUG_MSG, myStatus, "progressEvent:"));
  61. updateStatusValues();
  62. this.dispatchEvent(new JplayerEvent(JplayerEvent.JPLAYER_PROGRESS, myStatus));
  63. }
  64. private function timeUpdates(active:Boolean):void {
  65. if(active) {
  66. timeUpdateTimer.start();
  67. } else {
  68. timeUpdateTimer.stop();
  69. }
  70. }
  71. private function timeUpdateHandler(e:TimerEvent):void {
  72. timeUpdateEvent();
  73. }
  74. private function timeUpdateEvent():void {
  75. updateStatusValues();
  76. this.dispatchEvent(new JplayerEvent(JplayerEvent.JPLAYER_TIMEUPDATE, myStatus));
  77. }
  78. private function seeking(active:Boolean):void {
  79. if(active) {
  80. if(!myStatus.isSeeking) {
  81. seekingEvent();
  82. }
  83. seekingTimer.start();
  84. } else {
  85. if(myStatus.isSeeking) {
  86. seekedEvent();
  87. }
  88. seekingTimer.stop();
  89. }
  90. }
  91. private function seekingHandler(e:TimerEvent):void {
  92. if(getSeekTimeRatio() <= getLoadRatio()) {
  93. seeking(false);
  94. if(myStatus.playOnSeek) {
  95. myStatus.playOnSeek = false; // Capture the flag.
  96. play(myStatus.pausePosition); // Must pass time or the seek time is never set.
  97. } else {
  98. pause(myStatus.pausePosition); // Must pass time or the stream.time is read.
  99. }
  100. } else if(myStatus.metaDataReady && myStatus.pausePosition > myStatus.duration) {
  101. // Illegal seek time
  102. seeking(false);
  103. pause(0);
  104. }
  105. }
  106. private function seekingEvent():void {
  107. myStatus.isSeeking = true;
  108. updateStatusValues();
  109. this.dispatchEvent(new JplayerEvent(JplayerEvent.JPLAYER_SEEKING, myStatus));
  110. }
  111. private function seekedEvent():void {
  112. myStatus.isSeeking = false;
  113. updateStatusValues();
  114. this.dispatchEvent(new JplayerEvent(JplayerEvent.JPLAYER_SEEKED, myStatus));
  115. }
  116. private function netStatusHandler(e:NetStatusEvent):void {
  117. this.dispatchEvent(new JplayerEvent(JplayerEvent.DEBUG_MSG, myStatus, "netStatusHandler: '" + e.info.code + "'"));
  118. switch(e.info.code) {
  119. case "NetConnection.Connect.Success":
  120. connectStream();
  121. break;
  122. case "NetStream.Play.Start":
  123. // This event code occurs once, when the media is opened. Equiv to loadOpen() in mp3 player.
  124. myStatus.loading();
  125. this.dispatchEvent(new JplayerEvent(JplayerEvent.JPLAYER_LOADSTART, myStatus));
  126. progressUpdates(true);
  127. // See onMetaDataHandler() for other condition, since duration is vital.
  128. break;
  129. case "NetStream.Play.Stop":
  130. this.dispatchEvent(new JplayerEvent(JplayerEvent.DEBUG_MSG, myStatus, "NetStream.Play.Stop: getDuration() - getCurrentTime() = " + (getDuration() - getCurrentTime())));
  131. // Check if media is at the end (or close) otherwise this was due to download bandwidth stopping playback. ie., Download is not fast enough.
  132. if(Math.abs(getDuration() - getCurrentTime()) < 150) { // Testing found 150ms worked best for M4A files, where playHead(99.9) caused a stuck state due to firing with ~116ms left to play.
  133. endedEvent();
  134. }
  135. break;
  136. case "NetStream.Seek.InvalidTime":
  137. // Used for capturing invalid set times and clicks on the end of the progress bar.
  138. endedEvent();
  139. break;
  140. case "NetStream.Play.StreamNotFound":
  141. myStatus.error(); // Resets status except the src, and it sets srcError property.
  142. this.dispatchEvent(new JplayerEvent(JplayerEvent.JPLAYER_ERROR, myStatus));
  143. break;
  144. }
  145. // "NetStream.Seek.Notify" event code is not very useful. It occurs after every seek(t) command issued and does not appear to wait for the media to be ready.
  146. }
  147. private function endedEvent():void {
  148. var wasPlaying:Boolean = myStatus.isPlaying;
  149. pause(0);
  150. timeUpdates(false);
  151. timeUpdateEvent();
  152. if(wasPlaying) {
  153. this.dispatchEvent(new JplayerEvent(JplayerEvent.JPLAYER_ENDED, myStatus));
  154. }
  155. }
  156. private function securityErrorHandler(event:SecurityErrorEvent):void {
  157. this.dispatchEvent(new JplayerEvent(JplayerEvent.DEBUG_MSG, myStatus, "securityErrorHandler."));
  158. }
  159. private function connectStream():void {
  160. this.dispatchEvent(new JplayerEvent(JplayerEvent.DEBUG_MSG, myStatus, "connectStream."));
  161. var customClient:Object = new Object();
  162. customClient.onMetaData = onMetaDataHandler;
  163. // customClient.onPlayStatus = onPlayStatusHandler; // According to the forums and my tests, onPlayStatus only works with FMS (Flash Media Server).
  164. myStream = null;
  165. myStream = new NetStream(myConnection);
  166. myStream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
  167. myStream.client = customClient;
  168. myVideo.attachNetStream(myStream);
  169. setVolume(myStatus.volume);
  170. myStream.play(myStatus.src);
  171. }
  172. public function setFile(src:String):void {
  173. if(myStream != null) {
  174. myStream.close();
  175. }
  176. myVideo.clear();
  177. progressUpdates(false);
  178. timeUpdates(false);
  179. myStatus.reset();
  180. myStatus.src = src;
  181. myStatus.srcSet = true;
  182. timeUpdateEvent();
  183. }
  184. public function clearFile():void {
  185. setFile("");
  186. myStatus.srcSet = false;
  187. }
  188. public function load():Boolean {
  189. if(myStatus.loadRequired()) {
  190. myStatus.startingDownload();
  191. myConnection.connect(null);
  192. return true;
  193. } else {
  194. return false;
  195. }
  196. }
  197. public function play(time:Number = NaN):Boolean {
  198. var wasPlaying:Boolean = myStatus.isPlaying;
  199. if(!isNaN(time) && myStatus.srcSet) {
  200. if(myStatus.isPlaying) {
  201. myStream.pause();
  202. myStatus.isPlaying = false;
  203. }
  204. myStatus.pausePosition = time;
  205. }
  206. if(myStatus.isStartingDownload) {
  207. myStatus.playOnLoad = true; // Raise flag, captured in onMetaDataHandler()
  208. return true;
  209. } else if(myStatus.loadRequired()) {
  210. myStatus.playOnLoad = true; // Raise flag, captured in onMetaDataHandler()
  211. return load();
  212. } else if((myStatus.isLoading || myStatus.isLoaded) && !myStatus.isPlaying) {
  213. if(myStatus.metaDataReady && myStatus.pausePosition > myStatus.duration) { // The time is invalid, ie., past the end.
  214. myStream.pause(); // Since it is playing by default at this point.
  215. myStatus.pausePosition = 0;
  216. myStream.seek(0);
  217. timeUpdates(false);
  218. timeUpdateEvent();
  219. if(wasPlaying) { // For when playing and then get a play(huge)
  220. this.dispatchEvent(new JplayerEvent(JplayerEvent.JPLAYER_PAUSE, myStatus));
  221. }
  222. } else if(getSeekTimeRatio() > getLoadRatio()) { // Use an estimate based on the downloaded amount
  223. myStatus.playOnSeek = true;
  224. seeking(true);
  225. myStream.pause(); // Since it is playing by default at this point.
  226. } else {
  227. if(!isNaN(time)) { // Avoid using seek() when it is already correct.
  228. myStream.seek(myStatus.pausePosition/1000); // Since time is in ms and seek() takes seconds
  229. }
  230. myStatus.isPlaying = true; // Set immediately before playing. Could affects events.
  231. myStream.resume();
  232. timeUpdates(true);
  233. if(!wasPlaying) {
  234. this.dispatchEvent(new JplayerEvent(JplayerEvent.JPLAYER_PLAY, myStatus));
  235. }
  236. }
  237. return true;
  238. } else {
  239. return false;
  240. }
  241. }
  242. public function pause(time:Number = NaN):Boolean {
  243. myStatus.playOnLoad = false; // Reset flag in case load/play issued immediately before this command, ie., before onMetadata() event.
  244. myStatus.playOnSeek = false; // Reset flag in case play(time) issued before the command and is still seeking to time set.
  245. var wasPlaying:Boolean = myStatus.isPlaying;
  246. // To avoid possible loops with timeupdate and pause(time). A pause() does not have the problem.
  247. var alreadyPausedAtTime:Boolean = false;
  248. if(!isNaN(time) && myStatus.pausePosition == time) {
  249. alreadyPausedAtTime = true;
  250. }
  251. // Need to wait for metadata to load before ever issuing a pause. The metadata handler will call this function if needed, when ready.
  252. if(myStream != null && myStatus.metaDataReady) { // myStream is a null until the 1st media is loaded. ie., The 1st ever setMedia being followed by a pause() or pause(t).
  253. myStream.pause();
  254. }
  255. if(myStatus.isPlaying) {
  256. myStatus.isPlaying = false;
  257. myStatus.pausePosition = myStream.time * 1000;
  258. }
  259. if(!isNaN(time) && myStatus.srcSet) {
  260. myStatus.pausePosition = time;
  261. }
  262. if(wasPlaying) {
  263. this.dispatchEvent(new JplayerEvent(JplayerEvent.JPLAYER_PAUSE, myStatus));
  264. }
  265. if(myStatus.isStartingDownload) {
  266. return true;
  267. } else if(myStatus.loadRequired()) {
  268. if(time > 0) { // We do not want the stop() command, which does pause(0), causing a load operation.
  269. return load();
  270. } else {
  271. return true; // Technically the pause(0) succeeded. ie., It did nothing, since nothing was required.
  272. }
  273. } else if(myStatus.isLoading || myStatus.isLoaded) {
  274. if(myStatus.metaDataReady && myStatus.pausePosition > myStatus.duration) { // The time is invalid, ie., past the end.
  275. myStatus.pausePosition = 0;
  276. myStream.seek(0);
  277. seekedEvent(); // Deals with seeking effect when using setMedia() then pause(huge). NB: There is no preceeding seeking event.
  278. } else if(!isNaN(time)) {
  279. if(getSeekTimeRatio() > getLoadRatio()) { // Use an estimate based on the downloaded amount
  280. seeking(true);
  281. } else {
  282. if(myStatus.metaDataReady) { // Otherwise seek(0) will stop the metadata loading.
  283. myStream.seek(myStatus.pausePosition/1000);
  284. }
  285. }
  286. }
  287. timeUpdates(false);
  288. // Need to be careful with timeupdate event, otherwise a pause in a timeupdate event can cause a loop.
  289. // Neither pause() nor pause(time) will cause a timeupdate loop.
  290. if(wasPlaying || !isNaN(time) && !alreadyPausedAtTime) {
  291. timeUpdateEvent();
  292. }
  293. return true;
  294. } else {
  295. return false;
  296. }
  297. }
  298. public function playHead(percent:Number):Boolean {
  299. var time:Number = percent * getDuration() * getLoadRatio() / 100;
  300. if(myStatus.isPlaying || myStatus.playOnLoad || myStatus.playOnSeek) {
  301. return play(time);
  302. } else {
  303. return pause(time);
  304. }
  305. }
  306. public function setVolume(v:Number):void {
  307. myStatus.volume = v;
  308. myTransform.volume = v;
  309. if(myStream != null) {
  310. myStream.soundTransform = myTransform;
  311. }
  312. }
  313. private function updateStatusValues():void {
  314. myStatus.seekPercent = 100 * getLoadRatio();
  315. myStatus.currentTime = getCurrentTime();
  316. myStatus.currentPercentRelative = 100 * getCurrentRatioRel();
  317. myStatus.currentPercentAbsolute = 100 * getCurrentRatioAbs();
  318. myStatus.duration = getDuration();
  319. }
  320. public function getLoadRatio():Number {
  321. if((myStatus.isLoading || myStatus.isLoaded) && myStream.bytesTotal > 0) {
  322. return myStream.bytesLoaded / myStream.bytesTotal;
  323. } else {
  324. return 0;
  325. }
  326. }
  327. public function getDuration():Number {
  328. return myStatus.duration; // Set from meta data.
  329. }
  330. public function getCurrentTime():Number {
  331. if(myStatus.isPlaying) {
  332. return myStream.time * 1000;
  333. } else {
  334. return myStatus.pausePosition;
  335. }
  336. }
  337. public function getCurrentRatioRel():Number {
  338. if((getLoadRatio() > 0) && (getCurrentRatioAbs() <= getLoadRatio())) {
  339. return getCurrentRatioAbs() / getLoadRatio();
  340. } else {
  341. return 0;
  342. }
  343. }
  344. public function getCurrentRatioAbs():Number {
  345. if(getDuration() > 0) {
  346. return getCurrentTime() / getDuration();
  347. } else {
  348. return 0;
  349. }
  350. }
  351. public function getSeekTimeRatio():Number {
  352. if(getDuration() > 0) {
  353. return myStatus.pausePosition / getDuration();
  354. } else {
  355. return 1;
  356. }
  357. }
  358. public function onMetaDataHandler(info:Object):void { // Used in connectStream() in myStream.client object.
  359. // This event occurs when jumping to the start of static files! ie., seek(0) will cause this event to occur.
  360. if(!myStatus.metaDataReady) {
  361. this.dispatchEvent(new JplayerEvent(JplayerEvent.DEBUG_MSG, myStatus, "onMetaDataHandler: " + info.duration + " | " + info.width + "x" + info.height));
  362. myStatus.metaDataReady = true; // Set flag so that this event only effects jPlayer the 1st time.
  363. myStatus.metaData = info;
  364. myStatus.duration = info.duration * 1000; // Only available via Meta Data.
  365. if(info.width != undefined) {
  366. myVideo.width = myStatus.videoWidth = info.width;
  367. }
  368. if(info.height != undefined) {
  369. myVideo.height = myStatus.videoHeight = info.height;
  370. }
  371. if(myStatus.playOnLoad) {
  372. myStatus.playOnLoad = false; // Capture the flag
  373. if(myStatus.pausePosition > 0 ) { // Important for setMedia followed by play(time).
  374. play(myStatus.pausePosition);
  375. } else {
  376. play(); // Not always sending pausePosition avoids the extra seek(0) for a normal play() command.
  377. }
  378. } else {
  379. pause(myStatus.pausePosition); // Always send the pausePosition. Important for setMedia() followed by pause(time). Deals with not reading stream.time with setMedia() and play() immediately followed by stop() or pause(0)
  380. }
  381. this.dispatchEvent(new JplayerEvent(JplayerEvent.JPLAYER_LOADEDMETADATA, myStatus));
  382. } else {
  383. this.dispatchEvent(new JplayerEvent(JplayerEvent.DEBUG_MSG, myStatus, "onMetaDataHandler: Already read (NO EFFECT)"));
  384. }
  385. }
  386. }
  387. }