PageRenderTime 2421ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/flowplayer/branches/3_1_3_dev/src/actionscript/org/flowplayer/view/Launcher.as

http://flowplayer-core.googlecode.com/
ActionScript | 884 lines | 716 code | 121 blank | 47 comment | 143 complexity | f4b15836c698ec33268e0f28379776dd MD5 | raw file
Possible License(s): GPL-3.0, AGPL-1.0
  1. /*
  2. * Copyright (c) 2008, 2009 Flowplayer Oy
  3. *
  4. * This file is part of Flowplayer.
  5. *
  6. * Flowplayer is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Flowplayer is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Flowplayer. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package org.flowplayer.view {
  20. import org.flowplayer.config.Config;
  21. import org.flowplayer.config.ConfigParser;
  22. import org.flowplayer.config.ExternalInterfaceHelper;
  23. import org.flowplayer.config.VersionInfo;
  24. import org.flowplayer.controller.PlayListController;
  25. import org.flowplayer.controller.ResourceLoader;
  26. import org.flowplayer.controller.ResourceLoaderImpl;
  27. import org.flowplayer.flow_internal;
  28. import org.flowplayer.model.Callable;
  29. import org.flowplayer.model.Clip;
  30. import org.flowplayer.model.ClipEvent;
  31. import org.flowplayer.model.ClipEventType;
  32. import org.flowplayer.model.DisplayPluginModel;
  33. import org.flowplayer.model.DisplayProperties;
  34. import org.flowplayer.model.DisplayPropertiesImpl;
  35. import org.flowplayer.model.ErrorCode;
  36. import org.flowplayer.model.EventDispatcher;
  37. import org.flowplayer.model.Loadable;
  38. import org.flowplayer.model.Logo;
  39. import org.flowplayer.model.PlayButtonOverlay;
  40. import org.flowplayer.model.PlayerError;
  41. import org.flowplayer.model.PlayerEvent;
  42. import org.flowplayer.model.Playlist;
  43. import org.flowplayer.model.Plugin;
  44. import org.flowplayer.model.PluginError;
  45. import org.flowplayer.model.PluginEvent;
  46. import org.flowplayer.model.PluginModel;
  47. import org.flowplayer.model.ProviderModel;
  48. import org.flowplayer.model.State;
  49. import org.flowplayer.util.Arrange;
  50. import org.flowplayer.util.Log;
  51. import org.flowplayer.util.TextUtil;
  52. import org.flowplayer.util.URLUtil;
  53. import org.flowplayer.view.Panel;
  54. import org.flowplayer.view.PluginLoader;
  55. import org.flowplayer.view.Screen;
  56. import org.osflash.thunderbolt.Logger;
  57. import flash.display.DisplayObject;
  58. import flash.display.DisplayObjectContainer;
  59. import flash.display.Sprite;
  60. import flash.events.Event;
  61. import flash.events.KeyboardEvent;
  62. import flash.events.MouseEvent;
  63. import flash.events.TimerEvent;
  64. import flash.net.URLRequest;
  65. import flash.net.navigateToURL;
  66. import flash.system.Capabilities;
  67. import flash.system.Security;
  68. import flash.text.TextField;
  69. import flash.text.TextFieldAutoSize;
  70. import flash.ui.Keyboard;
  71. import flash.utils.Dictionary;
  72. import flash.utils.Timer;
  73. use namespace flow_internal;
  74. public class Launcher extends StyleableSprite implements ErrorHandler {
  75. private var _panel:Panel;
  76. private var _screen:Screen;
  77. private var _config:Config;
  78. private var _flowplayer:Flowplayer;
  79. private var _pluginRegistry:PluginRegistry;
  80. private var _animationEngine:AnimationEngine;
  81. private var _playButtonOverlay:PlayButtonOverlay;
  82. private var _controlsModel:DisplayPluginModel;
  83. private var _providers:Dictionary = new Dictionary();
  84. private var _canvasLogo:Sprite;
  85. private var _pluginLoader:PluginLoader;
  86. private var _error:TextField;
  87. private var _pluginsInitialized:Number = 0;
  88. private var _numLoadablePlugins:int = -1;
  89. private var _enteringFullscreen:Boolean;
  90. private var _copyrightNotice:TextField;
  91. private var _playlistLoader:ResourceLoader;
  92. private var _fullscreenManager:FullscreenManager;
  93. private var _screenArranged:Boolean;
  94. private var _clickCount:int;
  95. private var _clickTimer:Timer = new Timer(200, 1);
  96. private var _clickEvent:MouseEvent;
  97. private var _initialized:Boolean;
  98. [Frame(factoryClass="org.flowplayer.view.Preloader")]
  99. public function Launcher() {
  100. super("#canvas", this);
  101. addEventListener(Event.ADDED_TO_STAGE, function(e:Event):void {
  102. trace("Launcher added to stage");
  103. callAndHandleError(createFlashVarsConfig, PlayerError.INIT_FAILED);
  104. });
  105. }
  106. private function initPhase1():void {
  107. if (_flowplayer) {
  108. log.debug("already initialized, returning");
  109. return;
  110. }
  111. Log.configure(_config.getLogConfiguration());
  112. trace("created log configuration, tracing enabled? " + Log.traceEnabled)
  113. initCustomClipEvents();
  114. if (_config.playerId) {
  115. Security.allowDomain(URLUtil.pageUrl);
  116. }
  117. loader = createNewLoader();
  118. rootStyle = _config.canvas.style;
  119. stage.addEventListener(Event.RESIZE, onStageResize);
  120. setSize(stage.stageWidth, stage.stageHeight);
  121. if (! VersionInfo.commercial) {
  122. log.debug("Adding logo to canvas");
  123. createLogoForCanvas();
  124. }
  125. log = new Log(this);
  126. EventDispatcher.playerId = _config.playerId;
  127. log.debug("security sandbox type: " + Security.sandboxType);
  128. log.info(VersionInfo.versionInfo());
  129. trace(VersionInfo.versionInfo());
  130. log.debug("creating Panel");
  131. createPanel();
  132. _pluginRegistry = new PluginRegistry(_panel);
  133. log.debug("Creating animation engine");
  134. createAnimationEngine(_pluginRegistry);
  135. log.debug("creating play button overlay");
  136. createPlayButtonOverlay();
  137. log.debug("creating Flowplayer API");
  138. createFlowplayer();
  139. loadPlaylistFeed();
  140. }
  141. private function initPhase2(event:Event = null):void {
  142. log.info("initPhase2");
  143. loadPlugins();
  144. }
  145. private function initPhase3(event:Event = null):void {
  146. log.debug("initPhase3, all plugins loaded");
  147. createScreen();
  148. _config.getPlaylist().onBeforeBegin(function(event:ClipEvent):void { hideErrorMessage(); });
  149. if (_playButtonOverlay) {
  150. PlayButtonOverlayView(_playButtonOverlay.getDisplayObject()).playlist = _config.getPlaylist();
  151. }
  152. log.debug("creating PlayListController");
  153. _providers = _pluginLoader.providers;
  154. var playListController:PlayListController = createPlayListController();
  155. addPlayListListeners();
  156. createFullscreenManager(playListController.playlist);
  157. addScreenToPanel();
  158. if (!validateLicenseKey()) {
  159. createLogoForCanvas();
  160. resizeCanvasLogo();
  161. }
  162. log.debug("creating logo");
  163. createLogo();
  164. contextMenu = new ContextMenuBuilder(_config.playerId, _config.contextMenu).build();
  165. log.debug("initializing ExternalInterface");
  166. if (useExternalInterfade()) {
  167. _flowplayer.initExternalInterface();
  168. }
  169. log.debug("calling onLoad to plugins");
  170. _pluginRegistry.onLoad(_flowplayer);
  171. }
  172. private function initPhase4(event:Event = null):void {
  173. log.info("initPhase4, all plugins initialized");
  174. log.debug("Adding visible plugins to panel");
  175. addPluginsToPanel(_pluginRegistry);
  176. log.debug("dispatching onLoad");
  177. if (useExternalInterfade()) {
  178. _flowplayer.dispatchEvent(PlayerEvent.load("player"));
  179. }
  180. arrangeScreen();
  181. log.debug("starting configured streams");
  182. startStreams();
  183. stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
  184. addListeners();
  185. _initialized = true;
  186. // _controlsModel.onPluginEvent(function(event:PluginEvent):void {
  187. // log.debug("received plugin event " + event.id);
  188. // var model:DisplayPluginModel = event.target as DisplayPluginModel;
  189. // log.debug("controls y-pos now is " + model.getDisplayObject().y);
  190. // });
  191. }
  192. private function resizeCanvasLogo():void {
  193. _canvasLogo.alpha = 1;
  194. _canvasLogo.width = 150;
  195. _canvasLogo.scaleY = _canvasLogo.scaleX;
  196. arrangeCanvasLogo();
  197. }
  198. private function useExternalInterfade():Boolean {
  199. log.debug("useExternalInteface: " + (_config.playerId != null));
  200. return _config.playerId != null;
  201. }
  202. private function onStageResize(event:Event = null):void {
  203. setSize(stage.stageWidth, stage.stageHeight);
  204. arrangeCanvasLogo();
  205. if (! _screenArranged) {
  206. arrangeScreen();
  207. _screenArranged = true;
  208. }
  209. }
  210. private function arrangeCanvasLogo():void {
  211. if (!_canvasLogo) return;
  212. _canvasLogo.x = 15;
  213. _canvasLogo.y = stage.stageHeight - (_controlsModel ? _controlsModel.dimensions.height.toPx(stage.stageHeight) + 10 : 10) - _canvasLogo.height - _copyrightNotice.height;
  214. _copyrightNotice.x = 12;
  215. _copyrightNotice.y = _canvasLogo.y + _canvasLogo.height;
  216. }
  217. private function loadPlugins():void {
  218. var plugins:Array = _config.getLoadables();
  219. log.info("will load following plugins: ");
  220. logPluginInfo(plugins);
  221. _pluginLoader = new PluginLoader(URLUtil.playerBaseUrl(loaderInfo), _pluginRegistry, this, useExternalInterfade());
  222. _pluginLoader.addEventListener(Event.COMPLETE, pluginLoadListener);
  223. _flowplayer.pluginLoader = _pluginLoader;
  224. if (plugins.length == 0) {
  225. log.debug("configuration has no plugins");
  226. initPhase3();
  227. } else {
  228. // _builtInPlugins = _config.createLoadables(BuiltInConfig.config.plugins);
  229. // log.debug("following built-in plugins will be instantiated");
  230. // trace("builtIn plugins: ");
  231. // logPluginInfo(_builtInPlugins, true);
  232. _pluginLoader.load(plugins, onPluginLoad, onPluginLoadError);
  233. }
  234. }
  235. private function logPluginInfo(plugins:Array, doTrace:Boolean = false):void {
  236. for (var i:Number = 0; i < plugins.length; i++) {
  237. log.info("" + plugins[i]);
  238. if (doTrace) {
  239. trace("" + plugins[i]);
  240. }
  241. }
  242. }
  243. private function pluginLoadListener(event:Event = null):void {
  244. _pluginLoader.removeEventListener(Event.COMPLETE, pluginLoadListener);
  245. callAndHandleError(initPhase3, PlayerError.INIT_FAILED);
  246. }
  247. private function loadPlaylistFeed():void {
  248. var playlistFeed:String = _config.playlistFeed;
  249. if (! playlistFeed) {
  250. callAndHandleError(initPhase2, PlayerError.INIT_FAILED);
  251. return;
  252. }
  253. log.info("loading playlist from " + playlistFeed);
  254. _playlistLoader = _flowplayer.createLoader();
  255. _playlistLoader.addTextResourceUrl(playlistFeed);
  256. _playlistLoader.load(null,
  257. function(loader:ResourceLoader):void {
  258. log.info("received playlist feed");
  259. _config.playlistDocument = loader.getContent() as String;
  260. _config.getPlaylist().dispatchPlaylistReplace();
  261. callAndHandleError(initPhase2, PlayerError.INIT_FAILED);
  262. });
  263. }
  264. private function onPluginLoad(event:PluginEvent):void {
  265. var plugin:PluginModel = event.target as PluginModel;
  266. log.info("plugin " + plugin + " initialized");
  267. checkPluginsInitialized();
  268. }
  269. private function onPluginLoadError(event:PluginEvent):void {
  270. if (event.target is Loadable) {
  271. handleError(PlayerError.PLUGIN_LOAD_FAILED, "unable to load plugin '" + Loadable(event.target).name + "', url: '" + Loadable(event.target).url + "'");
  272. // throw new Error("unable to load plugin '" + Loadable(event.target).name + "', url: '" + Loadable(event.target).url + "'");
  273. } else {
  274. var plugin:PluginModel = event.target as PluginModel;
  275. _pluginRegistry.removePlugin(plugin);
  276. handleError(PlayerError.PLUGIN_LOAD_FAILED, "load/init error on " + plugin);
  277. }
  278. }
  279. private function checkPluginsInitialized():void {
  280. var numPlugins:int = countPlugins();
  281. if (++_pluginsInitialized == numPlugins) {
  282. log.info("all plugins initialized");
  283. callAndHandleError(initPhase4, PlayerError.INIT_FAILED);
  284. }
  285. log.info(_pluginsInitialized + " out of " + numPlugins + " plugins initialized");
  286. }
  287. private function countPlugins():int {
  288. var count:Number = 0;
  289. var loadables:Array = _config.getLoadables();
  290. for (var i:Number = 0; i < loadables.length; i++) {
  291. var plugin:PluginModel = Loadable(loadables[i]).plugin;
  292. if (! plugin) {
  293. handleError(PlayerError.PLUGIN_LOAD_FAILED, "Unable to load plugin, url " + Loadable(loadables[i]).url + ", name " + Loadable(loadables[i]).name);
  294. // throw new Error("Plugin " + loadables[i] + " not available");
  295. }
  296. var isNonAdHocPlugin:Boolean = plugin.pluginObject is Plugin;
  297. // var isNonAdHocPlugin:Boolean = (plugin is DisplayPluginModel && DisplayPluginModel(plugin).getDisplayObject() is Plugin) ||
  298. // plugin is ProviderModel && ProviderModel(plugin).pluginObject is Plugin;
  299. if (Loadable(loadables[i]).loadFailed) {
  300. log.debug("load failed for " + loadables[i]);
  301. count++;
  302. } else if (! plugin) {
  303. log.debug("this plugin is not loaded yet");
  304. count++;
  305. } else if (isNonAdHocPlugin) {
  306. log.debug("will wait for onLoad from plugin " + plugin);
  307. count++;
  308. } else {
  309. log.debug("will NOT wait for onLoad from plugin " + Loadable(loadables[i]).plugin);
  310. }
  311. }
  312. // +1 comes from the playbuttonoverlay
  313. return count + (_playButtonOverlay ? 1 : 0);
  314. }
  315. private function playerSwfName():String {
  316. var url:String = loaderInfo.url;
  317. var lastSlash:Number = url.lastIndexOf("/");
  318. return url.substring(lastSlash + 1, url.indexOf(".swf") + 4);
  319. }
  320. private function validateLicenseKey():Boolean {
  321. try {
  322. return LicenseKey.validate(root.loaderInfo.url, _flowplayer.version, _config.licenseKey, useExternalInterfade());
  323. } catch (e:Error) {
  324. log.warn("License key not accepted, will show flowplayer logo");
  325. }
  326. return false;
  327. }
  328. private function createFullscreenManager(playlist:Playlist):void {
  329. _fullscreenManager = new FullscreenManager(stage, playlist, _panel, _pluginRegistry, _animationEngine);
  330. _flowplayer.fullscreenManager = _fullscreenManager;
  331. }
  332. public function showError(message:String):void {
  333. if (! _panel) return;
  334. if (! _config.showErrors) return;
  335. if (_error && _error.parent == this) {
  336. removeChild(_error);
  337. }
  338. _error = TextUtil.createTextField(false);
  339. _error.background = true;
  340. _error.backgroundColor = 0;
  341. _error.textColor = 0xffffff;
  342. _error.autoSize = TextFieldAutoSize.CENTER;
  343. _error.multiline = true;
  344. _error.wordWrap = true;
  345. _error.text = message;
  346. _error.selectable = true;
  347. _error.width = stage.stageWidth - 40;
  348. Arrange.center(_error, stage.stageWidth, stage.stageHeight);
  349. addChild(_error);
  350. createErrorMessageHideTimer();
  351. }
  352. private function createErrorMessageHideTimer():void {
  353. var errorHideTimer:Timer = new Timer(10000, 1);
  354. errorHideTimer.addEventListener(TimerEvent.TIMER_COMPLETE, hideErrorMessage);
  355. errorHideTimer.start();
  356. }
  357. private function hideErrorMessage(event:TimerEvent = null):void {
  358. if (_error && _error.parent == this) {
  359. if (_animationEngine) {
  360. _animationEngine.fadeOut(_error, 1000, function():void { removeChild(_error); });
  361. } else {
  362. removeChild(_error);
  363. }
  364. }
  365. }
  366. public function handleError(error:ErrorCode, info:Object = null, throwError:Boolean = true):void {
  367. if (_flowplayer) {
  368. _flowplayer.dispatchError(error, info);
  369. } else {
  370. // initialization is not complete, create a dispatches just to dispatch this error
  371. new PlayerEventDispatcher().dispatchError(error, info);
  372. }
  373. doHandleError(error.code + ": " + error.message + ( info ? ": " + info : ""), throwError);
  374. }
  375. private function doHandleError(message:String, throwError:Boolean = true):void {
  376. if (_config && _config.playerId) {
  377. Logger.error(message);
  378. }
  379. showError(message);
  380. if (throwError && Capabilities.isDebugger) {
  381. throw new Error(message);
  382. }
  383. }
  384. private function createAnimationEngine(pluginRegistry:PluginRegistry):void {
  385. _animationEngine = new AnimationEngine(_panel, pluginRegistry);
  386. }
  387. private function addPluginsToPanel(_pluginRegistry:PluginRegistry):void {
  388. for each (var pluginObj:Object in _pluginRegistry.plugins) {
  389. if (pluginObj is DisplayPluginModel) {
  390. var model:DisplayPluginModel = pluginObj as DisplayPluginModel;
  391. log.debug("adding plugin '"+ model.name +"' to panel: " + model.visible + ", plugin object is " + model.getDisplayObject());
  392. if (model.visible) {
  393. if (model.zIndex == -1) {
  394. model.zIndex = 100;
  395. }
  396. _panel.addView(model.getDisplayObject(), undefined, model);
  397. }
  398. if (model.name == "controls") {
  399. _controlsModel = model;
  400. }
  401. }
  402. }
  403. if (_controlsModel) {
  404. arrangeCanvasLogo();
  405. }
  406. }
  407. private function addScreenToPanel():void {
  408. // if controls visible and screen was not explicitly configured --> place screen on top of controls
  409. var screen:DisplayProperties = _pluginRegistry.getPlugin("screen") as DisplayProperties;
  410. screen.display = "none";
  411. screen.getDisplayObject().visible = false;
  412. _panel.addView(screen.getDisplayObject(), null, screen);
  413. }
  414. private function arrangeScreen():void {
  415. if (! _pluginRegistry) return;
  416. var screen:DisplayProperties = _pluginRegistry.getPlugin("screen") as DisplayProperties;
  417. if (! screen) return;
  418. if (_controlsModel && _controlsModel.visible) {
  419. if (isControlsAlwaysAutoHide() || (_controlsModel.position.bottom.px > 0)) {
  420. log.debug("controls is autoHide or it's in a non-default vertical position, configuring screen to take all available space");
  421. setScreenBottomAndHeight(screen, 100, 0);
  422. } else {
  423. var controlsHeight:Number = _controlsModel.getDisplayObject().height;
  424. var occupiedHeight:Number = screenTopOrBottomConfigured() ? getScreenTopOrBottomPx(screen) : controlsHeight;
  425. log.debug("occupied by controls or screen's configured bottom/top is " + occupiedHeight);
  426. var heightPct:Number = 0;
  427. if (screenTopOrBottomConfigured() && (screen.position.top.pct >= 0 || screen.position.bottom.pct >= 0)) {
  428. heightPct = 100 - Math.abs(50 - (screen.position.top.pct >= 0 ? screen.position.top.pct : screen.position.bottom.pct))*2;
  429. setScreenBottomAndHeight(screen, heightPct, controlsHeight);
  430. } else {
  431. heightPct = ((stage.stageHeight - occupiedHeight) / stage.stageHeight) * 100;
  432. setScreenBottomAndHeight(screen, heightPct, controlsHeight);
  433. }
  434. }
  435. }
  436. log.debug("arranging screen to pos " + screen.position);
  437. screen.display = "block";
  438. screen.getDisplayObject().visible = true;
  439. _pluginRegistry.updateDisplayProperties(screen, true);
  440. _panel.update(screen.getDisplayObject(), screen);
  441. _panel.draw(screen.getDisplayObject());
  442. }
  443. private function getScreenTopOrBottomPx(screen:DisplayProperties):Number {
  444. var screenConf:Object = _config.getObject("screen");
  445. if (screenConf.hasOwnProperty("top")) return screen.position.top.toPx(stage.stageHeight);
  446. if (screenConf.hasOwnProperty("bottom")) return screen.position.bottom.toPx(stage.stageHeight);
  447. return 0;
  448. }
  449. private function setScreenBottomAndHeight(screen:DisplayProperties, heightPct:Number, bottom:Number = 0):void {
  450. if (! screenTopOrBottomConfigured()) {
  451. log.debug("screen vertical pos not configured, setting bottom to value " + bottom);
  452. screen.bottom = bottom;
  453. } else {
  454. log.debug("using configured top/bottom for screen");
  455. }
  456. var heightConfigured:Boolean = _config.getObject("screen") && _config.getObject("screen").hasOwnProperty("height");
  457. if (! heightConfigured) {
  458. log.debug("screen height not configured, setting it to value " + heightPct + "%");
  459. screen.height = heightPct + "%";
  460. } else {
  461. log.debug("using configured height for screen");
  462. }
  463. }
  464. private function screenTopOrBottomConfigured():Boolean {
  465. var screen:Object = _config.getObject("screen");
  466. if (! screen) return false;
  467. if (screen.hasOwnProperty("top")) return true;
  468. if (screen.hasOwnProperty("bottom")) return true;
  469. return false;
  470. }
  471. private function isControlsAlwaysAutoHide():Boolean {
  472. if (!_controlsModel) return false;
  473. if (!_controlsModel.config) return false;
  474. log.debug("controlsModel.config.auotoHide == always", (_controlsModel.config.autoHide == 'always'));
  475. return _controlsModel.config.autoHide == 'always';
  476. }
  477. private function createFlowplayer():void {
  478. _flowplayer = new Flowplayer(stage, _pluginRegistry, _panel,
  479. _animationEngine, this, this, _config, URLUtil.playerBaseUrl(loaderInfo));
  480. _flowplayer.onBeforeFullscreen(onFullscreen);
  481. // _flowplayer.onFullscreenExit(onFullscreen);
  482. }
  483. private function onFullscreen(event:PlayerEvent):void {
  484. log.debug("entering fullscreen, disabling display clicks");
  485. _enteringFullscreen = true;
  486. var delay:Timer = new Timer(1000, 1);
  487. delay.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
  488. delay.start();
  489. }
  490. private function onTimerComplete(event:TimerEvent):void {
  491. log.debug("fullscreen wait delay complete, display clicks are enabled again");
  492. _enteringFullscreen = false;
  493. }
  494. private function createFlashVarsConfig():void {
  495. if (! root.loaderInfo.parameters) {
  496. return;
  497. }
  498. var configStr:String = Preloader(root).injectedConfig || root.loaderInfo.parameters["config"];
  499. var configObj:Object = configStr && configStr.indexOf("{") == 0 ? ConfigParser.parse(configStr) : {};
  500. if (! configStr || (configStr && configStr.indexOf("{") == 0 && ! configObj.hasOwnProperty("url"))) {
  501. _config = ConfigParser.parseConfig(configObj, BuiltInConfig.config, playerSwfName(), VersionInfo.controlsVersion, VersionInfo.audioVersion);
  502. callAndHandleError(initPhase1, PlayerError.INIT_FAILED);
  503. } else {
  504. ConfigParser.loadConfig(configObj.hasOwnProperty("url") ? String(configObj["url"]) : configStr, BuiltInConfig.config, function(config:Config):void {
  505. _config = config;
  506. callAndHandleError(initPhase1, PlayerError.INIT_FAILED);
  507. }, new ResourceLoaderImpl(null, this), playerSwfName(), VersionInfo.controlsVersion, VersionInfo.audioVersion);
  508. }
  509. }
  510. private function createPlayListController():PlayListController {
  511. createHttpProviders();
  512. var playListController:PlayListController = new PlayListController(_config.getPlaylist(), _providers, _config, createNewLoader());
  513. playListController.playerEventDispatcher = _flowplayer;
  514. _flowplayer.playlistController = playListController;
  515. return playListController;
  516. }
  517. private function createHttpProviders():void {
  518. if (! _providers) {
  519. _providers = new Dictionary();
  520. }
  521. _providers["http"] = createProvider("http");
  522. _providers["httpInstream"] = createProvider("httpInstream");
  523. }
  524. private function createProvider(name:String):Object {
  525. log.debug("creating provider with name " + name);
  526. var httpProvider:ProviderModel = _config.createHttpProvider(name);
  527. _pluginRegistry.registerProvider(httpProvider);
  528. return httpProvider.pluginObject;
  529. }
  530. private function get hasHttpChildClip():Boolean {
  531. var children:Array = _config.getPlaylist().childClips;
  532. // log.debug("configuration has child clips", children);
  533. for (var i:int = 0; i < children.length; i++) {
  534. if (Clip(children[i]).provider == "httpInstream") {
  535. log.info("child clip with http provider found");
  536. return true;
  537. }
  538. }
  539. return false;
  540. }
  541. private function createScreen():void {
  542. _screen = new Screen(_config.getPlaylist(), _animationEngine, _playButtonOverlay, _pluginRegistry);
  543. var screenModel:DisplayProperties = _config.getScreenProperties();
  544. initView(_screen, screenModel, null, false);
  545. if (_playButtonOverlay) {
  546. PlayButtonOverlayView(_playButtonOverlay.getDisplayObject()).setScreen(_screen, hasClip);
  547. }
  548. // addViewLiteners(_screen);
  549. }
  550. private function createPlayButtonOverlay():void {
  551. _playButtonOverlay = _config.getPlayButtonOverlay();
  552. if (! _playButtonOverlay) return;
  553. _playButtonOverlay.onLoad(onPluginLoad);
  554. _playButtonOverlay.onError(onPluginLoadError);
  555. var overlay:PlayButtonOverlayView = new PlayButtonOverlayView(! playButtonOverlayWidthDefined(), _playButtonOverlay, _pluginRegistry);
  556. initView(overlay, _playButtonOverlay, null, false);
  557. }
  558. private function playButtonOverlayWidthDefined():Boolean {
  559. if (! _config.getObject("play")) return false;
  560. return _config.getObject("play").hasOwnProperty("width");
  561. }
  562. private function get hasClip():Boolean {
  563. var firstClip:Clip = _config.getPlaylist().current;
  564. var hasClip:Boolean = ! firstClip.isNullClip && (firstClip.url || firstClip.provider != 'http');
  565. return hasClip;
  566. }
  567. private function createLogo():void {
  568. var logo:Logo = _config.getLogo() || new Logo();
  569. var logoView:LogoView = new LogoView(_panel, logo, _flowplayer);
  570. initView(logoView, logo, logoView.draw, false);
  571. }
  572. private function initView(view:DisplayObject, props:DisplayProperties, resizeListener:Function = null, addToPanel:Boolean = true):void {
  573. if (props.name != "logo" || VersionInfo.commercial) {
  574. _pluginRegistry.registerDisplayPlugin(props, view);
  575. }
  576. if (addToPanel) {
  577. _panel.addView(view, resizeListener, props);
  578. }
  579. if (props is Callable) {
  580. ExternalInterfaceHelper.initializeInterface(props as Callable, view);
  581. }
  582. }
  583. private function addListeners():void {
  584. _clickTimer.addEventListener(TimerEvent.TIMER, onClickTimer);
  585. doubleClickEnabled = true;
  586. addEventListener(MouseEvent.DOUBLE_CLICK, onDoubleClick);
  587. _screen.addEventListener(MouseEvent.CLICK, onClickEvent);
  588. if (_playButtonOverlay) {
  589. _playButtonOverlay.getDisplayObject().addEventListener(MouseEvent.CLICK, onClickEvent);
  590. }
  591. addEventListener(MouseEvent.ROLL_OVER, onMouseOver);
  592. addEventListener(MouseEvent.ROLL_OUT, onMouseOut);
  593. // add some color so that the ROLL_OVER/ROLL_OUT events are always triggered
  594. graphics.beginFill(0, 0);
  595. graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
  596. graphics.endFill();
  597. }
  598. private function onMouseOut(event:MouseEvent):void {
  599. _flowplayer.dispatchEvent(PlayerEvent.mouseOut());
  600. }
  601. private function onMouseOver(event:MouseEvent):void {
  602. _flowplayer.dispatchEvent(PlayerEvent.mouseOver());
  603. }
  604. private function createPanel():void {
  605. _panel = new Panel();
  606. addChild(_panel);
  607. }
  608. private function startStreams():void {
  609. var canStart:Boolean = true;
  610. if (_flowplayer.state != State.WAITING) {
  611. log.debug("streams have been started in player.onLoad(), will not start streams here.");
  612. canStart = false;
  613. }
  614. if (! hasClip) {
  615. log.info("Configuration has no clips to play.");
  616. canStart = false;
  617. }
  618. var playButton:PlayButtonOverlayView = _playButtonOverlay ? PlayButtonOverlayView(_playButtonOverlay.getDisplayObject()) : null;
  619. if (canStart) {
  620. if (_flowplayer.currentClip.autoPlay) {
  621. log.debug("clip is autoPlay");
  622. _flowplayer.play();
  623. } else if (_flowplayer.currentClip.autoBuffering) {
  624. log.debug("clip is autoBuffering");
  625. _flowplayer.startBuffering();
  626. } else {
  627. if (playButton) {
  628. playButton.stopBuffering();
  629. playButton.showButton();
  630. }
  631. }
  632. } else {
  633. // cannot start playing here, stop buffering indicator, don't show the button
  634. if (playButton) {
  635. playButton.stopBuffering();
  636. }
  637. }
  638. }
  639. private function addPlayListListeners():void {
  640. var playlist:Playlist = _config.getPlaylist();
  641. playlist.onError(onClipError);
  642. }
  643. private function onClipError(event:ClipEvent):void {
  644. if (event.isDefaultPrevented()) return;
  645. doHandleError(event.error.code + ", " + event.error.message + ", " + event.info2 + ", clip: '" + Clip(event.target) + "'", false);
  646. }
  647. private function onClickTimer(event:TimerEvent):void {
  648. if (_clickCount == 1) {
  649. onSingleClick(_clickEvent);
  650. }
  651. _clickCount = 0;
  652. }
  653. private function onDoubleClick(event:MouseEvent = null):void {
  654. log.debug("onDoubleClick");
  655. _flowplayer.toggleFullscreen();
  656. }
  657. private function onSingleClick(event:MouseEvent):void {
  658. if (isParent(DisplayObject(event.target), _screen)) {
  659. log.debug("screen clicked");
  660. var clip:Clip = _flowplayer.playlist.current;
  661. if (clip.linkUrl) {
  662. log.debug("opening linked page");
  663. _flowplayer.pause();
  664. navigateToURL(new URLRequest(clip.linkUrl), clip.linkWindow);
  665. return;
  666. }
  667. _flowplayer.toggle();
  668. }
  669. }
  670. private function onClickEvent(event:MouseEvent):void {
  671. if (_enteringFullscreen) return;
  672. log.debug("onViewClicked, target " + event.target + ", current target " + event.currentTarget);
  673. event.stopPropagation();
  674. if (_playButtonOverlay && isParent(DisplayObject(event.target), _playButtonOverlay.getDisplayObject())) {
  675. _flowplayer.toggle();
  676. return;
  677. }
  678. if (_clickCount == 0) {
  679. _clickEvent = event;
  680. }
  681. if (++_clickCount == 2) {
  682. onDoubleClick(event);
  683. } else {
  684. _clickTimer.start();
  685. }
  686. }
  687. private function isParent(child:DisplayObject, parent:DisplayObject):Boolean {
  688. try {
  689. if (DisplayObject(child).parent == parent) return true;
  690. if (! (parent is DisplayObjectContainer)) return false;
  691. for (var i:Number = 0;i < DisplayObjectContainer(parent).numChildren; i++) {
  692. var curChild:DisplayObject = DisplayObjectContainer(parent).getChildAt(i);
  693. if (isParent(child, curChild)) {
  694. return true;
  695. }
  696. }
  697. } catch (e:SecurityError) {
  698. return true;
  699. }
  700. return false;
  701. }
  702. private function onKeyDown(event:KeyboardEvent):void {
  703. log.debug("keydown");
  704. if (_enteringFullscreen) return;
  705. if (_flowplayer.dispatchBeforeEvent(PlayerEvent.keyPress(event.keyCode))) {
  706. _flowplayer.dispatchEvent(PlayerEvent.keyPress(event.keyCode));
  707. if (event.keyCode == Keyboard.SPACE) {
  708. _flowplayer.toggle();
  709. }
  710. }
  711. }
  712. override protected function onRedraw():void {
  713. if (bgImageHolder && getChildIndex(bgImageHolder) > getChildIndex(_panel)) {
  714. swapChildren(bgImageHolder, _panel);
  715. }
  716. }
  717. private function createLogoForCanvas():void {
  718. if (_canvasLogo) return;
  719. _copyrightNotice = LogoUtil.createCopyrightNotice(8);
  720. addChild(_copyrightNotice);
  721. _canvasLogo = new CanvasLogo();
  722. _canvasLogo.width = 85;
  723. _canvasLogo.scaleY = _canvasLogo.scaleX;
  724. _canvasLogo.alpha = .4;
  725. _canvasLogo.addEventListener(MouseEvent.CLICK,
  726. function(event:MouseEvent):void { navigateToURL(new URLRequest("http://flowplayer.org"), "_self"); });
  727. _canvasLogo.buttonMode = true;
  728. log.debug("adding logo to display list");
  729. addChild(_canvasLogo);
  730. onStageResize();
  731. }
  732. private function createNewLoader():ResourceLoader {
  733. return new ResourceLoaderImpl(_config.playerId ? null : URLUtil.playerBaseUrl(loaderInfo), this);
  734. }
  735. private function initCustomClipEvents():void {
  736. createCustomClipEvents(_config.connectionCallbacks);
  737. createCustomClipEvents(_config.streamCallbacks);
  738. }
  739. private function createCustomClipEvents(callbacks:Array):void {
  740. if (! callbacks) return;
  741. for (var i:int = 0; i < callbacks.length; i++) {
  742. log.debug("creating custom event type " + callbacks[i]);
  743. new ClipEventType(callbacks[i], true);
  744. }
  745. }
  746. //
  747. // private function get stageWidth():Number {
  748. // return Math.max(stage.stageWidth, Preloader.stageWidth);
  749. // }
  750. //
  751. // private function get stageHeight():Number {
  752. // return Math.max(stage.stageHeight, Preloader.stageHeight);
  753. // }
  754. private function callAndHandleError(func:Function, error:PlayerError):void {
  755. try {
  756. func();
  757. } catch (e:Error) {
  758. handleError(error, e, false);
  759. throw e;
  760. }
  761. }
  762. }
  763. }