source: trunk/fl5/src/com/longtailvideo/jwplayer/controller/PlayerSetup.as @ 629

Revision 629, 7.5 KB checked in by pablo, 4 years ago (diff)

Plugins are now on the stage (but invisible) when initPlugin is called.

Line 
1package com.longtailvideo.jwplayer.controller {
2        import com.jeroenwijering.events.PluginInterface;
3        import com.longtailvideo.jwplayer.events.PlayerEvent;
4        import com.longtailvideo.jwplayer.events.PlaylistEvent;
5        import com.longtailvideo.jwplayer.model.Model;
6        import com.longtailvideo.jwplayer.player.IPlayer;
7        import com.longtailvideo.jwplayer.plugins.IPlugin;
8        import com.longtailvideo.jwplayer.plugins.PluginConfig;
9        import com.longtailvideo.jwplayer.plugins.V4Plugin;
10        import com.longtailvideo.jwplayer.utils.Configger;
11        import com.longtailvideo.jwplayer.utils.Logger;
12        import com.longtailvideo.jwplayer.utils.RootReference;
13        import com.longtailvideo.jwplayer.utils.Strings;
14        import com.longtailvideo.jwplayer.view.View;
15        import com.longtailvideo.jwplayer.view.interfaces.ISkin;
16        import com.longtailvideo.jwplayer.view.skins.DefaultSkin;
17        import com.longtailvideo.jwplayer.view.skins.PNGSkin;
18        import com.longtailvideo.jwplayer.view.skins.SWFSkin;
19        import com.longtailvideo.jwplayer.view.skins.SkinProperties;
20       
21        import flash.display.DisplayObject;
22        import flash.events.ErrorEvent;
23        import flash.events.Event;
24        import flash.events.EventDispatcher;
25        import flash.events.TimerEvent;
26        import flash.utils.Timer;
27
28        /**
29         * Sent when the all of the setup steps have successfully completed.
30         *
31         * @eventType flash.events.Event.COMPLETE
32         */
33        [Event(name="complete", type = "flash.events.Event")]
34
35        /**
36         * Sent when an error occurred during player setup
37         *
38         * @eventType flash.events.ErrorEvent.ERROR
39         */
40        [Event(name="error", type = "flash.events.ErrorEvent")]
41
42
43        /**
44         * PlayerSetup is a helper class to Controller.  It manages the initial player startup process, firing an
45         * Event.COMPLETE event when finished, or an ErrorEvent.ERROR if a problem occurred during setup.
46         *
47         * @see Controller
48         * @author Pablo Schklowsky
49         */
50        public class PlayerSetup extends EventDispatcher {
51
52                /** MVC references **/
53                private var _player:IPlayer;
54                private var _model:Model;
55                private var _view:View;
56               
57                /** TaskQueue **/
58                private var tasker:TaskQueue;
59               
60                /** User-defined configuration **/
61                private var confHash:Object;
62               
63                public function PlayerSetup(player:IPlayer, model:Model, view:View) {
64                        _player = player;
65                        _model = model;
66                        _view = view;
67                }
68               
69                public function setupPlayer():void {
70                        tasker = new TaskQueue(false);
71                        tasker.addEventListener(Event.COMPLETE, setupTasksComplete);
72                        tasker.addEventListener(ErrorEvent.ERROR, setupTasksFailed);
73                       
74                        tasker.queueTask(insertDelay);
75                        tasker.queueTask(loadConfig, loadConfigComplete);
76                        tasker.queueTask(loadSkin, loadSkinComplete);
77                        tasker.queueTask(setupMediaProviders);
78                        tasker.queueTask(setupView);
79                        tasker.queueTask(loadPlugins, loadPluginsComplete);
80                        tasker.queueTask(loadPlaylist, loadPlaylistComplete);
81                        tasker.queueTask(initPlugins);
82                        tasker.queueTask(setupJS);
83                       
84                        tasker.runTasks();
85                }
86               
87                private function setupTasksComplete(evt:Event):void {
88                        complete();
89                }
90               
91                private function setupTasksFailed(evt:ErrorEvent):void {
92                        error(evt.text);
93                }
94
95                private function complete():void {
96                        dispatchEvent(new Event(Event.COMPLETE));
97                }
98               
99                private function error(message:String):void {
100                        dispatchEvent(new ErrorEvent(ErrorEvent.ERROR, false, false, message));
101                }
102               
103                ///////////////////////
104                // Tasks
105                ///////////////////////
106               
107                private function insertDelay():void {
108                        var timer:Timer = new Timer(100, 1);
109                        timer.addEventListener(TimerEvent.TIMER_COMPLETE, tasker.success);
110                        timer.start();
111                }
112
113                private function loadConfig():void {
114                        var configger:Configger = new Configger();
115                        configger.addEventListener(Event.COMPLETE, tasker.success);
116                        configger.addEventListener(ErrorEvent.ERROR, tasker.failure);
117
118                        try {
119                                configger.loadConfig();
120                        } catch (e:Error) {
121                                error(e.message);
122                        }
123                }
124
125                private function loadConfigComplete(evt:Event):void {
126                        confHash = (evt.target as Configger).config;
127                }
128
129                private function loadSkin():void {
130                        var skin:ISkin;
131                        if (confHash && confHash['skin']) {
132                                if (Strings.extension(confHash['skin']) == "swf") {
133                                        skin = new SWFSkin();
134                                } else {
135                                        skin = new PNGSkin();
136                                }
137                        } else {
138                                skin = new DefaultSkin();
139                        }
140                        skin.addEventListener(Event.COMPLETE, tasker.success);
141                        skin.addEventListener(ErrorEvent.ERROR, tasker.failure);
142                        skin.load(confHash['skin']);
143                }
144               
145                private function loadSkinComplete(event:Event=null):void {
146                        if (event) {
147                                var skin:ISkin = event.target as ISkin;
148                                skin.removeEventListener(Event.COMPLETE, tasker.success);
149                                skin.removeEventListener(ErrorEvent.ERROR, tasker.failure);
150
151                                var props:SkinProperties = skin.getSkinProperties();
152                                _model.config.setConfig(props);
153                                _model.config.setConfig(confHash);
154                                _view.skin = skin;
155                        } else {
156                                _model.config.setConfig(confHash);
157                        }
158                        Logger.setConfig(_model.config);
159                }
160
161                private function setupMediaProviders():void {
162                        _model.setupMediaProviders();
163                        tasker.success();
164                }
165               
166                private function setupView():void {
167                        try {
168                                _view.setupView();
169                                tasker.success();
170                        } catch (e:Error) {
171                                tasker.failure(new ErrorEvent(ErrorEvent.ERROR, false, false, "View setup failed: " + e.message));
172                        }
173                }
174
175                private function loadPlugins():void {
176                        if (_model.config.plugins) {
177                                var loader:PluginLoader = new PluginLoader();
178                                loader.addEventListener(Event.COMPLETE, tasker.success);
179                                loader.addEventListener(ErrorEvent.ERROR, tasker.failure);
180                                loader.loadPlugins(_model.config.plugins);
181                        } else {
182                                tasker.success();
183                        }
184                }
185               
186                private function loadPluginsComplete(event:Event=null):void {
187                        if (event) {
188                                var loader:PluginLoader = event.target as PluginLoader;
189
190                                for (var pluginId:String in loader.plugins) {
191                                        var plugin:DisplayObject = loader.plugins[pluginId] as DisplayObject;
192                                        if (plugin is IPlugin) {
193                                                _view.addPlugin(pluginId, plugin as IPlugin);
194                                        } else if (plugin is PluginInterface) {
195                                                if ( (plugin as Object).hasOwnProperty('config') ) {
196                                                        var loadedConf:Object = (plugin as Object).config;
197                                                        var pluginConf:PluginConfig = _model.config.pluginConfig(pluginId);
198                                                        for (var i:String in loadedConf) {
199                                                                if (!pluginConf.hasOwnProperty(i)) pluginConf[i] = loadedConf[i];
200                                                        }
201                                                        pluginConf['width'] = _player.controls.display.width;
202                                                        pluginConf['height'] = _player.controls.display.height;
203                                                        pluginConf['visible'] = true;
204                                                }
205                                                _view.addPlugin(pluginId, new V4Plugin(plugin as PluginInterface, pluginId));
206                                        }
207                                }
208                        }
209                }
210
211                private function loadPlaylist():void {
212                        _model.playlist.addEventListener(PlaylistEvent.JWPLAYER_PLAYLIST_LOADED, tasker.success);
213                        _model.playlist.addEventListener(PlayerEvent.JWPLAYER_ERROR, tasker.failure);
214
215                        if (_model.config.playlistfile) {
216                                _model.playlist.load(_model.config.playlistfile);
217                        } else if (_model.config.singleItem.file) {
218                                _model.playlist.load(_model.config.singleItem);
219                        } else {
220                                tasker.success();
221                        }
222                }
223
224                private function loadPlaylistComplete(event:Event=null):void {
225                        _model.playlist.removeEventListener(PlaylistEvent.JWPLAYER_PLAYLIST_LOADED, tasker.success);
226                        _model.playlist.removeEventListener(PlayerEvent.JWPLAYER_ERROR, tasker.failure);
227                }
228
229                private function initPlugins():void {
230                        for each (var pluginId:String in _view.loadedPlugins()) {
231                                try {
232                                        var plugin:IPlugin = _view.getPlugin(pluginId);
233                                        plugin.initPlugin(_player, _model.config.pluginConfig(pluginId));
234                                } catch (e:Error) {
235                                        Logger.log("Error initializing plugin: " + e.message);
236                                        if (plugin) {
237                                                _view.removePlugin(plugin);
238                                        }
239                                }
240                        }
241                        tasker.success();
242                }
243
244                private function setupJS():void {
245                        tasker.success();
246                }
247
248        }
249}
Note: See TracBrowser for help on using the repository browser.