source: trunk/fl5/src/com/longtailvideo/jwplayer/controller/Controller.as @ 343

Revision 343, 7.7 KB checked in by pablo, 4 years ago (diff)

Plugin initialization

Line 
1package com.longtailvideo.jwplayer.controller {
2        import com.longtailvideo.jwplayer.events.GlobalEventDispatcher;
3        import com.longtailvideo.jwplayer.events.PlayerEvent;
4        import com.longtailvideo.jwplayer.events.PlaylistEvent;
5        import com.longtailvideo.jwplayer.media.MediaState;
6        import com.longtailvideo.jwplayer.model.Model;
7        import com.longtailvideo.jwplayer.model.PlaylistItem;
8        import com.longtailvideo.jwplayer.player.Player;
9        import com.longtailvideo.jwplayer.plugins.IPlugin;
10        import com.longtailvideo.jwplayer.utils.RootReference;
11        import com.longtailvideo.jwplayer.utils.Strings;
12        import com.longtailvideo.jwplayer.view.View;
13       
14        import flash.events.ErrorEvent;
15        import flash.events.Event;
16        import flash.net.URLRequest;
17        import flash.net.navigateToURL;
18
19        /**
20         * Sent when the player has been initialized and skins and plugins have been successfully loaded.
21         *
22         * @eventType com.longtailvideo.jwplayer.events.PlayerEvent.JWPLAYER_READY
23         */
24        [Event(name="jwplayerReady", type = "com.longtailvideo.jwplayer.events.PlayerEvent")]
25
26        /**
27         * Sent when the player has entered the ERROR state
28         *
29         * @eventType com.longtailvideo.jwplayer.events.PlayerEvent.JWPLAYER_ERROR
30         */
31        [Event(name="jwplayerError", type = "com.longtailvideo.jwplayer.events.PlayerEvent")]
32
33        /**
34         * The Controller is responsible for handling Model / View events and calling the appropriate responders
35         *
36         * @author Pablo Schklowsky
37         */
38        public class Controller extends GlobalEventDispatcher {
39
40                /** MVC References **/         
41                private var _player:Player;
42                private var _model:Model;
43                private var _view:View;
44
45                /** Current blocking state **/
46                private var _blocking:Boolean = false;
47               
48                /** File extensions of all supported mediatypes. **/
49                private var EXTENSIONS:Object = {
50                        '3g2':'video',
51                        '3gp':'video',
52                        'aac':'video',
53                        'f4b':'video',
54                        'f4p':'video',
55                        'f4v':'video',
56                        'flv':'video',
57                        'gif':'image',
58                        'jpg':'image',
59                        'jpeg':'image',
60                        'm4a':'video',
61                        'm4v':'video',
62                        'mov':'video',
63                        'mp3':'sound',
64                        'mp4':'video',
65                        'png':'image',
66                        'rbs':'sound',
67                        'sdp':'video',
68                        'swf':'image',
69                        'vp6':'video'
70                };
71               
72
73                public function Controller(player:Player, model:Model, view:View) {
74                        var rootRef:RootReference = new RootReference(player);
75
76                        _player = player;
77                        _model = model;
78                        _view = view;
79                       
80                }
81               
82                /**
83                 * Begin player setup
84                 * @param readyConfig If a PlayerConfig object is already available, use it to configure the player.
85                 * Otherwise, load the config from XML / flashvars.
86                 */
87                public function setupPlayer():void {
88                        var setup:PlayerSetup = new PlayerSetup(_player, _model, _view);
89
90                        setup.addEventListener(Event.COMPLETE, setupComplete);
91                        setup.addEventListener(ErrorEvent.ERROR, errorHandler);
92                       
93                        addViewListeners();
94                        addModelListeners();
95                       
96                        setup.setupPlayer();
97                }
98
99                private function addViewListeners():void {
100                       
101                }
102               
103                private function addModelListeners():void {
104                        _model.playlist.addEventListener(PlaylistEvent.JWPLAYER_PLAYLIST_LOADED, playlistLoadHandler);
105                        _model.playlist.addEventListener(ErrorEvent.ERROR, errorHandler);
106                }
107               
108                private function setupComplete(evt:Event):void {
109                        trace("Setup complete");
110                }
111               
112                private function playlistLoadHandler(evt:PlaylistEvent):void {
113                        for (var i:Number = 0; i < _model.playlist.length; i++) {
114                                if (!_model.hasMediaSource(_model.playlist.getItemAt(i).type)) {
115//                                      load the external media source                                 
116//                                      loadMediaSource(type);
117                                }
118                        }
119                }
120               
121                private function errorHandler(evt:ErrorEvent):void {
122                        errorState(evt.text);
123                }
124               
125                private function errorState(message:String=""):void {
126                        dispatchEvent(new PlayerEvent(PlayerEvent.JWPLAYER_ERROR, message));
127                }
128
129                ////////////////////
130                // Public methods //
131                ////////////////////
132
133                public function get blocking():Boolean {
134                        return _blocking;
135                }
136
137                /**
138                 * @private
139                 * @copy com.longtailvideo.jwplayer.player.Player#blockPlayback
140                 */
141                public function blockPlayback(plugin:IPlugin):Boolean {
142                        if (!_blocking) {
143                                _blocking = true;
144                                return true;
145                        } else {
146                                return false;
147                        }
148                }
149
150                /**
151                 * @private
152                 * @copy com.longtailvideo.jwplayer.player.Player#unblockPlayback
153                 */
154                public function unblockPlayback(target:IPlugin):Boolean {
155                        if (_blocking) {
156                                _blocking = false;
157                                return true;
158                        } else {
159                                return false;
160                        }
161                }
162               
163                public function setVolume(vol:Number):Boolean {
164                        if (_model.media) {
165                                _model.config.volume = vol;
166                                _model.media.setVolume(vol);
167                                return true;
168                        } else {
169                                return false;
170                        }
171                }
172               
173                public function mute(muted:Boolean):Boolean {
174                        if (muted && !_model.mute) {
175                                _model.mute = true;
176                                _model.media.setVolume(0);
177                                return true;
178                        } else if (!muted && _model.mute) {
179                                _model.mute = false;
180                                _model.media.setVolume(_model.config.volume);
181                                return true;
182                        }
183                       
184                        return false;
185                }
186
187                public function play():Boolean {
188                        if (!_model.media) return false;
189                       
190                        switch (_model.media.state) {
191                                case MediaState.PLAYING:
192                                case MediaState.BUFFERING:
193                                        return false;
194                                        break;
195                                default:
196                                        _model.media.play();
197                                        break;
198                        }
199                       
200                        return true;
201                }
202
203                public function pause():Boolean {
204                        if (!_model.media) return false;
205                       
206                        switch (_model.media.state) {
207                                case MediaState.PLAYING:
208                                case MediaState.BUFFERING:
209                                        _model.media.pause();
210                                        return true;
211                                        break;
212                        }
213                       
214                        return false;
215                }
216
217                public function stop():Boolean {
218                        if (!_model.media) return false;
219                       
220                        switch (_model.media.state) {
221                                case MediaState.PLAYING:
222                                case MediaState.BUFFERING:
223                                case MediaState.PAUSED:
224                                        _model.media.stop();
225                                        return true;
226                                        break;
227                        }
228                       
229                        return false;
230                }
231
232                public function seek(pos:Number):Boolean {
233                        if (!_model.media) return false;
234                       
235                        switch (_model.media.state) {
236                                case MediaState.PLAYING:
237                                case MediaState.BUFFERING:
238                                case MediaState.PAUSED:
239                                        _model.media.seek(pos);
240                                        return true;
241                                        break;
242                        }
243                       
244                        return false;
245                }
246               
247                public function load(item:*):Boolean {
248                        //if (!_model.media) return false;
249                       
250                        if (item is PlaylistItem) {
251                                return loadPlaylistItem(item as PlaylistItem);
252                        } else if (item is String) {
253                                return loadString(item as String);
254                        } else if (item is Number) {
255                                return loadNumber(item as Number);
256                        } else if (item is Object) {
257                                return loadObject(item as Object);
258                        }
259                        return false;
260                }
261
262                private function loadPlaylistItem(item:PlaylistItem):Boolean {
263                        _model.setActiveMediaSource(item.type);
264                        _model.media.load(item);
265                        return true;
266                }
267
268                private function loadString(item:String):Boolean {
269                        var ext:String = Strings.extension(item);
270                        if (EXTENSIONS.hasOwnProperty(ext)) {
271                                var type:String = EXTENSIONS[ext];
272                                _model.setActiveMediaSource(type);
273                                _model.media.load(new PlaylistItem({file:item}));
274                        } else {
275                                _model.playlist.load(item);
276                        }
277                        return false;
278                }
279               
280                private function loadNumber(item:Number):Boolean {
281                        if (item >= 0 && item < _model.playlist.length) {
282                                _model.media.load(_model.playlist.getItemAt(item));
283                                return true;
284                        }       
285                        return false;
286                }
287               
288                private function loadObject(item:Object):Boolean {
289                        if (Object(item).hasOwnProperty('file')) {
290                                _model.media.load(new PlaylistItem(item));
291                                return true;
292                        }
293                        return false;
294                }
295               
296                public function redraw():Boolean {
297                        _view.redraw();
298                        return true;
299                }
300
301                public function fullscreen(mode:Boolean):Boolean {
302                        _view.fullscreen(mode);
303                        return true;
304                }
305               
306                public function link(playlistIndex:Number=NaN):Boolean {
307                        if (isNaN(playlistIndex)) playlistIndex = _model.playlist.currentIndex;
308                       
309                        if (playlistIndex >= 0 && playlistIndex < _model.playlist.length) {
310                                navigateToURL(new URLRequest(_model.playlist.getItemAt(playlistIndex).link), _model.config.linktarget);
311                                return true;
312                        }
313                       
314                        return false;
315                }
316               
317        }
318}
Note: See TracBrowser for help on using the repository browser.