root/trunk/as3/com/jeroenwijering/player/Model.as @ 1

Revision 1, 5.2 kB (checked in by jeroen, 18 months ago)

initial commit of old repository into public one

  • 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;
15
16
17public class Model extends EventDispatcher {
18
19
20
21        /** Object with all configuration variables. **/
22        public var config:Object;
23        /** Reference to the skin MovieClip. **/
24        public var skin:MovieClip;
25        /** Reference to the player's controller. **/
26        private var controller:Controller;
27        /** Currently active model. **/
28        private var current:Object;
29        /** Current playback state **/
30        public var state:String;
31        /** Loader for the preview image. **/
32        private var loader:Loader;
33
34
35        /** Constructor, save arrays and set currentItem. **/
36        public function Model(cfg:Object,skn:MovieClip,ctr:Controller) {
37                config = cfg;
38                skin = skn;
39                controller = ctr;
40                controller.addEventListener(ControllerEvent.ITEM,itemHandler);
41                controller.addEventListener(ControllerEvent.MUTE,muteHandler);
42                controller.addEventListener(ControllerEvent.PLAY,playHandler);
43                controller.addEventListener(ControllerEvent.QUALITY,qualityHandler);
44                controller.addEventListener(ControllerEvent.RESIZE,resizeHandler);
45                controller.addEventListener(ControllerEvent.SEEK,seekHandler);
46                controller.addEventListener(ControllerEvent.STOP,stopHandler);
47                controller.addEventListener(ControllerEvent.VOLUME,volumeHandler);
48                loader = new Loader();
49                loader.contentLoaderInfo.addEventListener(Event.INIT,thumbHandler);
50                Draw.clear(skin.display.media);
51                Draw.clear(skin.display.thumb);
52        };
53
54
55        /** Item change: switch the curently active model if there's a new URL **/
56        private function itemHandler(evt:ControllerEvent) {
57                skin.display.media.visible = false;
58                if(current) { current.stop(); }
59                sendEvent(ModelEvent.STATE,{newstate:ModelStates.IDLE});
60                switch(playlist[evt.data.index]['type']) {
61                        case 'camera':
62                                current = new CameraModel(this);
63                                break;
64                        case 'image':
65                                current = new ImageModel(this);
66                                break;
67                        case 'rtmp':
68                                current = new RTMPModel(this);
69                                break;
70                        case 'sound':
71                                current = new SoundModel(this);
72                                break;
73                        case 'video':
74                                if(config['streamscript']) {
75                                        current = new HTTPModel(this);
76                                } else {
77                                        current = new VideoModel(this);
78                                }
79                                break;
80                        case 'youtube':
81                                current = new YoutubeModel(this);
82                                break;
83                }
84                if(playlist[evt.data.index]['image']) {
85                        skin.display.thumb.visible = true;
86                        loader.load(new URLRequest(playlist[evt.data.index]['image']));
87                } else {
88                        skin.display.thumb.visible = false;
89                }
90        };
91
92
93        /** Place a loaded thumb on stage. **/
94        private function thumbHandler(evt:Event) {
95                var obj = skin.display.thumb;
96                Draw.clear(obj);
97                obj.addChild(loader);
98                Bitmap(loader.content).smoothing = config['quality'];
99                Stretcher.stretch(obj,config['width'],config['height'],config['stretching']);
100        };
101
102
103        /** Place a loaded mediafile on stage **/
104        public function mediaHandler(chd:DisplayObject) {
105                var obj = skin.display.media;
106                Draw.clear(obj);
107                obj.addChild(chd);
108                Stretcher.stretch(obj,config['width'],config['height'],config['stretching']);
109                skin.display.thumb.visible = false;
110                skin.display.media.visible = true;
111        };
112
113
114        /** Load the configuration array. **/
115        private function muteHandler(evt:ControllerEvent) {
116                if(current && evt.data.state == true) {
117                        current.volume(0);
118                } else if(current && evt.data.state == false) {
119                        current.volume(config['volume']);
120                }
121        };
122
123
124        /** Togge the playback state. **/
125        private function playHandler(evt:ControllerEvent) {
126                if(evt.data.state == true) {
127                        if(state == ModelStates.IDLE) {
128                                current.load();
129                        } else if(state != ModelStates.PAUSED) {
130                                current.seek(playlist[config['item']]['start']);
131                        } else {
132                                current.play();
133                        }
134                } else {
135                        current.pause();
136                }
137        };
138
139
140        /** Toggle the playback quality. **/
141        private function qualityHandler(evt:ControllerEvent) {
142                current.quality(evt.data.state);
143        };
144
145
146        /** Resize the media and thumb. **/
147        private function resizeHandler(evt:ControllerEvent) {
148                Stretcher.stretch(skin.display.thumb,evt.data.width,evt.data.height,config['stretching']);
149                Stretcher.stretch(skin.display.media,evt.data.width,evt.data.height,config['stretching']);
150        };
151
152
153        /** Seek inside a file. **/
154        private function seekHandler(evt:ControllerEvent) {
155                if(state != ModelStates.IDLE) {
156                        current.seek(evt.data.position);
157                }
158        };
159
160
161        /** Load the configuration array. **/
162        private function stopHandler(evt:ControllerEvent) {
163                current.stop();
164                sendEvent(ModelEvent.STATE,{newstate:ModelStates.IDLE});
165        };
166
167
168        /**  Dispatch events. State switch is saved. **/
169        public function sendEvent(typ:String,dat:Object) {
170                if(typ == ModelEvent.STATE && dat.newstate != state) {
171                        dat.oldstate = state;
172                        state = dat.newstate;
173                        dispatchEvent(new ModelEvent(typ,dat));
174                } else if (typ != ModelEvent.STATE) {
175                        dispatchEvent(new ModelEvent(typ,dat));
176                }
177        };
178
179
180        /** Load the configuration array. **/
181        private function volumeHandler(evt:ControllerEvent) {
182                current.volume(evt.data.percentage);
183        };
184
185
186        /** Getter for the playlist **/
187        public function get playlist():Array {
188                return controller.playlist;
189        };
190
191
192}
193
194
195}
Note: See TracBrowser for help on using the browser.