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

Revision 438, 5.9 KB checked in by jeroen, 4 years ago (diff)

added a small fix to Model that prevents a brief flash of the image if the video is set to autostart

  • Property svn:executable set to *
Line 
1/**
2* Wraps all media APIs (all models) and manages thumbnail display.
3**/
4package com.jeroenwijering.player {
5
6
7import com.jeroenwijering.events.*;
8import com.jeroenwijering.models.AbstractModel;
9import com.jeroenwijering.player.*;
10import com.jeroenwijering.utils.*;
11
12import flash.display.*;
13import flash.events.*;
14import flash.net.URLRequest;
15import flash.system.LoaderContext;
16
17
18public class Model extends EventDispatcher {
19
20
21        /** Object with all configuration variables. **/
22        public var config:Object;
23        /** Reference to the media element. **/
24        public var media:Sprite;
25        /** Reference to the player's controller. **/
26        private var controller:Controller;
27        /** The list with all active models. **/
28        private var models:Object;
29        /** Loader for the preview image. **/
30        private var thumb:Loader;
31        /** Save the currently playing playlist item. **/
32        private var item:Object;
33        /** Save the current image url to prevent duplicate loading. **/
34        private var image:String;
35
36
37        /** Constructor, save references, setup listeners and  init thumbloader. **/
38        public function Model(cfg:Object,skn:MovieClip,ldr:SPLoader,ctr:Controller):void {
39                config = cfg;
40                controller = ctr;
41                controller.addEventListener(ControllerEvent.ITEM,itemHandler);
42                controller.addEventListener(ControllerEvent.MUTE,muteHandler);
43                controller.addEventListener(ControllerEvent.PLAY,playHandler);
44                controller.addEventListener(ControllerEvent.PLAYLIST,playlistHandler);
45                controller.addEventListener(ControllerEvent.RESIZE,resizeHandler);
46                controller.addEventListener(ControllerEvent.SEEK,seekHandler);
47                controller.addEventListener(ControllerEvent.STOP,stopHandler);
48                controller.addEventListener(ControllerEvent.VOLUME,volumeHandler);
49                models = new Object();
50                thumb = new Loader();
51                thumb.contentLoaderInfo.addEventListener(Event.COMPLETE,thumbHandler);
52                thumb.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,thumbHandler);
53                var dpl:MovieClip = skn.getChildByName('display') as MovieClip;
54                media = dpl.media as Sprite;
55                media.visible = false;
56                Draw.clear(media);
57                dpl.addChildAt(thumb,dpl.getChildIndex(media));
58        };
59
60
61        /** Load a new playback model. **/
62        public function addModel(mdl:AbstractModel,typ:String):void {
63                models[typ] = mdl;
64        };
65
66        /** Check if the currently playing item is audio only. **/
67        private function audioOnly():Boolean {
68                var ext:String = item['file'].substr(-3);
69                if(ext == 'm4a' || ext == 'mp3' || ext == 'aac') {
70                        return true;
71                } else {
72                        return false;
73                }
74        };
75
76
77        /** Item change: stop the old model and start the new one. **/
78        private function itemHandler(evt:ControllerEvent):void {
79                if(item) {
80                        models[item['type']].stop();
81                        media.removeChild(models[item['type']]);
82                }
83                item = controller.playlist[config['item']];
84                if(models[item['type']]) {
85                        media.addChild(models[item['type']]);
86                        models[item['type']].load(item);
87                } else {
88                        sendEvent(ModelEvent.ERROR,{message:'No suiteable model found for playback of this file.'});
89                }
90                if(item['image']) {
91                        if(item['image'] != image) {
92                                image = item['image'];
93                                thumb.load(new URLRequest(item['image']),new LoaderContext(true));
94                        }
95                } else if(image) {
96                        image = undefined;
97                        thumb.unload();
98                }
99        };
100
101
102        /** Make the current model toggle its mute state. **/
103        private function muteHandler(evt:ControllerEvent):void {
104                if(item) {
105                        if(evt.data.state == true) {
106                                models[item['type']].volume(0);
107                        } else {
108                                models[item['type']].volume(config['volume']);
109                        }
110                }
111        };
112
113
114        /** Make the current model play or pause. **/
115        private function playHandler(evt:ControllerEvent):void {
116                if(item) {
117                        if(evt.data.state == true) {
118                                models[item['type']].play();
119                        } else {
120                                models[item['type']].pause();
121                        }
122                }
123        };
124
125
126        /** Load a thumb; either from the playlist or (if there) player-wide.  **/
127        private function playlistHandler(evt:ControllerEvent):void {
128                var img:String = controller.playlist[config['item']]['image'];
129                if(img && img != image) {
130                        image = img;
131                        thumb.load(new URLRequest(img),new LoaderContext(true));
132                }
133        };
134
135
136        /** Resize the media and thumb. **/
137        private function resizeHandler(evt:ControllerEvent):void {
138                if(thumb.width) { thumbResize(); };
139                if(item) { models[item['type']].resize(); }
140        };
141
142
143        /** Make the current model seek. **/
144        private function seekHandler(evt:ControllerEvent):void {
145                if(item) {
146                        models[item['type']].seek(evt.data.position);
147                }
148        };
149
150
151        /** Make the current model stop and show the thumb. **/
152        private function stopHandler(evt:ControllerEvent=undefined):void {
153                if(item) {
154                        models[item['type']].stop();
155                }
156        };
157
158
159        /**
160        * Dispatch events to the View/ Controller.
161        * When switching states, the thumbnail is shown/hidden.
162        *
163        * @param typ    The eventtype to dispatch.
164        * @param dat    An object with data to send along.
165        * @see                  ModelEvent
166        **/
167        public function sendEvent(typ:String,dat:Object):void {
168                if(typ == ModelEvent.STATE) {
169                        if(dat.newstate == config['state']) { return; }
170                        dat['oldstate'] = config['state'];
171                        config['state'] = dat.newstate;
172                        switch(dat['newstate']) {
173                                case ModelStates.IDLE:
174                                        sendEvent(ModelEvent.LOADED,{loaded:0,offset:0,total:0});
175                                case ModelStates.COMPLETED:
176                                        thumb.visible = true;
177                                        media.visible = false;
178                                        sendEvent(ModelEvent.TIME,{position:item['start'],duration:item['duration']});
179                                        break;
180                                case ModelStates.BUFFERING:
181                                case ModelStates.PLAYING:
182                                        thumb.visible = audioOnly();
183                                        media.visible = !audioOnly();
184                                        break;
185                        }
186                }
187                Logger.log(dat,typ);
188                dispatchEvent(new ModelEvent(typ,dat));
189        };
190
191
192        /** Thumb loaded, try to antialias it before resizing. **/
193        private function thumbHandler(evt:Event):void {
194                try {
195                        Bitmap(thumb.content).smoothing = true;
196                } catch (err:Error) {}
197                thumbResize();
198        };
199
200
201        /** Resize the thumbnail. **/
202        private function thumbResize():void {
203                Stretcher.stretch(thumb,config['width'],config['height'],config['stretching']);
204        };
205
206
207        /** Make the current model change volume. **/
208        private function volumeHandler(evt:ControllerEvent):void {
209                if(item) {
210                        models[item['type']].volume(evt.data.percentage);
211                }
212        };
213
214
215}
216
217
218}
Note: See TracBrowser for help on using the repository browser.