PageRenderTime 47ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/flowplayer/tags/flow_3_0_5/src/actionscript/org/flowplayer/view/Launcher.as

http://flowplayer-core.googlecode.com/
ActionScript | 696 lines | 572 code | 99 blank | 25 comment | 112 complexity | 811cf4defa5f5a3e9d4abfc597b65943 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.model.Loadable;
  21. import org.flowplayer.model.ProviderModel;
  22. import org.flowplayer.config.Config;
  23. import org.flowplayer.config.ConfigLoader;
  24. import org.flowplayer.config.ExternalInterfaceHelper;
  25. import org.flowplayer.config.VersionInfo;
  26. import org.flowplayer.controller.NetStreamControllingStreamProvider;
  27. import org.flowplayer.controller.PlayListController;
  28. import org.flowplayer.controller.ResourceLoader;
  29. import org.flowplayer.controller.ResourceLoaderImpl;
  30. import org.flowplayer.flow_internal;
  31. import org.flowplayer.model.Callable;
  32. import org.flowplayer.model.Clip;
  33. import org.flowplayer.model.ClipEvent;
  34. import org.flowplayer.model.DisplayPluginModel;
  35. import org.flowplayer.model.DisplayProperties;
  36. import org.flowplayer.model.DisplayPropertiesImpl;
  37. import org.flowplayer.model.EventDispatcher;
  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.State;
  48. import org.flowplayer.util.Arrange;
  49. import org.flowplayer.util.Log;
  50. import org.flowplayer.util.TextUtil;
  51. import org.flowplayer.util.URLUtil;
  52. import org.flowplayer.view.Panel;
  53. import org.flowplayer.view.Screen;
  54. import org.osflash.thunderbolt.Logger;
  55. import flash.display.DisplayObject;
  56. import flash.display.DisplayObjectContainer;
  57. import flash.display.Sprite;
  58. import flash.events.Event;
  59. import flash.events.KeyboardEvent;
  60. import flash.events.MouseEvent;
  61. import flash.events.TimerEvent;
  62. import flash.net.URLRequest;
  63. import flash.net.navigateToURL;
  64. import flash.system.Capabilities;
  65. import flash.system.Security;
  66. import flash.text.TextField;
  67. import flash.text.TextFieldAutoSize;
  68. import flash.ui.Keyboard;
  69. import flash.utils.Dictionary;
  70. import flash.utils.Timer;
  71. use namespace flow_internal;
  72. public class Launcher extends StyleableSprite implements ErrorHandler {
  73. private var _panel:Panel;
  74. private var _screen:Screen;
  75. private var _config:Config;
  76. private var _flowplayer:Flowplayer;
  77. private var _pluginRegistry:PluginRegistry;
  78. private var _animationEngine:AnimationEngine;
  79. private var _playButtonOverlay:PlayButtonOverlay;
  80. private var _controlsModel:DisplayPluginModel; private var _providers:Dictionary = new Dictionary();
  81. private var _fullscreenManager:FullscreenManager;
  82. private var _canvasLogo:Sprite;
  83. private var _pluginLoader:PluginLoader;
  84. private var _error:TextField;
  85. private var _pluginsInitialized:Number = 0;
  86. private var _numLoadablePlugins:int = -1;
  87. private var _enteringFullscreen:Boolean; private var _copyrightNotice:TextField;
  88. [Frame(factoryClass="org.flowplayer.view.Preloader")]
  89. public function Launcher() {
  90. super("#canvas", this);
  91. addEventListener(Event.ADDED_TO_STAGE, initPhase1);
  92. }
  93. private function initPhase1(event:Event):void {
  94. createFlashVarsConfig();
  95. Log.configure(_config.getLogConfiguration());
  96. if (_config.playerId) {
  97. Security.allowDomain(URLUtil.pageUrl);
  98. }
  99. _config.getPlaylist().onBeforeBegin(function(event:ClipEvent):void { hideErrorMessage(); });
  100. loader = createNewLoader();
  101. rootStyle = _config.canvasStyle;
  102. stage.addEventListener(Event.RESIZE, onStageResize);
  103. setSize(stage.stageWidth, stage.stageHeight);
  104. if (! VersionInfo.commercial) {
  105. log.debug("Adding logo to canvas");
  106. createLogoForCanvas();
  107. }
  108. log = new Log(this);
  109. EventDispatcher.playerId = _config.playerId;
  110. log.debug("security sandbox type: " + Security.sandboxType);
  111. log.info(VersionInfo.versionInfo());
  112. log.debug("creating Panel");
  113. createPanel();
  114. _pluginRegistry = new PluginRegistry(_panel);
  115. log.debug("Creating animation engine");
  116. createAnimationEngine(_pluginRegistry);
  117. log.debug("creating play button overlay");
  118. createPlayButtonOverlay();
  119. log.debug("creating screen");
  120. createScreen();
  121. loadPluginsIfConfigured();
  122. }
  123. private function initPhase2(pluginsLoadedEvent:Event = null):void {
  124. _pluginLoader.removeEventListener(Event.COMPLETE, this.initPhase2);
  125. log.debug("creating PlayListController");
  126. _providers = _pluginLoader.providers;
  127. var playListController:PlayListController = createPlayListController();
  128. addPlayListListeners();
  129. createFullscreenManager(playListController.playlist);
  130. log.debug("creating Flowplayer API");
  131. createFlowplayer(playListController);
  132. addScreenToPanel();
  133. if (!validateLicenseKey()) {
  134. createLogoForCanvas();
  135. resizeCanvasLogo();
  136. }
  137. log.debug("creating logo");
  138. createLogo();
  139. contextMenu = new ContextMenuBuilder(_config.playerId, _config.contextMenu).build();
  140. log.debug("initializing ExternalInterface");
  141. if (useExternalInterfade()) {
  142. _flowplayer.initExternalInterface();
  143. }
  144. log.debug("calling onLoad to plugins");
  145. _pluginRegistry.onLoad(_flowplayer);
  146. }
  147. private function initPhase3(event:Event = null):void {
  148. log.debug("Adding visible plugins to panel");
  149. addPluginsToPanel(_pluginRegistry);
  150. log.debug("arranging screen");
  151. arrangeScreen();
  152. log.debug("dispatching onLoad");
  153. if (useExternalInterfade()) {
  154. _flowplayer.dispatchEvent(PlayerEvent.load("player"));
  155. }
  156. log.debug("starting configured streams");
  157. startStreams();
  158. stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
  159. addListeners();
  160. }
  161. private function resizeCanvasLogo():void {
  162. _canvasLogo.alpha = 1;
  163. _canvasLogo.width = 150;
  164. _canvasLogo.scaleY = _canvasLogo.scaleX;
  165. arrangeCanvasLogo();
  166. }
  167. private function useExternalInterfade():Boolean {
  168. log.debug("useExternalInteface: " + (_config.playerId != null));
  169. return _config.playerId != null;
  170. }
  171. private function onStageResize(event:Event = null):void {
  172. setSize(stage.stageWidth, stage.stageHeight);
  173. arrangeCanvasLogo();
  174. }
  175. private function arrangeCanvasLogo():void {
  176. if (!_canvasLogo) return;
  177. _canvasLogo.x = 15;
  178. _canvasLogo.y = stage.stageHeight - (_controlsModel ? _controlsModel.dimensions.height.toPx(stage.stageHeight) + 10 : 10) - _canvasLogo.height - _copyrightNotice.height;
  179. _copyrightNotice.x = 12;
  180. _copyrightNotice.y = _canvasLogo.y + _canvasLogo.height;
  181. }
  182. private function loadPluginsIfConfigured():void {
  183. var plugins:Array = _config.getLoadables();
  184. log.info("will load following plugins: ");
  185. for (var i:Number = 0; i < plugins.length; i++) {
  186. log.info("" + plugins[i]);
  187. }
  188. _pluginLoader = new PluginLoader(URLUtil.playerBaseUrl(loaderInfo), _pluginRegistry, this, useExternalInterfade(), onPluginLoad, onPluginLoadError);
  189. _pluginLoader.addEventListener(Event.COMPLETE, initPhase2);
  190. if (plugins.length == 0) {
  191. log.debug("configuration has no plugins");
  192. initPhase2();
  193. } else {
  194. log.debug("loading plugins and providers");
  195. _pluginLoader.load(plugins);
  196. }
  197. }
  198. private function onPluginLoad(event:PluginEvent):void {
  199. var plugin:PluginModel = event.target as PluginModel;
  200. log.info("plugin " + plugin + " initialized");
  201. checkPluginsInitialized();
  202. }
  203. private function onPluginLoadError(event:PluginEvent):void {
  204. if (! event.hasError(PluginError.INIT_FAILED)) return;
  205. var plugin:PluginModel = event.target as PluginModel;
  206. log.warn("load/init error on " + plugin);
  207. _pluginRegistry.removePlugin(plugin);
  208. checkPluginsInitialized();
  209. }
  210. private function checkPluginsInitialized():void {
  211. var numPlugins:int = getLoadablePluginCount();
  212. if (++_pluginsInitialized == numPlugins) {
  213. log.info("all plugins initialized");
  214. initPhase3();
  215. }
  216. log.info(_pluginsInitialized + " out of " + numPlugins + " plugins initialized");
  217. }
  218. private function getLoadablePluginCount():int {
  219. if (_numLoadablePlugins == -1) {
  220. _numLoadablePlugins = countLoadablePlugins();
  221. }
  222. return _numLoadablePlugins;
  223. }
  224. private function countLoadablePlugins():int {
  225. var count:Number = 0;
  226. var loadables:Array = _config.getLoadables();
  227. for (var i:Number = 0; i < loadables.length; i++) {
  228. var plugin:PluginModel = Loadable(loadables[i]).plugin;
  229. var isNonAdHocPlugin:Boolean = (plugin is DisplayPluginModel && DisplayPluginModel(plugin).getDisplayObject() is Plugin) ||
  230. plugin is ProviderModel && ProviderModel(plugin).getProviderObject() is Plugin;
  231. if (isNonAdHocPlugin) {
  232. log.debug("will wait for onLoad from plugin " + plugin);
  233. count++;
  234. } else {
  235. log.debug("will NOT wait for onLoad from plugin " + Loadable(loadables[i]).plugin);
  236. }
  237. }
  238. // +1 comes from the playbuttonoverlay
  239. return count + (_playButtonOverlay ? 1 : 0);
  240. }
  241. private function playerSwfName():String {
  242. var url:String = loaderInfo.url;
  243. var lastSlash:Number = url.lastIndexOf("/");
  244. return url.substring(lastSlash + 1, url.indexOf(".swf") + 4);
  245. }
  246. private function validateLicenseKey():Boolean {
  247. try {
  248. return LicenseKey.validate(useExternalInterfade() ? null: root.loaderInfo.url, _flowplayer.version, _config.licenseKey);
  249. } catch (e:Error) {
  250. log.warn("License key not accepted, will show flowplayer logo");
  251. }
  252. return false;
  253. }
  254. private function createFullscreenManager(playlist:Playlist):void {
  255. _fullscreenManager = new FullscreenManager(stage, playlist, _panel, _pluginRegistry, _animationEngine);
  256. }
  257. public function showError(message:String):void {
  258. if (! _panel) return;
  259. if (! _config.showErrors) return;
  260. if (_error && _error.parent == this) {
  261. removeChild(_error);
  262. }
  263. _error = TextUtil.createTextField(false);
  264. _error.background = true;
  265. _error.backgroundColor = 0;
  266. _error.textColor = 0xffffff;
  267. _error.autoSize = TextFieldAutoSize.CENTER;
  268. _error.multiline = true;
  269. _error.wordWrap = true;
  270. _error.text = message;
  271. _error.selectable = true;
  272. _error.width = stage.stageWidth - 40;
  273. Arrange.center(_error, stage.stageWidth, stage.stageHeight);
  274. addChild(_error);
  275. createErrorMessageHideTimer();
  276. } private function createErrorMessageHideTimer():void {
  277. var errorHideTimer:Timer = new Timer(4000, 1);
  278. errorHideTimer.addEventListener(TimerEvent.TIMER_COMPLETE, hideErrorMessage);
  279. errorHideTimer.start();
  280. }
  281. private function hideErrorMessage(event:TimerEvent = null):void {
  282. if (_error && _error.parent == this) {
  283. if (_animationEngine) {
  284. _animationEngine.fadeOut(_error, 1000, function():void { removeChild(_error); });
  285. } else {
  286. removeChild(_error);
  287. }
  288. }
  289. }
  290. public function handleError(error:PlayerError, info:Object = null, throwError:Boolean = true):void {
  291. if (_flowplayer) {
  292. _flowplayer.dispatchError(error, info);
  293. } else {
  294. // initialization is not complete, create a dispatches just to dispatch this error
  295. new PlayerEventDispatcher().dispatchError(error, info);
  296. }
  297. doHandleError(error.code + ": " + error.message + ( info ? ": " + info : ""), throwError);
  298. }
  299. private function doHandleError(message:String, throwError:Boolean = true):void {
  300. if (_config && _config.playerId) {
  301. Logger.error(message);
  302. }
  303. showError(message);
  304. if (throwError && Capabilities.isDebugger) {
  305. throw new Error(message);
  306. }
  307. }
  308. private function createAnimationEngine(pluginRegistry:PluginRegistry):void {
  309. _animationEngine = new AnimationEngine(_panel, pluginRegistry);
  310. }
  311. private function addPluginsToPanel(_pluginRegistry:PluginRegistry):void {
  312. for each (var pluginObj:Object in _pluginRegistry.plugins) {
  313. if (pluginObj is DisplayPluginModel) {
  314. var model:DisplayPluginModel = pluginObj as DisplayPluginModel;
  315. log.debug("adding plugin '"+ model.name +"' to panel: " + model.visible + ", plugin object is " + model.getDisplayObject());
  316. if (model.visible) {
  317. if (model.zIndex == -1) {
  318. model.zIndex = 100;
  319. }
  320. _panel.addView(model.getDisplayObject(), undefined, model);
  321. }
  322. if (model.name == "controls") {
  323. _controlsModel = model;
  324. }
  325. }
  326. }
  327. if (_controlsModel) {
  328. arrangeCanvasLogo();
  329. }
  330. }
  331. private function addScreenToPanel():void {
  332. // if controls visible and screen was not explicitly configured --> place screen on top of controls
  333. var screen:DisplayProperties = _pluginRegistry.getPlugin("screen") as DisplayProperties;
  334. screen.display = "none";
  335. screen.getDisplayObject().visible = false;
  336. _panel.addView(screen.getDisplayObject(), null, screen);
  337. }
  338. private function arrangeScreen():void {
  339. var screen:DisplayProperties = _pluginRegistry.getPlugin("screen") as DisplayProperties;
  340. if (_controlsModel && _controlsModel.visible) {
  341. if (isControlsAlwaysAutoHide() || (_controlsModel.position.bottom.px > 0)) {
  342. log.debug("controls is autoHide or it's in a non-default vertical position, configuring screen to take all available space");
  343. setScreenBottomAndHeight(screen, 100, 0);
  344. } else {
  345. var controlsHeight:Number = _controlsModel.getDisplayObject().height;
  346. var occupiedHeight:Number = screenTopOrBottomConfigured() ? getScreenTopOrBottomPx(screen) : controlsHeight;
  347. log.debug("occupied by controls or screen's configured bottom/top is " + occupiedHeight);
  348. var heightPct:Number = 0;
  349. if (screenTopOrBottomConfigured() && (screen.position.top.pct >= 0 || screen.position.bottom.pct >= 0)) {
  350. heightPct = 100 - Math.abs(50 - (screen.position.top.pct >= 0 ? screen.position.top.pct : screen.position.bottom.pct))*2;
  351. setScreenBottomAndHeight(screen, heightPct, controlsHeight);
  352. } else {
  353. heightPct = ((stage.stageHeight - occupiedHeight) / stage.stageHeight) * 100;
  354. setScreenBottomAndHeight(screen, heightPct, controlsHeight);
  355. }
  356. }
  357. }
  358. log.debug("arranging screen to pos " + screen.position);
  359. screen.display = "block";
  360. screen.getDisplayObject().visible = true;
  361. _pluginRegistry.updateDisplayProperties(screen);
  362. _panel.update(screen.getDisplayObject(), screen);
  363. _panel.draw(screen.getDisplayObject());
  364. }
  365. private function getScreenTopOrBottomPx(screen:DisplayProperties):Number {
  366. var screenConf:Object = _config.getObject("screen");
  367. if (screenConf.hasOwnProperty("top")) return screen.position.top.toPx(stage.stageHeight);
  368. if (screenConf.hasOwnProperty("bottom")) return screen.position.bottom.toPx(stage.stageHeight);
  369. return 0;
  370. }
  371. private function setScreenBottomAndHeight(screen:DisplayProperties, heightPct:Number, bottom:Number = 0):void {
  372. if (! screenTopOrBottomConfigured()) {
  373. log.debug("screen vertical pos not configured, setting bottom to value " + bottom);
  374. screen.bottom = bottom;
  375. } else {
  376. log.debug("using configured top/bottom for screen");
  377. }
  378. var heightConfigured:Boolean = _config.getObject("screen") && _config.getObject("screen").hasOwnProperty("height");
  379. if (! heightConfigured) {
  380. log.debug("screen height not configured, setting it to value " + heightPct + "%");
  381. screen.height = heightPct + "%";
  382. } else {
  383. log.debug("using configured height for screen");
  384. }
  385. }
  386. private function screenTopOrBottomConfigured():Boolean {
  387. var screen:Object = _config.getObject("screen");
  388. if (! screen) return false;
  389. if (screen.hasOwnProperty("top")) return true;
  390. if (screen.hasOwnProperty("bottom")) return true;
  391. return false;
  392. }
  393. private function isControlsAlwaysAutoHide():Boolean {
  394. if (!_controlsModel) return false;
  395. if (!_controlsModel.config) return false;
  396. log.debug("controlsModel.config.auotoHide == always", (_controlsModel.config.autoHide == 'always'));
  397. return _controlsModel.config.autoHide == 'always';
  398. }
  399. private function createFlowplayer(playListController:PlayListController):void {
  400. _flowplayer = new Flowplayer(stage, playListController, _pluginRegistry, _panel,
  401. _animationEngine, this, this, _config, _fullscreenManager, _pluginLoader, URLUtil.playerBaseUrl(loaderInfo));
  402. playListController.playerEventDispatcher = _flowplayer;
  403. _flowplayer.onBeforeFullscreen(onFullscreen);
  404. // _flowplayer.onFullscreenExit(onFullscreen);
  405. }
  406. private function onFullscreen(event:PlayerEvent):void {
  407. log.debug("entering fullscreen, disabling display clicks");
  408. _enteringFullscreen = true;
  409. var delay:Timer = new Timer(1000, 1);
  410. delay.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
  411. delay.start();
  412. }
  413. private function onTimerComplete(event:TimerEvent):void {
  414. log.debug("fullscreen wait delay complete, display clicks are enabled again");
  415. _enteringFullscreen = false;
  416. }
  417. private function createFlashVarsConfig():void {
  418. for (var prop:String in stage.loaderInfo.parameters) {
  419. log.debug(prop + ": " + (stage.loaderInfo.parameters[prop]));
  420. }
  421. if (! stage.loaderInfo.parameters) {
  422. return;
  423. }
  424. _config = ConfigLoader.flow_internal::parseConfig(stage.loaderInfo.parameters["config"], playerSwfName(), VersionInfo.controlsVersion, VersionInfo.audioVersion);
  425. }
  426. private function createPlayListController():PlayListController {
  427. if (! _providers) {
  428. _providers = new Dictionary();
  429. }
  430. var httpProvider:NetStreamControllingStreamProvider = new NetStreamControllingStreamProvider();
  431. httpProvider.playerConfig = _config;
  432. _providers["http"] = httpProvider;
  433. return new PlayListController(_config.getPlaylist(), _providers, _config, createNewLoader());
  434. }
  435. private function createScreen():void {
  436. _screen = new Screen(_config.getPlaylist(), _animationEngine, _playButtonOverlay, _pluginRegistry);
  437. var screenModel:DisplayProperties = _config.getScreenProperties();
  438. initView(_screen, screenModel, null, false);
  439. if (_playButtonOverlay) {
  440. PlayButtonOverlayView(_playButtonOverlay.getDisplayObject()).setScreen(_screen, hasClip);
  441. }
  442. // addViewLiteners(_screen);
  443. }
  444. private function createPlayButtonOverlay():void {
  445. _playButtonOverlay = _config.getPlayButtonOverlay();
  446. if (! _playButtonOverlay) return;
  447. _playButtonOverlay.onLoad(onPluginLoad);
  448. _playButtonOverlay.onError(onPluginLoadError);
  449. log.debug("playlist has clips? " + hasClip);
  450. var overlay:PlayButtonOverlayView = new PlayButtonOverlayView(! playButtonOverlayWidthDefined(), _playButtonOverlay, _pluginRegistry, _config.getPlaylist());
  451. initView(overlay, _playButtonOverlay, null, false);
  452. }
  453. private function playButtonOverlayWidthDefined():Boolean {
  454. if (! _config.getObject("play")) return false;
  455. return _config.getObject("play").hasOwnProperty("width");
  456. }
  457. private function get hasClip():Boolean {
  458. var firstClip:Clip = _config.getPlaylist().current;
  459. var hasClip:Boolean = ! firstClip.isNullClip && (firstClip.url || firstClip.provider != 'http');
  460. return hasClip;
  461. }
  462. private function createLogo():void {
  463. var logo:Logo = _config.getLogo() || new Logo();
  464. var logoView:LogoView = new LogoView(_panel, logo, _flowplayer);
  465. initView(logoView, logo, logoView.draw, false);
  466. }
  467. private function initView(view:DisplayObject, props:DisplayProperties, resizeListener:Function = null, addToPanel:Boolean = true):void {
  468. if (props.name != "logo" || VersionInfo.commercial) {
  469. _pluginRegistry.registerDisplayPlugin(props, view);
  470. }
  471. if (addToPanel) {
  472. _panel.addView(view, resizeListener, props);
  473. }
  474. if (props is Callable) {
  475. ExternalInterfaceHelper.initializeInterface(props as Callable, view);
  476. }
  477. }
  478. private function addListeners():void {
  479. _screen.addEventListener(MouseEvent.CLICK, onViewClicked);
  480. addEventListener(MouseEvent.ROLL_OVER, onMouseOver);
  481. addEventListener(MouseEvent.ROLL_OUT, onMouseOut);
  482. // add some color so that the ROLL_OVER/ROLL_OUT events are always triggered
  483. graphics.beginFill(0, 0);
  484. graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
  485. graphics.endFill();
  486. }
  487. private function onMouseOut(event:MouseEvent):void {
  488. _flowplayer.dispatchEvent(PlayerEvent.mouseOut());
  489. }
  490. private function onMouseOver(event:MouseEvent):void {
  491. _flowplayer.dispatchEvent(PlayerEvent.mouseOver());
  492. }
  493. private function createPanel():void {
  494. _panel = new Panel();
  495. addChild(_panel);
  496. }
  497. private function startStreams():void {
  498. var canStart:Boolean = true;
  499. if (_flowplayer.state != State.WAITING) {
  500. log.debug("streams have been started in player.onLoad(), will not start streams here.");
  501. canStart = false;
  502. }
  503. if (! hasClip) {
  504. log.info("Configuration has no clips to play.");
  505. canStart = false;
  506. }
  507. var playButton:PlayButtonOverlayView = _playButtonOverlay ? PlayButtonOverlayView(_playButtonOverlay.getDisplayObject()) : null;
  508. if (canStart) {
  509. if (_flowplayer.currentClip.autoPlay) {
  510. log.debug("clip is autoPlay");
  511. _flowplayer.play();
  512. } else if (_flowplayer.currentClip.autoBuffering) {
  513. log.debug("clip is autoBuffering");
  514. _flowplayer.startBuffering();
  515. } else {
  516. if (playButton) {
  517. playButton.stopBuffering();
  518. playButton.showButton();
  519. }
  520. }
  521. } else {
  522. // cannot start playing here, stop buffering indicator, don't show the button
  523. if (playButton) {
  524. playButton.stopBuffering();
  525. }
  526. }
  527. }
  528. private function addPlayListListeners():void {
  529. var playlist:Playlist = _config.getPlaylist();
  530. playlist.onError(onClipError);
  531. }
  532. private function onClipError(event:ClipEvent):void {
  533. doHandleError(event.info + ", " + event.info2 + ", " + event.info3 + ", clip: '" + Clip(event.target) + "'");
  534. }
  535. private function onViewClicked(event:MouseEvent):void {
  536. if (_enteringFullscreen) return;
  537. log.debug("onViewClicked, target " + event.target + ", current target " + event.currentTarget);
  538. if (_playButtonOverlay && isParent(DisplayObject(event.target), _playButtonOverlay.getDisplayObject())) {
  539. _flowplayer.toggle();
  540. return;
  541. }
  542. var clip:Clip = _flowplayer.playlist.current;
  543. if (clip.linkUrl) {
  544. _flowplayer.pause();
  545. navigateToURL(new URLRequest(clip.linkUrl), clip.linkWindow);
  546. return;
  547. }
  548. if (isParent(DisplayObject(event.target), _screen)) {
  549. _flowplayer.toggle();
  550. }
  551. }
  552. private function isParent(child:DisplayObject, parent:DisplayObject):Boolean {
  553. if (DisplayObject(child).parent == parent) return true;
  554. if (! (parent is DisplayObjectContainer)) return false;
  555. for (var i:Number = 0;i < DisplayObjectContainer(parent).numChildren; i++) {
  556. var curChild:DisplayObject = DisplayObjectContainer(parent).getChildAt(i);
  557. if (isParent(child, curChild)) {
  558. return true;
  559. }
  560. }
  561. return false;
  562. }
  563. private function onKeyDown(event:KeyboardEvent):void {
  564. log.debug("keydown");
  565. if (_enteringFullscreen) return;
  566. if (_flowplayer.dispatchBeforeEvent(PlayerEvent.keyPress(event.keyCode))) {
  567. _flowplayer.dispatchEvent(PlayerEvent.keyPress(event.keyCode));
  568. if (event.keyCode == Keyboard.SPACE) {
  569. _flowplayer.toggle();
  570. }
  571. }
  572. }
  573. override protected function onRedraw():void {
  574. if (bgImageHolder && getChildIndex(bgImageHolder) > getChildIndex(_panel)) {
  575. swapChildren(bgImageHolder, _panel);
  576. }
  577. }
  578. private function createLogoForCanvas():void {
  579. if (_canvasLogo) return;
  580. _copyrightNotice = LogoUtil.createCopyrightNotice(8);
  581. addChild(_copyrightNotice);
  582. _canvasLogo = new CanvasLogo();
  583. _canvasLogo.width = 85;
  584. _canvasLogo.scaleY = _canvasLogo.scaleX;
  585. _canvasLogo.alpha = .4;
  586. _canvasLogo.addEventListener(MouseEvent.CLICK,
  587. function(event:MouseEvent):void { navigateToURL(new URLRequest("http://flowplayer.org"), "_self"); });
  588. _canvasLogo.buttonMode = true;
  589. log.debug("adding logo to display list");
  590. addChild(_canvasLogo);
  591. onStageResize();
  592. }
  593. private function createNewLoader():ResourceLoader {
  594. return new ResourceLoaderImpl(_config.playerId ? null : URLUtil.playerBaseUrl(loaderInfo), this);
  595. }
  596. }
  597. }