PageRenderTime 78ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/actionscript/src/com/vimeo/api/VimeoPlayer.as

https://gitlab.com/sheldonels/player-api
ActionScript | 365 lines | 257 code | 52 blank | 56 comment | 16 complexity | 7793cccb23d3e8c7d05632ff47954d12 MD5 | raw file
  1. /**
  2. * VimeoPlayer
  3. *
  4. * A wrapper class for Vimeo's video player (codenamed Moogaloop)
  5. * that allows you to embed easily into any AS3 application.
  6. *
  7. * Example on how to use:
  8. * var vimeo_player = new VimeoPlayer([YOUR_APPLICATIONS_CONSUMER_KEY], 2, 400, 300);
  9. * vimeo_player.addEventListener(Event.COMPLETE, vimeoPlayerLoaded);
  10. * addChild(vimeo_player);
  11. *
  12. * http://vimeo.com/api/docs/moogaloop
  13. *
  14. * Register your application for access to the Moogaloop API at:
  15. *
  16. * http://vimeo.com/api/applications
  17. */
  18. package com.vimeo.api
  19. {
  20. import flash.display.DisplayObject;
  21. import flash.display.Loader;
  22. import flash.display.Sprite;
  23. import flash.events.Event;
  24. import flash.events.MouseEvent;
  25. import flash.events.TimerEvent;
  26. import flash.external.ExternalInterface;
  27. import flash.geom.Point;
  28. import flash.net.URLRequest;
  29. import flash.system.LoaderContext;
  30. import flash.system.Security;
  31. import flash.utils.Timer;
  32. public class VimeoPlayer extends Sprite {
  33. // Assets
  34. private var container : Sprite = new Sprite(); // sprite that holds the player
  35. private var moogaloop : Object = false; // the player
  36. private var player_mask : Sprite = new Sprite(); // some sprites inside moogaloop go outside the bounds of the player. we use a mask to hide it
  37. // Default variables
  38. private var player_width : int = 400;
  39. private var player_height : int = 300;
  40. private var api_version : int = 2;
  41. private var load_timer : Timer = new Timer(200);
  42. // Events
  43. // API v2
  44. public static const FINISH : String = 'finish';
  45. public static const LOAD_PROGRESS : String = 'loadProgress';
  46. public static const PAUSE : String = 'pause';
  47. public static const PLAY : String = 'play';
  48. public static const PLAY_PROGRESS : String = 'playProgress';
  49. public static const READY : String = 'ready';
  50. public static const SEEK : String = 'seek';
  51. // API v1
  52. public static const ON_FINISH : String = 'onFinish';
  53. public static const ON_LOADING : String = 'onLoading';
  54. public static const ON_PAUSE : String = 'onPause';
  55. public static const ON_PLAY : String = 'onPlay';
  56. public static const ON_PROGRESS : String = 'onProgress';
  57. public static const ON_SEEK : String = 'onSeek';
  58. public function VimeoPlayer(oauth_key:String, clip_id:int, w:int, h:int, fp_version:String='10', api_version:int=2)
  59. {
  60. this.setDimensions(w, h);
  61. Security.allowDomain('*');
  62. Security.allowInsecureDomain('*');
  63. var api_param : String = '&js_api=1';
  64. this.api_version = api_version;
  65. //
  66. if (fp_version != '9')
  67. {
  68. switch(api_version)
  69. {
  70. case 2:
  71. api_param = '&api=1';
  72. break;
  73. }
  74. }
  75. else
  76. {
  77. this.api_version = 1;
  78. }
  79. var request : URLRequest = new URLRequest("http://api.vimeo.com/moogaloop_api.swf?oauth_key=" + oauth_key + "&clip_id=" + clip_id + "&width=" + w + "&height=" + h + "&fullscreen=0&fp_version=" + fp_version + api_param + "&cache_buster=" + (Math.random() * 1000));
  80. var loaderContext : LoaderContext = new LoaderContext(true);
  81. var loader : Loader = new Loader();
  82. loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete, false, 0, true);
  83. loader.load(request, loaderContext);
  84. this.addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler, false, 0, true);
  85. }
  86. public function destroy() : void
  87. {
  88. if (api_version == 2)
  89. {
  90. // API v2 Event Handlers
  91. moogaloop.removeEventListener(READY, readyHandler);
  92. moogaloop.removeEventListener(PLAY, playHandler);
  93. moogaloop.removeEventListener(PAUSE, pauseHandler);
  94. moogaloop.removeEventListener(SEEK, seekHandler);
  95. moogaloop.removeEventListener(LOAD_PROGRESS, loadProgressHandler);
  96. moogaloop.removeEventListener(PLAY_PROGRESS, playProgressHandler);
  97. moogaloop.removeEventListener(FINISH, finishHandler);
  98. }
  99. else
  100. {
  101. // API v1 Event Handlers
  102. moogaloop.removeEventListener(ON_PLAY, onPlayHandler);
  103. moogaloop.removeEventListener(ON_PAUSE, onPauseHandler);
  104. moogaloop.removeEventListener(ON_SEEK, onSeekHandler);
  105. moogaloop.removeEventListener(ON_LOADING, onLoadingHandler);
  106. moogaloop.removeEventListener(ON_PROGRESS, onProgressHandler);
  107. moogaloop.removeEventListener(ON_FINISH, onFinishHandler);
  108. }
  109. moogaloop.destroy();
  110. if (container.contains(DisplayObject(moogaloop))) container.removeChild(DisplayObject(moogaloop));
  111. if (this.contains(player_mask)) this.removeChild(player_mask);
  112. if (this.contains(container)) this.removeChild(container);
  113. stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMove);
  114. }
  115. private function setDimensions(w:int, h:int) : void
  116. {
  117. player_width = w;
  118. player_height = h;
  119. }
  120. private function onComplete(e:Event) : void
  121. {
  122. // Finished loading moogaloop
  123. container.addChild(e.currentTarget.loader.content);
  124. moogaloop = e.currentTarget.loader.content;
  125. if (api_version == 2)
  126. {
  127. // API v2 Event Handlers
  128. moogaloop.addEventListener(READY, readyHandler, false, 0, true);
  129. moogaloop.addEventListener(PLAY, playHandler, false, 0, true);
  130. moogaloop.addEventListener(PAUSE, pauseHandler, false, 0, true);
  131. moogaloop.addEventListener(SEEK, seekHandler, false, 0, true);
  132. moogaloop.addEventListener(LOAD_PROGRESS, loadProgressHandler, false, 0, true);
  133. moogaloop.addEventListener(PLAY_PROGRESS, playProgressHandler, false, 0, true);
  134. moogaloop.addEventListener(FINISH, finishHandler, false, 0, true);
  135. }
  136. else
  137. {
  138. // API v1 Event Handlers
  139. moogaloop.addEventListener(ON_PLAY, onPlayHandler, false, 0, true);
  140. moogaloop.addEventListener(ON_PAUSE, onPauseHandler, false, 0, true);
  141. moogaloop.addEventListener(ON_SEEK, onSeekHandler, false, 0, true);
  142. moogaloop.addEventListener(ON_LOADING, onLoadingHandler, false, 0, true);
  143. moogaloop.addEventListener(ON_PROGRESS, onProgressHandler, false, 0, true);
  144. moogaloop.addEventListener(ON_FINISH, onFinishHandler, false, 0, true);
  145. }
  146. // Create the mask for moogaloop
  147. this.addChild(player_mask);
  148. container.mask = player_mask;
  149. this.addChild(container);
  150. redrawMask();
  151. load_timer.addEventListener(TimerEvent.TIMER, playerLoadedCheck);
  152. load_timer.start();
  153. }
  154. /**
  155. * Wait for Moogaloop to finish setting up
  156. */
  157. private function playerLoadedCheck(e:TimerEvent) : void
  158. {
  159. if (moogaloop.player_loaded)
  160. {
  161. // Moogaloop is finished configuring
  162. load_timer.stop();
  163. load_timer.removeEventListener(TimerEvent.TIMER, playerLoadedCheck);
  164. // remove moogaloop's mouse listeners listener
  165. moogaloop.disableMouseMove();
  166. if (stage)
  167. {
  168. stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMove, false, 0, true);
  169. }
  170. dispatchEvent(new Event(Event.COMPLETE));
  171. }
  172. }
  173. /**
  174. * Fake the mouse move/out events for Moogaloop
  175. */
  176. private function mouseMove(e:MouseEvent) : void
  177. {
  178. if (moogaloop && moogaloop.player_loaded)
  179. {
  180. var pos : Point = this.parent.localToGlobal(new Point(this.x, this.y));
  181. if (e.stageX >= pos.x && e.stageX <= pos.x + this.player_width &&
  182. e.stageY >= pos.y && e.stageY <= pos.y + this.player_height)
  183. {
  184. moogaloop.mouseMove(e);
  185. }
  186. else
  187. {
  188. moogaloop.mouseOut();
  189. }
  190. }
  191. }
  192. private function redrawMask() : void
  193. {
  194. with (player_mask.graphics)
  195. {
  196. beginFill(0x000000, 1);
  197. drawRect(container.x, container.y, player_width, player_height);
  198. endFill();
  199. }
  200. }
  201. public function play() : void
  202. {
  203. moogaloop.play();
  204. }
  205. public function pause() : void
  206. {
  207. moogaloop.pause();
  208. }
  209. /**
  210. * returns duration of video in seconds
  211. */
  212. public function getDuration() : int
  213. {
  214. return moogaloop.duration;
  215. }
  216. /**
  217. * Seek to specific loaded time in video (in seconds)
  218. */
  219. public function seekTo(time:int) : void
  220. {
  221. moogaloop.seek(time);
  222. }
  223. /**
  224. * Change the primary color (i.e. 00ADEF)
  225. */
  226. public function changeColor(hex:String) : void
  227. {
  228. moogaloop.color = uint('0x' + hex);
  229. }
  230. /**
  231. * Load in a different video
  232. */
  233. public function loadVideo(id:int) : void
  234. {
  235. moogaloop.loadVideo(id);
  236. }
  237. public function setSize(w:int, h:int) : void
  238. {
  239. this.setDimensions(w, h);
  240. moogaloop.setSize(w, h);
  241. this.redrawMask();
  242. }
  243. // Event Handlers ____________________________________________________
  244. private function addedToStageHandler(event:Event) : void
  245. {
  246. stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMove);
  247. this.addEventListener(Event.REMOVED_FROM_STAGE, removedFromStageHandler, false, 0, true);
  248. }
  249. private function removedFromStageHandler(event:Event) : void
  250. {
  251. stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMove);
  252. this.removeEventListener(Event.REMOVED_FROM_STAGE, removedFromStageHandler);
  253. }
  254. /**
  255. * API v2 Event Handlers
  256. */
  257. private function readyHandler(event:Event) : void
  258. {
  259. trace('readyHandler');
  260. }
  261. private function playHandler(event:Event) : void
  262. {
  263. trace('playHandler');
  264. }
  265. private function pauseHandler(event:Event) : void
  266. {
  267. trace('pauseHandler');
  268. }
  269. private function seekHandler(event:Event) : void
  270. {
  271. trace('seekHandler');
  272. }
  273. private function loadProgressHandler(event:Event) : void
  274. {
  275. trace('loadProgressHandler');
  276. }
  277. private function playProgressHandler(event:Event) : void
  278. {
  279. trace('playProgressHandler');
  280. }
  281. private function finishHandler(event:Event) : void
  282. {
  283. trace('finishHandler');
  284. }
  285. /**
  286. * API v1 Event Handlers
  287. */
  288. private function onPlayHandler(event:Event) : void
  289. {
  290. trace('onPlayHandler');
  291. }
  292. private function onPauseHandler(event:Event) : void
  293. {
  294. trace('onPauseHandler');
  295. }
  296. private function onSeekHandler(event:Event) : void
  297. {
  298. trace('onSeekHandler');
  299. }
  300. private function onLoadingHandler(event:Event) : void
  301. {
  302. trace('onLoadingHandler');
  303. }
  304. private function onProgressHandler(event:Event) : void
  305. {
  306. trace('onProgressHandler');
  307. }
  308. private function onFinishHandler(event:Event) : void
  309. {
  310. trace('onFinishHandler');
  311. }
  312. }
  313. }