| 1 | /** |
|---|
| 2 | * Process all input from the views and modifies the model. |
|---|
| 3 | **/ |
|---|
| 4 | package com.jeroenwijering.player { |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | import com.jeroenwijering.events.*; |
|---|
| 8 | import com.jeroenwijering.player.*; |
|---|
| 9 | import com.jeroenwijering.utils.*; |
|---|
| 10 | import flash.display.MovieClip; |
|---|
| 11 | import flash.events.*; |
|---|
| 12 | import flash.geom.Rectangle; |
|---|
| 13 | import flash.net.*; |
|---|
| 14 | import flash.system.Capabilities; |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | public class Controller extends EventDispatcher { |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | /** Configuration object **/ |
|---|
| 21 | private var config:Object; |
|---|
| 22 | /** Reference to the skin; for stage event subscription. **/ |
|---|
| 23 | private var skin:MovieClip; |
|---|
| 24 | /** Object that manages the playlist. **/ |
|---|
| 25 | private var playlister:Playlister; |
|---|
| 26 | /** Reference to the player's model. **/ |
|---|
| 27 | private var model:Model; |
|---|
| 28 | /** Reference to the player's view. **/ |
|---|
| 29 | private var view:View; |
|---|
| 30 | /** object that provides randomization. **/ |
|---|
| 31 | private var randomizer:Randomizer; |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | /** Constructor, set up stage and playlist listeners. **/ |
|---|
| 35 | public function Controller(cfg:Object,skn:MovieClip):void { |
|---|
| 36 | config = cfg; |
|---|
| 37 | skin = skn; |
|---|
| 38 | playlister = new Playlister(); |
|---|
| 39 | playlister.addEventListener(Event.COMPLETE,playlistHandler); |
|---|
| 40 | playlister.addEventListener(ErrorEvent.ERROR,errorHandler); |
|---|
| 41 | resizeHandler(new ViewEvent(ViewEvent.RESIZE,{ |
|---|
| 42 | width:skin.stage.stageWidth, |
|---|
| 43 | height:skin.stage.stageHeight |
|---|
| 44 | })); |
|---|
| 45 | }; |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | /** Register view and model with controller, start loading playlist. **/ |
|---|
| 49 | public function start(mdl:Model,vie:View):void { |
|---|
| 50 | model= mdl; |
|---|
| 51 | model.addEventListener(ModelEvent.META,metaHandler); |
|---|
| 52 | model.addEventListener(ModelEvent.TIME,metaHandler); |
|---|
| 53 | model.addEventListener(ModelEvent.STATE,stateHandler); |
|---|
| 54 | view = vie; |
|---|
| 55 | view.addEventListener(ViewEvent.FULLSCREEN,fullscreenHandler); |
|---|
| 56 | view.addEventListener(ViewEvent.ITEM,itemHandler); |
|---|
| 57 | view.addEventListener(ViewEvent.LINK,linkHandler); |
|---|
| 58 | view.addEventListener(ViewEvent.LOAD,loadHandler); |
|---|
| 59 | view.addEventListener(ViewEvent.MUTE,muteHandler); |
|---|
| 60 | view.addEventListener(ViewEvent.NEXT,nextHandler); |
|---|
| 61 | view.addEventListener(ViewEvent.PLAY,playHandler); |
|---|
| 62 | view.addEventListener(ViewEvent.PREV,prevHandler); |
|---|
| 63 | view.addEventListener(ViewEvent.QUALITY,qualityHandler); |
|---|
| 64 | view.addEventListener(ViewEvent.RESIZE,resizeHandler); |
|---|
| 65 | view.addEventListener(ViewEvent.SEEK,seekHandler); |
|---|
| 66 | view.addEventListener(ViewEvent.STOP,stopHandler); |
|---|
| 67 | view.addEventListener(ViewEvent.VOLUME,volumeHandler); |
|---|
| 68 | resizeHandler(new ViewEvent(ViewEvent.RESIZE, { |
|---|
| 69 | width:skin.stage.stageWidth, |
|---|
| 70 | height:skin.stage.stageHeight |
|---|
| 71 | })); |
|---|
| 72 | if(config['file']) { |
|---|
| 73 | playlister.load(config); |
|---|
| 74 | } |
|---|
| 75 | }; |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | /** Catch errors dispatched by the playlister. **/ |
|---|
| 80 | private function errorHandler(evt:ErrorEvent):void { |
|---|
| 81 | dispatchEvent(new ControllerEvent(ControllerEvent.ERROR,{message:evt.text})); |
|---|
| 82 | }; |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | /** Switch fullscreen state. **/ |
|---|
| 86 | private function fullscreenHandler(evt:ViewEvent):void { |
|---|
| 87 | if(skin.stage['displayState'] == 'fullScreen') { |
|---|
| 88 | skin.stage['displayState'] = 'normal'; |
|---|
| 89 | } else { |
|---|
| 90 | fullscreenrect(); |
|---|
| 91 | skin.stage['displayState'] = 'fullScreen'; |
|---|
| 92 | } |
|---|
| 93 | }; |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | /** Set the fullscreen rectangle **/ |
|---|
| 97 | private function fullscreenrect():void { |
|---|
| 98 | try { |
|---|
| 99 | skin.stage["fullScreenSourceRect"] = new Rectangle(skin.x,skin.y, |
|---|
| 100 | Capabilities.screenResolutionX/2,Capabilities.screenResolutionY/2); |
|---|
| 101 | } catch (err:Error) {} |
|---|
| 102 | }; |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | /** Jump to a userdefined item in the playlist. **/ |
|---|
| 106 | private function itemHandler(evt:ViewEvent):void { |
|---|
| 107 | var itm = evt.data.index; |
|---|
| 108 | if (itm < 0) { |
|---|
| 109 | playItem(0); |
|---|
| 110 | } else if (itm > playlist.length-1) { |
|---|
| 111 | playItem(playlist.length-1); |
|---|
| 112 | } else if (!isNaN(itm)) { |
|---|
| 113 | playItem(itm); |
|---|
| 114 | } |
|---|
| 115 | }; |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | /** Jump to the link of a playlistitem. **/ |
|---|
| 119 | private function linkHandler(evt:ViewEvent):void { |
|---|
| 120 | var itm = evt.data.index; |
|---|
| 121 | if (itm == undefined) { |
|---|
| 122 | itm = config['item']; |
|---|
| 123 | } |
|---|
| 124 | var lnk = playlist[itm]['link']; |
|---|
| 125 | if(lnk != undefined) { |
|---|
| 126 | navigateToURL(new URLRequest(lnk),config['linktarget']); |
|---|
| 127 | } |
|---|
| 128 | }; |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | /** Load a new playlist. **/ |
|---|
| 132 | private function loadHandler(evt:ViewEvent):void { |
|---|
| 133 | stopHandler(); |
|---|
| 134 | try { |
|---|
| 135 | playlister.load(evt.data.object); |
|---|
| 136 | } catch (err:Error) { |
|---|
| 137 | dispatchEvent(new ControllerEvent(ControllerEvent.ERROR,{message:err.message})); |
|---|
| 138 | } |
|---|
| 139 | }; |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | /** Update playlist item duration. **/ |
|---|
| 144 | private function metaHandler(evt:ModelEvent):void { |
|---|
| 145 | if(evt.data.duration) { |
|---|
| 146 | var dur = Math.round(evt.data.duration*10)/10 |
|---|
| 147 | playlister.update(config['item'],'duration',dur); |
|---|
| 148 | } |
|---|
| 149 | }; |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | /** Save new state of the mute switch and send volume. **/ |
|---|
| 153 | private function muteHandler(evt:ViewEvent):void { |
|---|
| 154 | if(evt.data.state) { |
|---|
| 155 | if(evt.data.state == config['mute']) { |
|---|
| 156 | return; |
|---|
| 157 | } else { |
|---|
| 158 | config['mute'] = evt.data.state; |
|---|
| 159 | } |
|---|
| 160 | } else { |
|---|
| 161 | config['mute'] = !config['mute']; |
|---|
| 162 | } |
|---|
| 163 | dispatchEvent(new ControllerEvent(ControllerEvent.MUTE,{state:config['mute']})); |
|---|
| 164 | }; |
|---|
| 165 | |
|---|
| 166 | |
|---|
| 167 | /** Jump to the next item in the playlist. **/ |
|---|
| 168 | private function nextHandler(evt:ViewEvent):void { |
|---|
| 169 | if(playlist && config['shuffle'] == true) { |
|---|
| 170 | playItem(randomizer.pick()); |
|---|
| 171 | } else if (playlist && config['item'] == playlist.length-1) { |
|---|
| 172 | playItem(0); |
|---|
| 173 | } else if (playlist) { |
|---|
| 174 | playItem(config['item']+1); |
|---|
| 175 | } |
|---|
| 176 | }; |
|---|
| 177 | |
|---|
| 178 | |
|---|
| 179 | /** Change the playback state. **/ |
|---|
| 180 | private function playHandler(evt:ViewEvent):void { |
|---|
| 181 | if(playlist) { |
|---|
| 182 | if(evt.data.state != false && config['state'] == ModelStates.PAUSED) { |
|---|
| 183 | dispatchEvent(new ControllerEvent(ControllerEvent.PLAY,{state:true})); |
|---|
| 184 | } else if (evt.data.state != false && config['state'] == ModelStates.COMPLETED) { |
|---|
| 185 | dispatchEvent(new ControllerEvent(ControllerEvent.SEEK,{position:playlist[config['item']]['start']})); |
|---|
| 186 | } else if(evt.data.state != false && config['state'] == ModelStates.IDLE) { |
|---|
| 187 | playItem(); |
|---|
| 188 | } else if (evt.data.state != true && |
|---|
| 189 | (config['state'] == ModelStates.PLAYING || config['state'] == ModelStates.BUFFERING)) { |
|---|
| 190 | dispatchEvent(new ControllerEvent(ControllerEvent.PLAY,{state:false})); |
|---|
| 191 | } |
|---|
| 192 | } |
|---|
| 193 | }; |
|---|
| 194 | |
|---|
| 195 | |
|---|
| 196 | /** Direct the model to play a new item. **/ |
|---|
| 197 | private function playItem(nbr:Number=undefined):void { |
|---|
| 198 | if(nbr > -1) { |
|---|
| 199 | if(playlist[nbr]['file'] == playlist[config['item']]['file']) { |
|---|
| 200 | playlist[nbr]['duration'] = playlist[config['item']]['duration']; |
|---|
| 201 | } |
|---|
| 202 | config['item'] = nbr; |
|---|
| 203 | } |
|---|
| 204 | dispatchEvent(new ControllerEvent(ControllerEvent.ITEM,{index:config['item']})); |
|---|
| 205 | }; |
|---|
| 206 | |
|---|
| 207 | |
|---|
| 208 | /** Manage loading of a new playlist. **/ |
|---|
| 209 | private function playlistHandler(evt:Event):void { |
|---|
| 210 | if(config['shuffle'] == true) { |
|---|
| 211 | randomizer = new Randomizer(playlist.length); |
|---|
| 212 | config['item'] = randomizer.pick(); |
|---|
| 213 | } else if (config['item'] > playlist.length) { |
|---|
| 214 | config['item'] = playlist.length-1; |
|---|
| 215 | } |
|---|
| 216 | dispatchEvent(new ControllerEvent(ControllerEvent.PLAYLIST,{playlist:playlist})); |
|---|
| 217 | if(config['autostart'] == true) { |
|---|
| 218 | playItem(); |
|---|
| 219 | } |
|---|
| 220 | }; |
|---|
| 221 | |
|---|
| 222 | |
|---|
| 223 | /** Jump to the previous item in the playlist. **/ |
|---|
| 224 | private function prevHandler(evt:ViewEvent):void { |
|---|
| 225 | if (config['item'] == 0) { |
|---|
| 226 | playItem(playlist.length-1); |
|---|
| 227 | } else { |
|---|
| 228 | playItem(config['item']-1); |
|---|
| 229 | } |
|---|
| 230 | }; |
|---|
| 231 | |
|---|
| 232 | |
|---|
| 233 | /** Switch playback quality. **/ |
|---|
| 234 | private function qualityHandler(evt:ViewEvent=null):void { |
|---|
| 235 | if(evt.data.state != undefined) { |
|---|
| 236 | if(evt.data.state == config['quality']) { |
|---|
| 237 | return; |
|---|
| 238 | } else { |
|---|
| 239 | config['quality'] = evt.data.state; |
|---|
| 240 | } |
|---|
| 241 | } else { |
|---|
| 242 | config['quality'] = !config['quality']; |
|---|
| 243 | } |
|---|
| 244 | fullscreenrect(); |
|---|
| 245 | dispatchEvent(new ControllerEvent(ControllerEvent.QUALITY,{state:config['quality']})); |
|---|
| 246 | }; |
|---|
| 247 | |
|---|
| 248 | |
|---|
| 249 | /** Forward a resizing of the stage. **/ |
|---|
| 250 | private function resizeHandler(evt:ViewEvent):void { |
|---|
| 251 | var mgn = config['margins'].split(','); |
|---|
| 252 | var dat = { |
|---|
| 253 | height:evt.data.height-mgn[0], |
|---|
| 254 | width:evt.data.width-mgn[1], |
|---|
| 255 | fullscreen:false |
|---|
| 256 | }; |
|---|
| 257 | try { |
|---|
| 258 | var dps = skin.stage['displayState']; |
|---|
| 259 | } catch (err:Error) {} |
|---|
| 260 | if(dps == 'fullScreen') { |
|---|
| 261 | dat.fullscreen = true; |
|---|
| 262 | } else { |
|---|
| 263 | if(config['controlbar'] == 'bottom') { |
|---|
| 264 | dat.height -= config['controlbarsize']; |
|---|
| 265 | } |
|---|
| 266 | if(config['playlist'] == 'right') { |
|---|
| 267 | dat.width -= config['playlistsize']; |
|---|
| 268 | } else if(config['playlist'] == 'bottom') { |
|---|
| 269 | dat.height -= config['playlistsize']; |
|---|
| 270 | } |
|---|
| 271 | } |
|---|
| 272 | config['height'] = dat.height; |
|---|
| 273 | config['width'] = dat.width; |
|---|
| 274 | dispatchEvent(new ControllerEvent(ControllerEvent.RESIZE,dat)); |
|---|
| 275 | }; |
|---|
| 276 | |
|---|
| 277 | |
|---|
| 278 | /** Seek to a specific part in a mediafile. **/ |
|---|
| 279 | private function seekHandler(evt:ViewEvent):void { |
|---|
| 280 | if(config['state'] != ModelStates.IDLE && playlist[config['item']]['duration'] > 0) { |
|---|
| 281 | var pos = evt.data.position; |
|---|
| 282 | if(pos < 2) { |
|---|
| 283 | pos = 0; |
|---|
| 284 | } else if (pos > playlist[config['item']]['duration']-2) { |
|---|
| 285 | pos = playlist[config['item']]['duration']-2; |
|---|
| 286 | } |
|---|
| 287 | dispatchEvent(new ControllerEvent(ControllerEvent.SEEK,{position:pos})); |
|---|
| 288 | } |
|---|
| 289 | }; |
|---|
| 290 | |
|---|
| 291 | |
|---|
| 292 | /** Stop all playback and buffering. **/ |
|---|
| 293 | private function stopHandler(evt:ViewEvent=undefined):void { |
|---|
| 294 | dispatchEvent(new ControllerEvent(ControllerEvent.STOP)); |
|---|
| 295 | }; |
|---|
| 296 | |
|---|
| 297 | |
|---|
| 298 | /** Manage playback state changes. **/ |
|---|
| 299 | private function stateHandler(evt:ModelEvent):void { |
|---|
| 300 | if(evt.data.newstate == ModelStates.COMPLETED && (config['repeat'] == 'always' || |
|---|
| 301 | (config['repeat'] == 'list' && config['shuffle'] == true && randomizer.length > 0) || |
|---|
| 302 | (config['repeat'] == 'list' && config['shuffle'] == false && config['item'] < playlist.length-1))) { |
|---|
| 303 | if(config['shuffle'] == true) { |
|---|
| 304 | playItem(randomizer.pick()); |
|---|
| 305 | } else if(config['item'] == playlist.length-1) { |
|---|
| 306 | playItem(0); |
|---|
| 307 | } else { |
|---|
| 308 | playItem(config['item']+1); |
|---|
| 309 | } |
|---|
| 310 | } |
|---|
| 311 | }; |
|---|
| 312 | |
|---|
| 313 | |
|---|
| 314 | /** Save new state of the mute switch and send volume. **/ |
|---|
| 315 | private function volumeHandler(evt:ViewEvent):void { |
|---|
| 316 | var vol = evt.data.percentage; |
|---|
| 317 | if (vol < 1) { |
|---|
| 318 | muteHandler(new ViewEvent(ViewEvent.MUTE,{state:true})); |
|---|
| 319 | } else if (!isNaN(vol) && vol < 101) { |
|---|
| 320 | if(config['mute'] == true) { |
|---|
| 321 | muteHandler(new ViewEvent(ViewEvent.MUTE,{state:false})); |
|---|
| 322 | } |
|---|
| 323 | config['volume'] = vol; |
|---|
| 324 | dispatchEvent(new ControllerEvent(ControllerEvent.VOLUME,{percentage:vol})); |
|---|
| 325 | } |
|---|
| 326 | }; |
|---|
| 327 | |
|---|
| 328 | |
|---|
| 329 | /** Getter for the playlist. **/ |
|---|
| 330 | public function get playlist():Array { |
|---|
| 331 | return playlister.playlist; |
|---|
| 332 | }; |
|---|
| 333 | |
|---|
| 334 | |
|---|
| 335 | } |
|---|
| 336 | |
|---|
| 337 | |
|---|
| 338 | } |
|---|