source: branches/4.2/com/jeroenwijering/player/Controller.as @ 78

Revision 78, 9.6 KB checked in by jeroen, 5 years ago (diff)

fixed fullscreen and playlist glitch

  • Property svn:executable set to *
Line 
1/**
2* Process all input from the views and modifies the model.
3**/
4package com.jeroenwijering.player {
5
6
7import com.jeroenwijering.events.*;
8import com.jeroenwijering.player.*;
9import com.jeroenwijering.utils.*;
10import flash.display.MovieClip;
11import flash.events.*;
12import flash.geom.Rectangle;
13import flash.net.*;
14import flash.system.Capabilities;
15
16
17public class Controller extends EventDispatcher {
18
19
20        /** Configuration object **/
21        private var config:Object;
22        /** Reference to the skin; for stage event subscription. **/
23        private var skin:MovieClip;
24        /** Object that manages the playlist. **/
25        private var playlister:Playlister;
26        /** Reference to the player's model. **/
27        private var model:Model;
28        /** Reference to the player's view. **/
29        private var view:View;
30        /** object that provides randomization. **/
31        private var randomizer:Randomizer;
32
33
34        /** Constructor, set up stage and playlist listeners. **/
35        public function Controller(cfg:Object,skn:MovieClip):void {
36                config = cfg;
37                skin = skn;
38                playlister = new Playlister();
39                playlister.addEventListener(Event.COMPLETE,playlistHandler);
40                playlister.addEventListener(ErrorEvent.ERROR,errorHandler);
41                config['controlbarsize'] = skin.controlbar.height;
42                redrawHandler();
43        };
44
45
46        /** Register view and model with controller, start loading playlist. **/
47        public function start(mdl:Model,vie:View):void {
48                model= mdl;
49                model.addEventListener(ModelEvent.META,metaHandler);
50                model.addEventListener(ModelEvent.TIME,metaHandler);
51                model.addEventListener(ModelEvent.STATE,stateHandler);
52                view = vie;
53                view.addEventListener(ViewEvent.FULLSCREEN,fullscreenHandler);
54                view.addEventListener(ViewEvent.ITEM,itemHandler);
55                view.addEventListener(ViewEvent.LINK,linkHandler);
56                view.addEventListener(ViewEvent.LOAD,loadHandler);
57                view.addEventListener(ViewEvent.MUTE,muteHandler);
58                view.addEventListener(ViewEvent.NEXT,nextHandler);
59                view.addEventListener(ViewEvent.PLAY,playHandler);
60                view.addEventListener(ViewEvent.PREV,prevHandler);
61                view.addEventListener(ViewEvent.QUALITY,qualityHandler);
62                view.addEventListener(ViewEvent.REDRAW,redrawHandler);
63                view.addEventListener(ViewEvent.SEEK,seekHandler);
64                view.addEventListener(ViewEvent.STOP,stopHandler);
65                view.addEventListener(ViewEvent.VOLUME,volumeHandler);
66                if(config['file']) { playlister.load(config); }
67        };
68
69
70
71        /** Catch errors dispatched by the playlister. **/
72        private function errorHandler(evt:ErrorEvent):void {
73                dispatchEvent(new ControllerEvent(ControllerEvent.ERROR,{message:evt.text}));
74        };
75
76
77        /** Switch fullscreen state. **/
78        private function fullscreenHandler(evt:ViewEvent):void {
79                if(skin.stage['displayState'] == 'fullScreen') {
80                        skin.stage['displayState'] = 'normal';
81                } else {
82                        fullscreenrect();
83                        skin.stage['displayState'] = 'fullScreen';
84                }
85        };
86
87
88        /** Set the fullscreen rectangle **/
89        private function fullscreenrect():void {
90                try {
91                        skin.stage["fullScreenSourceRect"] = new Rectangle(skin.x,skin.y,
92                                Capabilities.screenResolutionX/2,Capabilities.screenResolutionY/2);
93                } catch (err:Error) {}
94        };
95
96
97        /** Jump to a userdefined item in the playlist. **/
98        private function itemHandler(evt:ViewEvent):void {
99                var itm = evt.data.index;
100                if (itm < 0) {
101                        playItem(0);
102                } else if (itm > playlist.length-1) {
103                        playItem(playlist.length-1);
104                } else if (!isNaN(itm)) {
105                        playItem(itm);
106                }
107        };
108
109
110        /** Jump to the link of a playlistitem. **/
111        private function linkHandler(evt:ViewEvent):void {
112                var itm = evt.data.index;
113                if (itm  == undefined) {
114                        itm = config['item'];
115                }
116                var lnk = playlist[itm]['link'];
117                if(lnk != undefined) {
118                        navigateToURL(new URLRequest(lnk),config['linktarget']);
119                }
120        };
121
122
123        /** Load a new playlist. **/
124        private function loadHandler(evt:ViewEvent):void {
125                stopHandler();
126                try {
127                        playlister.load(evt.data.object);
128                } catch (err:Error) {
129                        dispatchEvent(new ControllerEvent(ControllerEvent.ERROR,{message:err.message}));
130                }
131        };
132
133
134
135        /** Update playlist item duration. **/
136        private function metaHandler(evt:ModelEvent):void {
137                if(evt.data.duration) {
138                        var dur = Math.round(evt.data.duration*10)/10
139                        playlister.update(config['item'],'duration',dur);
140                }
141        };
142
143
144        /** Save new state of the mute switch and send volume. **/
145        private function muteHandler(evt:ViewEvent):void {
146                if(evt.data.state) {
147                        if(evt.data.state == config['mute']) {
148                                return;
149                        } else {
150                                config['mute'] = evt.data.state;
151                        }
152                } else {
153                        config['mute'] = !config['mute'];
154                }
155                dispatchEvent(new ControllerEvent(ControllerEvent.MUTE,{state:config['mute']}));
156        };
157
158
159        /** Jump to the next item in the playlist. **/
160        private function nextHandler(evt:ViewEvent):void {
161                if(playlist && config['shuffle'] == true) {
162                        playItem(randomizer.pick());
163                } else if (playlist && config['item'] == playlist.length-1) {
164                        playItem(0);
165                } else if (playlist) {
166                        playItem(config['item']+1);
167                }
168        };
169
170
171        /** Change the playback state. **/
172        private function playHandler(evt:ViewEvent):void {
173                if(playlist) {
174                        if(evt.data.state != false && config['state'] == ModelStates.PAUSED) {
175                                dispatchEvent(new ControllerEvent(ControllerEvent.PLAY,{state:true}));
176                        } else if (evt.data.state != false && config['state'] == ModelStates.COMPLETED) {
177                                dispatchEvent(new ControllerEvent(ControllerEvent.SEEK,{position:playlist[config['item']]['start']}));
178                        } else if(evt.data.state != false && config['state'] == ModelStates.IDLE) {
179                                playItem();
180                        } else if (evt.data.state != true &&
181                                (config['state'] == ModelStates.PLAYING || config['state'] == ModelStates.BUFFERING)) {
182                                dispatchEvent(new ControllerEvent(ControllerEvent.PLAY,{state:false}));
183                        }
184                }
185        };
186
187
188        /** Direct the model to play a new item. **/
189        private function playItem(nbr:Number=undefined):void {
190                if(nbr > -1) {
191                        if(playlist[nbr]['file'] == playlist[config['item']]['file']) {
192                                playlist[nbr]['duration'] = playlist[config['item']]['duration'];
193                        }
194                        config['item'] = nbr;
195                }
196                dispatchEvent(new ControllerEvent(ControllerEvent.ITEM,{index:config['item']}));
197        };
198
199
200        /** Manage loading of a new playlist. **/
201        private function playlistHandler(evt:Event):void {
202                if(config['shuffle'] == true) {
203                        randomizer = new Randomizer(playlist.length);
204                        config['item'] = randomizer.pick();
205                } else if (config['item'] > playlist.length) {
206                        config['item'] = playlist.length-1;
207                }
208                dispatchEvent(new ControllerEvent(ControllerEvent.PLAYLIST,{playlist:playlist}));
209                if(config['autostart'] == true) {
210                        playItem();
211                }
212        };
213
214
215        /** Jump to the previous item in the playlist. **/
216        private function prevHandler(evt:ViewEvent):void {
217                if (config['item'] == 0) {
218                        playItem(playlist.length-1);
219                } else {
220                        playItem(config['item']-1);
221                }
222        };
223
224
225        /** Switch playback quality. **/
226        private function qualityHandler(evt:ViewEvent=null):void {
227                if(evt.data.state != undefined) {
228                        if(evt.data.state == config['quality']) {
229                                return;
230                        } else {
231                                config['quality'] = evt.data.state;
232                        }
233                } else {
234                        config['quality'] = !config['quality'];
235                }
236                dispatchEvent(new ControllerEvent(ControllerEvent.QUALITY,{state:config['quality']}));
237        };
238
239
240        /** Forward a resizing of the stage. **/
241        private function redrawHandler(evt:ViewEvent=null):void {
242                var dat = new Object();
243                try {
244                        var dps = skin.stage['displayState'];
245                } catch (err:Error) {}
246                if(dps == 'fullScreen') {
247                        dat.fullscreen = true;
248                        dat.width = skin.stage.stageWidth;
249                        dat.height = skin.stage.stageHeight;
250                } else if(config['scaling']) {
251                        dat.fullscreen = false;
252                        dat.width = skin.stage.stageWidth;
253                        dat.height = skin.stage.stageHeight;
254                        if(config['controlbar'] == 'bottom') {
255                                dat.height -= config['controlbarsize'];
256                        }
257                        if(config['playlist'] == 'right') {
258                                dat.width -= config['playlistsize'];
259                        } else if(config['playlist'] == 'bottom') {
260                                dat.height -= config['playlistsize'];
261                        }
262                } else {
263                        dat.fullscreen = false;
264                        dat.width = config['width'];
265                        dat.height = config['height'];
266                }
267                config['width'] = dat.width;
268                config['height'] = dat.height;
269                dispatchEvent(new ControllerEvent(ControllerEvent.RESIZE,dat));
270        };
271
272
273        /** Seek to a specific part in a mediafile. **/
274        private function seekHandler(evt:ViewEvent):void {
275                if(config['state'] != ModelStates.IDLE && playlist[config['item']]['duration'] > 0) {
276                        var pos = evt.data.position;
277                        if(pos < 2) {
278                                pos = 0;
279                        } else if (pos > playlist[config['item']]['duration']-2) {
280                                pos = playlist[config['item']]['duration']-2;
281                        }
282                        dispatchEvent(new ControllerEvent(ControllerEvent.SEEK,{position:pos}));
283                }
284        };
285
286
287        /** Stop all playback and buffering. **/
288        private function stopHandler(evt:ViewEvent=undefined):void {
289                dispatchEvent(new ControllerEvent(ControllerEvent.STOP));
290        };
291
292
293        /** Manage playback state changes. **/
294        private function stateHandler(evt:ModelEvent):void {
295                if(evt.data.newstate == ModelStates.COMPLETED && (config['repeat'] == 'always' ||
296                        (config['repeat'] == 'list' && config['shuffle'] == true && randomizer.length > 0) ||
297                        (config['repeat'] == 'list' && config['shuffle'] == false && config['item'] < playlist.length-1))) {
298                        if(config['shuffle'] == true) {
299                                playItem(randomizer.pick());
300                        } else if(config['item'] == playlist.length-1) {
301                                playItem(0);
302                        } else {
303                                playItem(config['item']+1);
304                        }
305                }
306        };
307
308
309        /** Save new state of the mute switch and send volume. **/
310        private function volumeHandler(evt:ViewEvent):void {
311                var vol = evt.data.percentage;
312                if (vol < 1) {
313                        muteHandler(new ViewEvent(ViewEvent.MUTE,{state:true}));
314                } else if (!isNaN(vol) && vol < 101) {
315                        if(config['mute'] == true) {
316                                muteHandler(new ViewEvent(ViewEvent.MUTE,{state:false}));
317                        }
318                        config['volume'] = vol;
319                        dispatchEvent(new ControllerEvent(ControllerEvent.VOLUME,{percentage:vol}));
320                }
321        };
322
323
324        /** Getter for the playlist. **/
325        public function get playlist():Array {
326                return playlister.playlist;
327        };
328
329
330}
331
332
333}
Note: See TracBrowser for help on using the repository browser.