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

Revision 104, 6.5 KB checked in by jeroen, 5 years ago (diff)

added HD switch plugin (and fixed a bug in the LOAD event

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