| 1 | package com.longtailvideo.jwplayer.player { |
|---|
| 2 | import com.longtailvideo.jwplayer.controller.Controller; |
|---|
| 3 | import com.longtailvideo.jwplayer.events.PlayerEvent; |
|---|
| 4 | import com.longtailvideo.jwplayer.model.IPlaylist; |
|---|
| 5 | import com.longtailvideo.jwplayer.model.Model; |
|---|
| 6 | import com.longtailvideo.jwplayer.model.PlayerConfig; |
|---|
| 7 | import com.longtailvideo.jwplayer.plugins.IPlugin; |
|---|
| 8 | import com.longtailvideo.jwplayer.utils.Logger; |
|---|
| 9 | import com.longtailvideo.jwplayer.utils.RootReference; |
|---|
| 10 | import com.longtailvideo.jwplayer.view.IPlayerComponents; |
|---|
| 11 | import com.longtailvideo.jwplayer.view.View; |
|---|
| 12 | import com.longtailvideo.jwplayer.view.interfaces.IPlayerComponent; |
|---|
| 13 | import com.longtailvideo.jwplayer.view.interfaces.ISkin; |
|---|
| 14 | |
|---|
| 15 | import flash.display.DisplayObject; |
|---|
| 16 | import flash.display.Sprite; |
|---|
| 17 | import flash.events.Event; |
|---|
| 18 | import flash.utils.setTimeout; |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | /** |
|---|
| 22 | * Sent when the player has been initialized and skins and plugins have been successfully loaded. |
|---|
| 23 | * |
|---|
| 24 | * @eventType com.longtailvideo.jwplayer.events.PlayerEvent.JWPLAYER_READY |
|---|
| 25 | */ |
|---|
| 26 | [Event(name="jwplayerReady", type="com.longtailvideo.jwplayer.events.PlayerEvent")] |
|---|
| 27 | /** |
|---|
| 28 | * Main class for JW Flash Media Player |
|---|
| 29 | * |
|---|
| 30 | * @author Pablo Schklowsky |
|---|
| 31 | */ |
|---|
| 32 | public class Player extends Sprite implements IPlayer { |
|---|
| 33 | protected var model:Model; |
|---|
| 34 | protected var view:View; |
|---|
| 35 | protected var controller:Controller; |
|---|
| 36 | |
|---|
| 37 | /** Player constructor **/ |
|---|
| 38 | public function Player() { |
|---|
| 39 | try { |
|---|
| 40 | this.addEventListener(Event.ADDED_TO_STAGE, setupPlayer); |
|---|
| 41 | } catch (err:Error) { |
|---|
| 42 | setupPlayer(); |
|---|
| 43 | } |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | protected function setupPlayer(event:Event=null):void { |
|---|
| 48 | try { |
|---|
| 49 | this.removeEventListener(Event.ADDED_TO_STAGE, setupPlayer); |
|---|
| 50 | } catch (err:Error) { |
|---|
| 51 | } |
|---|
| 52 | new RootReference(this); |
|---|
| 53 | model = newModel(); |
|---|
| 54 | view = newView(model); |
|---|
| 55 | controller = newController(model, view); |
|---|
| 56 | controller.addEventListener(PlayerEvent.JWPLAYER_READY, playerReady, false, -1); |
|---|
| 57 | controller.setupPlayer(); |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | protected function newModel():Model { |
|---|
| 61 | return new Model(); |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | protected function newView(mod:Model):View { |
|---|
| 65 | return new View(this, mod); |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | protected function newController(mod:Model, vw:View):Controller { |
|---|
| 69 | return new Controller(this, mod, vw); |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | protected function playerReady(evt:PlayerEvent):void { |
|---|
| 73 | // Only handle JWPLAYER_READY once |
|---|
| 74 | controller.removeEventListener(PlayerEvent.JWPLAYER_READY, playerReady); |
|---|
| 75 | |
|---|
| 76 | // Initialize Javascript interface |
|---|
| 77 | var jsAPI:JavascriptAPI = new JavascriptAPI(this); |
|---|
| 78 | |
|---|
| 79 | // Forward all MVC events |
|---|
| 80 | model.addGlobalListener(forward); |
|---|
| 81 | view.addGlobalListener(forward); |
|---|
| 82 | controller.addGlobalListener(forward); |
|---|
| 83 | |
|---|
| 84 | forward(evt); |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | /** |
|---|
| 89 | * Forwards all MVC events to interested listeners. |
|---|
| 90 | * @param evt |
|---|
| 91 | */ |
|---|
| 92 | protected function forward(evt:PlayerEvent):void { |
|---|
| 93 | Logger.log(evt.toString(), evt.type); |
|---|
| 94 | dispatchEvent(evt); |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | /** |
|---|
| 99 | * @inheritDoc |
|---|
| 100 | */ |
|---|
| 101 | public function get config():PlayerConfig { |
|---|
| 102 | return model.config; |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | /** |
|---|
| 107 | * @inheritDoc |
|---|
| 108 | */ |
|---|
| 109 | public function get version():String { |
|---|
| 110 | return PlayerVersion.version; |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | /** |
|---|
| 115 | * @inheritDoc |
|---|
| 116 | */ |
|---|
| 117 | public function get skin():ISkin { |
|---|
| 118 | return view.skin; |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | /** |
|---|
| 123 | * @inheritDoc |
|---|
| 124 | */ |
|---|
| 125 | public function get state():String { |
|---|
| 126 | return model.state; |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | /** |
|---|
| 131 | * @inheritDoc |
|---|
| 132 | */ |
|---|
| 133 | public function get playlist():IPlaylist { |
|---|
| 134 | return model.playlist; |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | /** |
|---|
| 139 | * @inheritDoc |
|---|
| 140 | */ |
|---|
| 141 | public function get locked():Boolean { |
|---|
| 142 | return controller.locking; |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | /** |
|---|
| 147 | * @inheritDoc |
|---|
| 148 | */ |
|---|
| 149 | public function lock(target:IPlugin, callback:Function):void { |
|---|
| 150 | controller.lockPlayback(target, callback); |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | /** |
|---|
| 155 | * @inheritDoc |
|---|
| 156 | */ |
|---|
| 157 | public function unlock(target:IPlugin):Boolean { |
|---|
| 158 | return controller.unlockPlayback(target); |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | /** |
|---|
| 163 | * @inheritDoc |
|---|
| 164 | */ |
|---|
| 165 | public function volume(volume:Number):Boolean { |
|---|
| 166 | return controller.setVolume(volume); |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | |
|---|
| 170 | /** |
|---|
| 171 | * @inheritDoc |
|---|
| 172 | */ |
|---|
| 173 | public function mute(state:Boolean):void { |
|---|
| 174 | controller.mute(state); |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | |
|---|
| 178 | /** |
|---|
| 179 | * @inheritDoc |
|---|
| 180 | */ |
|---|
| 181 | public function play():Boolean { |
|---|
| 182 | return controller.play(); |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | /** |
|---|
| 187 | * @inheritDoc |
|---|
| 188 | */ |
|---|
| 189 | public function pause():Boolean { |
|---|
| 190 | return controller.pause(); |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | |
|---|
| 194 | /** |
|---|
| 195 | * @inheritDoc |
|---|
| 196 | */ |
|---|
| 197 | public function stop():Boolean { |
|---|
| 198 | return controller.stop(); |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | |
|---|
| 202 | /** |
|---|
| 203 | * @inheritDoc |
|---|
| 204 | */ |
|---|
| 205 | public function seek(position:Number):Boolean { |
|---|
| 206 | return controller.seek(position); |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | |
|---|
| 210 | /** |
|---|
| 211 | * @inheritDoc |
|---|
| 212 | */ |
|---|
| 213 | public function load(item:*):Boolean { |
|---|
| 214 | return controller.load(item); |
|---|
| 215 | } |
|---|
| 216 | |
|---|
| 217 | |
|---|
| 218 | /** |
|---|
| 219 | * @inheritDoc |
|---|
| 220 | */ |
|---|
| 221 | public function playlistItem(index:Number):Boolean { |
|---|
| 222 | return controller.setPlaylistIndex(index); |
|---|
| 223 | } |
|---|
| 224 | |
|---|
| 225 | /** |
|---|
| 226 | * @inheritDoc |
|---|
| 227 | */ |
|---|
| 228 | public function playlistNext():Boolean { |
|---|
| 229 | return controller.next(); |
|---|
| 230 | } |
|---|
| 231 | |
|---|
| 232 | /** |
|---|
| 233 | * @inheritDoc |
|---|
| 234 | */ |
|---|
| 235 | public function playlistPrev():Boolean { |
|---|
| 236 | return controller.previous(); |
|---|
| 237 | } |
|---|
| 238 | |
|---|
| 239 | /** |
|---|
| 240 | * @inheritDoc |
|---|
| 241 | */ |
|---|
| 242 | public function redraw():Boolean { |
|---|
| 243 | return controller.redraw(); |
|---|
| 244 | } |
|---|
| 245 | |
|---|
| 246 | /** |
|---|
| 247 | * @inheritDoc |
|---|
| 248 | */ |
|---|
| 249 | public function fullscreen(on:Boolean):void { |
|---|
| 250 | controller.fullscreen(on); |
|---|
| 251 | } |
|---|
| 252 | |
|---|
| 253 | |
|---|
| 254 | /** |
|---|
| 255 | * @inheritDoc |
|---|
| 256 | */ |
|---|
| 257 | public function get controls():IPlayerComponents { |
|---|
| 258 | return view.components; |
|---|
| 259 | } |
|---|
| 260 | |
|---|
| 261 | /** |
|---|
| 262 | * @inheritDoc |
|---|
| 263 | */ |
|---|
| 264 | public function overrideComponent(plugin:IPlayerComponent):void { |
|---|
| 265 | view.overrideComponent(plugin); |
|---|
| 266 | } |
|---|
| 267 | |
|---|
| 268 | /** |
|---|
| 269 | * @private |
|---|
| 270 | * |
|---|
| 271 | * This method is deprecated, and is used for backwards compatibility only. |
|---|
| 272 | */ |
|---|
| 273 | public function getPlugin(id:String):Object { |
|---|
| 274 | return view.getPlugin(id); |
|---|
| 275 | } |
|---|
| 276 | |
|---|
| 277 | /** The player should not accept any calls referencing its display stack **/ |
|---|
| 278 | public override function addChild(child:DisplayObject):DisplayObject { |
|---|
| 279 | return null; |
|---|
| 280 | } |
|---|
| 281 | |
|---|
| 282 | /** The player should not accept any calls referencing its display stack **/ |
|---|
| 283 | public override function addChildAt(child:DisplayObject, index:int):DisplayObject { |
|---|
| 284 | return null; |
|---|
| 285 | } |
|---|
| 286 | |
|---|
| 287 | /** The player should not accept any calls referencing its display stack **/ |
|---|
| 288 | public override function removeChild(child:DisplayObject):DisplayObject { |
|---|
| 289 | return null; |
|---|
| 290 | } |
|---|
| 291 | |
|---|
| 292 | /** The player should not accept any calls referencing its display stack **/ |
|---|
| 293 | public override function removeChildAt(index:int):DisplayObject { |
|---|
| 294 | return null; |
|---|
| 295 | } |
|---|
| 296 | |
|---|
| 297 | } |
|---|
| 298 | } |
|---|