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

Revision 396, 10.6 KB checked in by pablo, 4 years ago (diff)

Implemented clone() on all custom events.
Stretch media to config.stretching
PlayerReady from JavascriptAPI.as
config.file => config.playlist if file has .xml suffix
PluginConfig uses dynamic properties
V4ControlBar dispatches V5 view events
Media placed on stage!

Line 
1package com.longtailvideo.jwplayer.controller {
2        import com.longtailvideo.jwplayer.events.GlobalEventDispatcher;
3        import com.longtailvideo.jwplayer.events.MediaEvent;
4        import com.longtailvideo.jwplayer.events.PlayerEvent;
5        import com.longtailvideo.jwplayer.events.PlaylistEvent;
6        import com.longtailvideo.jwplayer.events.ViewEvent;
7        import com.longtailvideo.jwplayer.model.Model;
8        import com.longtailvideo.jwplayer.model.PlaylistItem;
9        import com.longtailvideo.jwplayer.player.Player;
10        import com.longtailvideo.jwplayer.player.PlayerState;
11        import com.longtailvideo.jwplayer.player.PlayerV4Emulation;
12        import com.longtailvideo.jwplayer.plugins.IPlugin;
13        import com.longtailvideo.jwplayer.utils.RootReference;
14        import com.longtailvideo.jwplayer.utils.Strings;
15        import com.longtailvideo.jwplayer.view.View;
16       
17        import flash.events.ErrorEvent;
18        import flash.events.Event;
19        import flash.net.URLRequest;
20        import flash.net.navigateToURL;
21
22        /**
23         * Sent when the player has been initialized and skins and plugins have been successfully loaded.
24         *
25         * @eventType com.longtailvideo.jwplayer.events.PlayerEvent.JWPLAYER_READY
26         */
27        [Event(name="jwplayerReady", type = "com.longtailvideo.jwplayer.events.PlayerEvent")]
28
29        /**
30         * Sent when the player has entered the ERROR state
31         *
32         * @eventType com.longtailvideo.jwplayer.events.PlayerEvent.JWPLAYER_ERROR
33         */
34        [Event(name="jwplayerError", type = "com.longtailvideo.jwplayer.events.PlayerEvent")]
35
36        /**
37         * The Controller is responsible for handling Model / View events and calling the appropriate responders
38         *
39         * @author Pablo Schklowsky
40         */
41        public class Controller extends GlobalEventDispatcher {
42
43                /** MVC References **/
44                private var _player:Player;
45                private var _model:Model;
46                private var _view:View;
47
48                /** Current blocking state **/
49                private var _blocking:Boolean = false;
50
51                /** File extensions of all supported mediatypes. **/
52                private var extensions:Object = {
53                                '3g2':'video',
54                                '3gp':'video',
55                                'aac':'video',
56                                'f4b':'video',
57                                'f4p':'video',
58                                'f4v':'video',
59                                'flv':'video',
60                                'gif':'image',
61                                'jpg':'image',
62                                'jpeg':'image',
63                                'm4a':'video',
64                                'm4v':'video',
65                                'mov':'video',
66                                'mp3':'sound',
67                                'mp4':'video',
68                                'png':'image',
69                                'rbs':'sound',
70                                'sdp':'video',
71                                'swf':'image',
72                                'vp6':'video'
73                        };
74
75                /** A list with legacy CDN classes that are now redirected to buit-in ones. **/
76                private var cdns:Object = {
77                                bitgravity:{'http.startparam':'starttime', provider:'http'},
78                                edgecast:{'http.startparam':'ec_seek', provider:'http'},
79                                flvseek:{'http.startparam':'fs', provider:'http'},
80                                highwinds:{'rtmp.loadbalance':true, provider:'rtmp'},
81                                lighttpd:{'http.startparam':'start', provider:'http'},
82                                vdox:{'rtmp.loadbalance':true, provider:'rtmp'}
83                        };
84
85                public function Controller(player:Player, model:Model, view:View) {
86                        _player = player;
87                        _model = model;
88                        _view = view;
89                }
90
91                /**
92                 * Begin player setup
93                 * @param readyConfig If a PlayerConfig object is already available, use it to configure the player.
94                 * Otherwise, load the config from XML / flashvars.
95                 */
96                public function setupPlayer():void {
97                        var setup:PlayerSetup = new PlayerSetup(_player, _model, _view);
98
99                        setup.addEventListener(Event.COMPLETE, setupComplete);
100                        setup.addEventListener(ErrorEvent.ERROR, errorHandler);
101
102                        addViewListeners();
103                        addModelListeners();
104
105                        setup.setupPlayer();
106                }
107
108                private function addViewListeners():void {
109                        _view.addEventListener(ViewEvent.JWPLAYER_VIEW_PLAY, playHandler);
110                        _view.addEventListener(ViewEvent.JWPLAYER_VIEW_STOP, stopHandler);
111                }
112
113                private function playHandler(evt:Event):void {
114                        if (_model.playlist.currentItem) {
115                                switch (_player.state) {
116                                        case PlayerState.IDLE:
117                                                load(_model.playlist.currentItem);
118                                                break;
119                                        case PlayerState.PAUSED:
120                                                _model.media.play();
121                                                break;
122                                }
123                        }
124                }
125
126                private function stopHandler(evt:ViewEvent):void {
127                        switch (_player.state) {
128                                case PlayerState.BUFFERING:
129                                case PlayerState.PLAYING:
130                                        _model.media.stop();
131                                        break;
132                        }
133                }
134
135                private function addModelListeners():void {
136                        _model.playlist.addEventListener(PlaylistEvent.JWPLAYER_PLAYLIST_LOADED, playlistLoadHandler);
137                        _model.playlist.addEventListener(ErrorEvent.ERROR, errorHandler);
138                        _model.playlist.addEventListener(PlaylistEvent.JWPLAYER_PLAYLIST_ITEM, playlistItemHandler);
139                }
140
141                private function setupComplete(evt:Event):void {
142                        dispatchEvent(new PlayerEvent(PlayerEvent.JWPLAYER_READY));
143                        RootReference.stage.dispatchEvent(new Event(Event.RESIZE));
144                        loadFirstItem();
145                }
146
147                private function playlistLoadHandler(evt:PlaylistEvent):void {
148                        // This stuff moved to playlist item handler
149                }
150
151                private function playlistItemHandler(evt:PlaylistEvent):void {
152                        var item:PlaylistItem = _model.playlist.currentItem;
153                        if (item.provider) {
154
155                                // Backwards compatibility for CDNs in the 'type' flashvar.
156                                if (cdns.hasOwnProperty(item.provider)) {
157                                        _model.config.setConfig(cdns[item.provider]);
158                                }
159
160                                // If the model doesn't have an instance of the provider, load & instantiate it
161                                if (!_model.hasMediaProvider(item.provider)) {
162                                        var mediaLoader:MediaProviderLoader = new MediaProviderLoader();
163                                        mediaLoader.addEventListener(Event.COMPLETE, mediaSourceLoaded);
164                                        mediaLoader.addEventListener(ErrorEvent.ERROR, errorHandler);
165                                        mediaLoader.loadSource(item.provider);
166                                        return;
167                                }
168                               
169                                _model.setActiveMediaProvider(item.provider);
170                        }
171
172                        if (_player.config.autostart) {
173                                load(_model.playlist.currentItem);
174                        }
175                }
176
177                private function mediaSourceLoaded(evt:Event):void {
178                        var loader:MediaProviderLoader = evt.target as MediaProviderLoader;
179                        _model.setMediaProvider(_model.playlist.currentItem.provider, loader.loadedSource);
180                        _model.setActiveMediaProvider(_model.playlist.currentItem.provider);
181                        load(_model.playlist.currentItem);
182                }
183
184                private function errorHandler(evt:ErrorEvent):void {
185                        errorState(evt.text);
186                }
187
188                private function errorState(message:String=""):void {
189                        dispatchEvent(new PlayerEvent(PlayerEvent.JWPLAYER_ERROR, message));
190                }
191
192                ////////////////////
193                // Public methods //
194                ////////////////////
195
196                public function get blocking():Boolean {
197                        return _blocking;
198                }
199
200                /**
201                 * @private
202                 * @copy com.longtailvideo.jwplayer.player.Player#blockPlayback
203                 */
204                public function blockPlayback(plugin:IPlugin):Boolean {
205                        if (!_blocking) {
206                                _blocking = true;
207                                return true;
208                        } else {
209                                return false;
210                        }
211                }
212
213                /**
214                 * @private
215                 * @copy com.longtailvideo.jwplayer.player.Player#unblockPlayback
216                 */
217                public function unblockPlayback(target:IPlugin):Boolean {
218                        if (_blocking) {
219                                _blocking = false;
220                                return true;
221                        } else {
222                                return false;
223                        }
224                }
225
226                public function setVolume(vol:Number):Boolean {
227                        if (_model.media) {
228                                _model.config.volume = vol;
229                                _model.media.setVolume(vol);
230                                return true;
231                        } else {
232                                return false;
233                        }
234                }
235
236                public function mute(muted:Boolean):Boolean {
237                        if (muted && !_model.mute) {
238                                _model.mute = true;
239                                _model.media.mute(true);
240                                return true;
241                        } else if (!muted && _model.mute) {
242                                _model.mute = false;
243                                _model.media.mute(false);
244                                return true;
245                        }
246
247                        return false;
248                }
249
250                public function play():Boolean {
251                        if (!_model.media)
252                                return false;
253
254                        switch (_model.media.state) {
255                                case PlayerState.PLAYING:
256                                case PlayerState.BUFFERING:
257                                        return false;
258                                        break;
259                                default:
260                                        _model.media.play();
261                                        break;
262                        }
263
264                        return true;
265                }
266
267                public function pause():Boolean {
268                        if (!_model.media)
269                                return false;
270
271                        switch (_model.media.state) {
272                                case PlayerState.PLAYING:
273                                case PlayerState.BUFFERING:
274                                        _model.media.pause();
275                                        return true;
276                                        break;
277                        }
278
279                        return false;
280                }
281
282                public function stop():Boolean {
283                        if (!_model.media)
284                                return false;
285
286                        switch (_model.media.state) {
287                                case PlayerState.PLAYING:
288                                case PlayerState.BUFFERING:
289                                case PlayerState.PAUSED:
290                                        _model.media.stop();
291                                        return true;
292                                        break;
293                        }
294
295                        return false;
296                }
297
298                public function seek(pos:Number):Boolean {
299                        if (!_model.media)
300                                return false;
301
302                        switch (_model.media.state) {
303                                case PlayerState.PLAYING:
304                                case PlayerState.BUFFERING:
305                                case PlayerState.PAUSED:
306                                        _model.media.seek(pos);
307                                        return true;
308                                        break;
309                        }
310
311                        return false;
312                }
313
314                public function load(item:*):Boolean {
315                        //if (!_model.media) return false;
316
317                        if (item is PlaylistItem) {
318                                return loadPlaylistItem(item as PlaylistItem);
319                        } else if (item is String) {
320                                return loadString(item as String);
321                        } else if (item is Number) {
322                                return loadNumber(item as Number);
323                        } else if (item is Object) {
324                                return loadObject(item as Object);
325                        }
326                        return false;
327                }
328
329                private function loadPlaylistItem(item:PlaylistItem):Boolean {
330                        _model.setActiveMediaProvider(item.provider);
331                        _model.media.addEventListener(MediaEvent.JWPLAYER_MEDIA_BUFFER_FULL, lockHandler);
332                        _model.media.load(item);
333                        return true;
334                }
335
336                private function loadString(item:String):Boolean {
337                        var ext:String = Strings.extension(item);
338                        if (extensions.hasOwnProperty(ext)) {
339                                var type:String = extensions[ext];
340                                _model.setActiveMediaProvider(type);
341                                _model.media.addEventListener(MediaEvent.JWPLAYER_MEDIA_BUFFER_FULL, lockHandler);
342                                _model.media.load(new PlaylistItem({file:item}));
343                        } else {
344                                _model.playlist.load(item);
345                        }
346                        return false;
347                }
348
349                private function loadNumber(item:Number):Boolean {
350                        if (item >= 0 && item < _model.playlist.length) {
351                                _model.media.addEventListener(MediaEvent.JWPLAYER_MEDIA_BUFFER_FULL, lockHandler);
352                                _model.media.load(_model.playlist.getItemAt(item));
353                                return true;
354                        }
355                        return false;
356                }
357
358                private function loadObject(item:Object):Boolean {
359                        if (Object(item).hasOwnProperty('file')) {
360                                _model.media.addEventListener(MediaEvent.JWPLAYER_MEDIA_BUFFER_FULL, lockHandler);
361                                _model.media.load(new PlaylistItem(item));
362                                return true;
363                        }
364                        return false;
365                }
366
367                private function lockHandler(evt:MediaEvent):void {
368                        _model.media.play();
369                }
370
371                public function redraw():Boolean {
372                        _view.redraw();
373                        PlayerV4Emulation.getInstance().resize(_model.config.width, _model.config.height);
374                        return true;
375                }
376
377                public function fullscreen(mode:Boolean):Boolean {
378                        _view.fullscreen(mode);
379                        return true;
380                }
381
382                public function link(playlistIndex:Number=NaN):Boolean {
383                        if (isNaN(playlistIndex))
384                                playlistIndex = _model.playlist.currentIndex;
385
386                        if (playlistIndex >= 0 && playlistIndex < _model.playlist.length) {
387                                navigateToURL(new URLRequest(_model.playlist.getItemAt(playlistIndex).link), _model.config.linktarget);
388                                return true;
389                        }
390
391                        return false;
392                }
393               
394                private function loadFirstItem():void {
395//                      if (_model.playlist.currentItem) {
396//                              load(_model.playlist.currentItem);
397//                      }       
398                }
399               
400
401        }
402}
Note: See TracBrowser for help on using the repository browser.