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

Revision 3, 5.1 kB (checked in by jeroen, 18 months ago)

added js initer BUT destroyed controlbar display

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