source: trunk/as3/com/jeroenwijering/player/Model.as @ 118

Revision 118, 7.0 KB checked in by jeroen, 4 years ago (diff)

made desktopplayer work again and fixed fullscreen without resizing

  • Property svn:executable set to *
Line 
1/**
2* Wrap all media API's and manage playback.
3**/
4package com.jeroenwijering.player {
5
6
7import com.jeroenwijering.events.*;
8import com.jeroenwijering.models.*;
9import com.jeroenwijering.player.*;
10import com.jeroenwijering.utils.*;
11import flash.display.*;
12import flash.events.Event;
13import flash.events.EventDispatcher;
14import flash.net.URLRequest;
15import flash.system.LoaderContext;
16import flash.utils.getDefinitionByName;
17
18
19public class Model extends EventDispatcher {
20
21
22        /** Object with all configuration variables. **/
23        public var config:Object;
24        /** Reference to the skin MovieClip. **/
25        public var skin:MovieClip;
26        /** Object with all display variables. **/
27        private var sploader:SPLoader;
28        /** Reference to the player's controller. **/
29        private var controller:Controller;
30        /** The list with all active models. **/
31        private var models:Object;
32        /** Loader for the preview image. **/
33        private var thumb:Loader;
34        /** Save the current image to prevent overloading. **/
35        private var image:String;
36        /** Currently active model. **/
37        private var currentModel:String;
38
39
40        /** Constructor, save arrays and set currentItem. **/
41        public function Model(cfg:Object,skn:MovieClip,ldr:SPLoader,ctr:Controller):void {
42                config = cfg;
43                skin = skn;
44                sploader = ldr;
45                Draw.clear(skin.display.media);
46                controller = ctr;
47                controller.addEventListener(ControllerEvent.ITEM,itemHandler);
48                controller.addEventListener(ControllerEvent.MUTE,muteHandler);
49                controller.addEventListener(ControllerEvent.PLAY,playHandler);
50                controller.addEventListener(ControllerEvent.PLAYLIST,playlistHandler);
51                controller.addEventListener(ControllerEvent.QUALITY,qualityHandler);
52                controller.addEventListener(ControllerEvent.RESIZE,resizeHandler);
53                controller.addEventListener(ControllerEvent.SEEK,seekHandler);
54                controller.addEventListener(ControllerEvent.STOP,stopHandler);
55                controller.addEventListener(ControllerEvent.VOLUME,volumeHandler);
56                thumb = new Loader();
57                thumb.contentLoaderInfo.addEventListener(Event.COMPLETE,resizeHandler);
58                skin.display.addChildAt(thumb,skin.display.getChildIndex(skin.display.media)+1);
59                models = new Object();
60        };
61
62
63        /** Item change: switch the curently active model if there's a new URL **/
64        private function itemHandler(evt:ControllerEvent):void {
65                var typ:String = playlist[config['item']]['type'];
66                if(currentModel) {
67                        models[currentModel].stop();
68                }
69                if(!models[typ]) {
70                        loadModel(typ);
71                }
72                if(models[typ]) {
73                        currentModel = typ;
74                        models[typ].load();
75                } else {
76                        sendEvent(ModelEvent.ERROR,{message:'No suiteable model found for playback.'});
77                }
78                thumbLoader();
79        };
80
81
82        /** Initialize a new model. **/
83        private function loadModel(typ:String):void {
84                switch(typ) {
85                        case 'brightcove':
86                                models[typ] = new BrightcoveModel(this);
87                                break;
88                        case 'camera':
89                                models[typ] = new CameraModel(this);
90                                break;
91                        case 'http':
92                                models[typ] = new HTTPModel(this);
93                                break;
94                        case 'image':
95                                models[typ] = new ImageModel(this);
96                                break;
97                        case 'rtmp':
98                                models[typ] = new RTMPModel(this);
99                                break;
100                        case 'sound':
101                                models[typ] = new SoundModel(this);
102                                break;
103                        case 'video':
104                                models[typ] = new VideoModel(this);
105                                break;
106                        case 'youtube':
107                                models[typ] = new YoutubeModel(this);
108                                break;
109                }
110        };
111
112
113        /** Place a loaded mediafile on stage **/
114        public function mediaHandler(chd:DisplayObject=undefined):void {
115                Draw.clear(skin.display.media);
116                skin.display.media.addChild(chd);
117                resizeHandler();
118        };
119
120
121        /** Load the configuration array. **/
122        private function muteHandler(evt:ControllerEvent):void {
123                if(currentModel) {
124                        if(evt.data.state == true) {
125                                models[currentModel].volume(0);
126                        } else {
127                                models[currentModel].volume(config['volume']);
128                        }
129                }
130        };
131
132
133        /** Togge the playback state. **/
134        private function playHandler(evt:ControllerEvent):void {
135                if(currentModel) {
136                        if(evt.data.state == true) {
137                                models[currentModel].play();
138                        } else {
139                                models[currentModel].pause();
140                        }
141                }
142        };
143
144
145        /** Send an idle with new playlist. **/
146        private function playlistHandler(evt:ControllerEvent):void {
147                thumbLoader();
148        };
149
150
151        /** Toggle the playback quality. **/
152        private function qualityHandler(evt:ControllerEvent):void {
153                if(currentModel) {
154                        models[currentModel].quality(evt.data.state);
155                }
156        };
157
158
159        /** Resize the media and thumb. **/
160        private function resizeHandler(evt:Event=null):void {
161                var cfg:Object = sploader.getPluginConfig(sploader.getPlugin('display'));
162                Stretcher.stretch(skin.display.media,cfg['width'],cfg['height'],config['stretching']);
163                if(thumb.content) {
164                        Stretcher.stretch(thumb,cfg['width'],cfg['height'],config['stretching']);
165                }
166        };
167
168
169        /** Seek inside a file. **/
170        private function seekHandler(evt:ControllerEvent):void {
171                if(currentModel) {
172                        models[currentModel].seek(evt.data.position);
173                }
174        };
175
176
177        /** Load the configuration array. **/
178        private function stopHandler(evt:ControllerEvent=undefined):void {
179                if(currentModel) {
180                        models[currentModel].stop();
181                }
182                sendEvent(ModelEvent.STATE,{newstate:ModelStates.IDLE});
183        };
184
185
186        /**  Dispatch events. State switch is saved. **/
187        public function sendEvent(typ:String,dat:Object):void {
188                switch(typ) {
189                        case ModelEvent.STATE:
190                                dat['oldstate'] = config['state'];
191                                config['state'] = dat.newstate;
192                                dispatchEvent(new ModelEvent(typ,dat));
193                                switch(dat['newstate']) {
194                                        case ModelStates.IDLE:
195                                        case ModelStates.COMPLETED:
196                                                thumb.visible = true;
197                                                skin.display.media.visible = false;
198                                                if(playlist) {
199                                                        sendEvent(ModelEvent.TIME,{position:0,duration:playlist[config['item']]['duration']});
200                                                }
201                                                break;
202                                        case ModelStates.PLAYING:
203                                                var ext:String = playlist[config['item']]['file'].substr(-3);
204                                                if(ext != 'aac' && ext != 'mp3' && ext != 'm4a') {
205                                                        thumb.visible = false;
206                                                        skin.display.media.visible = true;
207                                                } else {
208                                                        thumb.visible = true;
209                                                        skin.display.media.visible = false;
210                                                }
211                                                break;
212                                }
213                                break;
214                        case ModelEvent.TIME:
215                                dat['duration'] = playlist[config['item']]['duration'];
216                                if(dat['duration'] > 0 && dat['duration'] < dat['position']) {
217                                        models[currentModel].pause();
218                                        sendEvent(ModelEvent.STATE,{newstate:ModelStates.COMPLETED});
219                                } else {
220                                        dispatchEvent(new ModelEvent(typ,dat));
221                                }
222                                break;
223                       
224                        case ModelEvent.ERROR:
225                                models[currentModel].stop();
226                                sendEvent(ModelEvent.STATE,{newstate:ModelStates.IDLE});
227                                dispatchEvent(new ModelEvent(typ,dat));
228                                break;
229                        case ModelEvent.META:
230                                if(dat.width) { resizeHandler(); }
231                        default:
232                                dispatchEvent(new ModelEvent(typ,dat));
233                                break;
234                }
235        };
236
237
238        /** Load a thumb on stage. **/
239        private function thumbLoader():void {
240                var img:String = playlist[config['item']]['image'];
241                if(img && img != image) {
242                        image = img;
243                        thumb.load(new URLRequest(img),new LoaderContext(true));
244                }
245        };
246
247
248        /** Load the configuration array. **/
249        private function volumeHandler(evt:ControllerEvent):void {
250                if(currentModel) {
251                        models[currentModel].volume(evt.data.percentage);
252                }
253        };
254
255
256        /** Getter for the playlist **/
257        public function get playlist():Array {
258                return controller.playlist;
259        };
260
261
262}
263
264
265}
Note: See TracBrowser for help on using the repository browser.