PageRenderTime 21ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/flowplayer/tags/flow_3_1_3/src/actionscript/org/flowplayer/view/Preloader.as

http://flowplayer-core.googlecode.com/
ActionScript | 190 lines | 148 code | 19 blank | 23 comment | 25 complexity | 96c7723d57a92decc5c345155e7322cc 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 flash.display.DisplayObject;
  21. import flash.display.MovieClip;
  22. import flash.display.StageAlign;
  23. import flash.display.StageScaleMode;
  24. import flash.events.Event;
  25. import flash.events.ProgressEvent;
  26. import flash.events.TimerEvent;
  27. import flash.utils.Timer;
  28. import flash.utils.getDefinitionByName;
  29. import org.flowplayer.util.Arrange;
  30. import org.flowplayer.util.Log;
  31. import org.flowplayer.util.LogConfiguration;
  32. public class Preloader extends MovieClip {
  33. private var _log:Log = new Log(this);
  34. private var _app:DisplayObject;
  35. private var _initTimer:Timer;
  36. private var _stageTimer:Timer;
  37. private var _rotation:RotatingAnimation;
  38. // this variable can be set from external SWF files, if it's set well use it to construct the config
  39. public var injectedConfig:String;
  40. private var _ready:Boolean = false;
  41. public function Preloader() {
  42. var logConfig:LogConfiguration = new LogConfiguration();
  43. logConfig.level = "error";
  44. logConfig.filter = "org.flowplayer.view.Preloader";
  45. Log.configure(logConfig);
  46. _log.debug("Preloader") ;
  47. stop();
  48. addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
  49. }
  50. private function onAddedToStage(event:Event):void {
  51. log("onAddedToStage(): stage size " + stage.width + " x " + stage.height);
  52. if (rotationEnabled) {
  53. _rotation = new RotatingAnimation();
  54. addChild(_rotation);
  55. arrange();
  56. _rotation.start();
  57. }
  58. addEventListener(Event.RESIZE, arrange);
  59. loaderInfo.addEventListener(ProgressEvent.PROGRESS, onLoadProgress);
  60. // loaderInfo.addEventListener(Event.COMPLETE, init);
  61. // prepare stage if the app (Launcher) has been greated already. this is the
  62. // case when we are embedded in another SWF
  63. if (_app) {
  64. prepareStage();
  65. }
  66. }
  67. private function startStageWait():void {
  68. if (_stageTimer) return;
  69. _stageTimer = new Timer(200);
  70. _stageTimer.addEventListener(TimerEvent.TIMER, onStageWait);
  71. _stageTimer.start();
  72. }
  73. private function onStageWait(event:TimerEvent):void {
  74. if (stage.stageWidth == 0 || stage.stageHeight == 0) {
  75. log("stage dimensions " + stage.stageWidth + "x" + stage.stageHeight);
  76. return;
  77. }
  78. log("stage has nonzero size " + stage.stageWidth + "x" + stage.stageHeight);
  79. if (_ready) {
  80. _stageTimer.stop();
  81. init();
  82. } else {
  83. _ready = true;
  84. }
  85. }
  86. private function onLoadProgress(event:ProgressEvent):void {
  87. var percent:Number = Math.floor((event.bytesLoaded*100) / event.bytesTotal);
  88. log(percent);
  89. if (_rotation) {
  90. Arrange.center(_rotation, stage.stageWidth, stage.stageHeight);
  91. }
  92. if (percent < 100) {
  93. return;
  94. }
  95. prepareStage();
  96. if (stageHasSize()) {
  97. log("onLoadProgress() calling init()");
  98. init();
  99. } else {
  100. log("onLoadProgress() starting stage wait");
  101. startStageWait();
  102. }
  103. }
  104. private function init(event:Event = null):void {
  105. log("init()");
  106. if (_initTimer) {
  107. _initTimer.stop();
  108. }
  109. if (_stageTimer) {
  110. _stageTimer.stop();
  111. }
  112. nextFrame();
  113. // prepareStage();
  114. if (! stageHasSize()) {
  115. log("init(), stage does not have size yet, starting wait timer");
  116. startStageWait();
  117. return;
  118. } else {
  119. log("stage has size " + stage.stageWidth + " x " + stage.stageHeight);
  120. }
  121. if (_app) {
  122. log("init(), _app already instantiated returning");
  123. return;
  124. }
  125. if (_rotation) {
  126. _rotation.stop();
  127. if (_rotation.parent) {
  128. removeChild(_rotation);
  129. }
  130. }
  131. try {
  132. var mainClass:Class = Class(getDefinitionByName("org.flowplayer.view.Launcher"));
  133. _app = new mainClass() as DisplayObject;
  134. addChild(_app as DisplayObject);
  135. log("Launcher instantiated " + _app);
  136. } catch (e:Error) {
  137. log("error instantiating Launcher " + e + ": " + e.message);
  138. _app = null;
  139. if (! _initTimer) {
  140. log("starting init timer");
  141. prevFrame();
  142. _initTimer = new Timer(300);
  143. _initTimer.addEventListener(TimerEvent.TIMER, function(e:TimerEvent):void { init(); });
  144. }
  145. _initTimer.start();
  146. }
  147. }
  148. private function prepareStage():void {
  149. if (! stage) return;
  150. stage.align = StageAlign.TOP_LEFT;
  151. stage.scaleMode = StageScaleMode.NO_SCALE;
  152. }
  153. private function log(msg:Object):void {
  154. _log.debug(msg + "");
  155. trace(msg + "");
  156. }
  157. private function stageHasSize():Boolean {
  158. return stage.stageWidth > 0 && stage.stageHeight > 0
  159. }
  160. private function get rotationEnabled():Boolean {
  161. var config:Object = stage.loaderInfo.parameters["config"];
  162. if (! config) return true;
  163. if (config.replace(/\s/g, "").indexOf("buffering:null") > 0) return false;
  164. return true;
  165. }
  166. private function arrange(event:Event = null):void {
  167. _rotation.setSize(stage.height * 0.22, stage.width * 0.22);
  168. Arrange.center(_rotation, stage.width, stage.stage.height - 28);
  169. }
  170. }
  171. }