| 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.Model; |
|---|
| 5 | import com.longtailvideo.jwplayer.model.PlayerConfig; |
|---|
| 6 | import com.longtailvideo.jwplayer.model.Playlist; |
|---|
| 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.PlayerComponents; |
|---|
| 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.Sprite; |
|---|
| 16 | import flash.events.Event; |
|---|
| 17 | |
|---|
| 18 | /** |
|---|
| 19 | * Sent when the player has been initialized and skins and plugins have been successfully loaded. |
|---|
| 20 | * |
|---|
| 21 | * @eventType com.longtailvideo.jwplayer.events.PlayerEvent.JWPLAYER_READY |
|---|
| 22 | */ |
|---|
| 23 | [Event(name="jwplayerReady", type = "com.longtailvideo.jwplayer.events.PlayerEvent")] |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | /** |
|---|
| 27 | * Main class for JW Flash Media Player |
|---|
| 28 | * |
|---|
| 29 | * @author Pablo Schklowsky |
|---|
| 30 | */ |
|---|
| 31 | public class Player extends Sprite { |
|---|
| 32 | private static var playerVersion:String = "5.0.530 alpha"; |
|---|
| 33 | private static var _commercial:Boolean = Boolean(CONFIG::commercial); |
|---|
| 34 | |
|---|
| 35 | private var model:Model; |
|---|
| 36 | private var view:View; |
|---|
| 37 | private var controller:Controller; |
|---|
| 38 | |
|---|
| 39 | /** Player constructor **/ |
|---|
| 40 | public function Player() { |
|---|
| 41 | new RootReference(this); |
|---|
| 42 | |
|---|
| 43 | try { |
|---|
| 44 | this.addEventListener(Event.ADDED_TO_STAGE, setupPlayer); |
|---|
| 45 | } catch (err:Error) { |
|---|
| 46 | setupPlayer(); |
|---|
| 47 | } |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | private function setupPlayer(event:Event = null):void { |
|---|
| 51 | try { |
|---|
| 52 | this.removeEventListener(Event.ADDED_TO_STAGE, setupPlayer); |
|---|
| 53 | } catch (err:Error) { |
|---|
| 54 | } |
|---|
| 55 | model = new Model(); |
|---|
| 56 | view = new View(this, model); |
|---|
| 57 | controller = new Controller(this, model, view); |
|---|
| 58 | |
|---|
| 59 | controller.addEventListener(PlayerEvent.JWPLAYER_READY, playerReady); |
|---|
| 60 | |
|---|
| 61 | controller.setupPlayer(); |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | protected function playerReady(evt:PlayerEvent):void { |
|---|
| 65 | // Only handle JWPLAYER_READY once |
|---|
| 66 | controller.removeEventListener(PlayerEvent.JWPLAYER_READY, playerReady); |
|---|
| 67 | |
|---|
| 68 | var jsAPI:JavascriptAPI = new JavascriptAPI(this); |
|---|
| 69 | |
|---|
| 70 | model.addGlobalListener(forward); |
|---|
| 71 | view.addGlobalListener(forward); |
|---|
| 72 | controller.addGlobalListener(forward); |
|---|
| 73 | |
|---|
| 74 | forward(evt); |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | /** |
|---|
| 78 | * Forwards all MVC events to interested listeners. |
|---|
| 79 | * @param evt |
|---|
| 80 | */ |
|---|
| 81 | protected function forward(evt:PlayerEvent):void { |
|---|
| 82 | Logger.log(evt.toString(), evt.type); |
|---|
| 83 | dispatchEvent(evt); |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | /** |
|---|
| 87 | * The player's current configuration |
|---|
| 88 | */ |
|---|
| 89 | public function get config():PlayerConfig { |
|---|
| 90 | return model.config; |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | /** |
|---|
| 94 | * Player version getter |
|---|
| 95 | */ |
|---|
| 96 | public static function get version():String { |
|---|
| 97 | return playerVersion; |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | /** |
|---|
| 101 | * Player type getter |
|---|
| 102 | */ |
|---|
| 103 | public static function get commercial():Boolean { |
|---|
| 104 | return _commercial; |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | /** |
|---|
| 108 | * Reference to player's skin. If no skin has been loaded, returns null. |
|---|
| 109 | */ |
|---|
| 110 | public function get skin():ISkin { |
|---|
| 111 | return view.skin; |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | /** |
|---|
| 115 | * The current player state |
|---|
| 116 | */ |
|---|
| 117 | public function get state():String { |
|---|
| 118 | return model.state; |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | /** |
|---|
| 122 | * The player's playlist |
|---|
| 123 | */ |
|---|
| 124 | public function get playlist():Playlist { |
|---|
| 125 | return model.playlist; |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | /** |
|---|
| 129 | * Set to true when the player is blocking playback. |
|---|
| 130 | */ |
|---|
| 131 | public function get isBlocking():Boolean { |
|---|
| 132 | return controller.blocking; |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | /** |
|---|
| 136 | * Request that the player block playback. When the Player is blocking, the currently playing stream is |
|---|
| 137 | * paused, and no new playback-related commands will be honored until <code>unblockPlayback</code> is |
|---|
| 138 | * called. |
|---|
| 139 | * |
|---|
| 140 | * @param target Reference to plugin requesting playback blocking |
|---|
| 141 | * @return <code>true</code>, if the blocking request is successful. If another plugin is blocking,returns |
|---|
| 142 | * <code>false</code>. |
|---|
| 143 | */ |
|---|
| 144 | public function blockPlayback(target:IPlugin):Boolean { |
|---|
| 145 | return controller.blockPlayback(target); |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | /** |
|---|
| 149 | * Unblocks the player. If the player was buffering or playing when it was blocked, playback will resume. |
|---|
| 150 | * |
|---|
| 151 | * @param target Reference to the requesting plugin. |
|---|
| 152 | * @return <code>true</code>, if <code>target</code> had previously requested player blocking. |
|---|
| 153 | * |
|---|
| 154 | */ |
|---|
| 155 | public function unblockPlayback(target:IPlugin):Boolean { |
|---|
| 156 | return controller.unblockPlayback(target); |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | public function volume(volume:Number):Boolean { |
|---|
| 160 | return controller.setVolume(volume); |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | public function get mute():Boolean{ |
|---|
| 164 | return model.mute; |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | public function set mute(state:Boolean):void { |
|---|
| 168 | controller.mute(state); |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | public function play():Boolean { |
|---|
| 172 | return controller.play(); |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | public function pause():Boolean { |
|---|
| 176 | return controller.pause(); |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | public function stop():Boolean { |
|---|
| 180 | return controller.stop(); |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | public function seek(position:Number):Boolean { |
|---|
| 184 | return controller.seek(position); |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | public function load(item:*):Boolean { |
|---|
| 188 | return controller.load(item); |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | public function playlistItem(index:Number):Boolean { |
|---|
| 192 | return controller.setPlaylistIndex(index); |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | public function playlistNext():Boolean { |
|---|
| 196 | return controller.next(); |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | public function playlistPrev():Boolean { |
|---|
| 200 | return controller.previous(); |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | /** Force a redraw of the player **/ |
|---|
| 204 | public function redraw():Boolean { |
|---|
| 205 | return controller.redraw(); |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | public function get fullscreen():Boolean { |
|---|
| 209 | return model.fullscreen; |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | public function set fullscreen(on:Boolean):void { |
|---|
| 213 | controller.fullscreen(on); |
|---|
| 214 | } |
|---|
| 215 | |
|---|
| 216 | public function link(index:Number=NaN):Boolean { |
|---|
| 217 | return controller.link(index); |
|---|
| 218 | } |
|---|
| 219 | |
|---|
| 220 | public function get controls():PlayerComponents { |
|---|
| 221 | return view.components; |
|---|
| 222 | } |
|---|
| 223 | |
|---|
| 224 | public function overrideComponent(plugin:IPlayerComponent):void { |
|---|
| 225 | view.overrideComponent(plugin); |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | } |
|---|
| 229 | } |
|---|