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

Revision 395, 9.6 KB checked in by zach, 4 years ago (diff)

Adding Controlbar V4 compatibility Mode

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