| 1 | package com.longtailvideo.jwplayer.media { |
|---|
| 2 | import com.longtailvideo.jwplayer.events.GlobalEventDispatcher; |
|---|
| 3 | import com.longtailvideo.jwplayer.events.IGlobalEventDispatcher; |
|---|
| 4 | import com.longtailvideo.jwplayer.events.MediaEvent; |
|---|
| 5 | import com.longtailvideo.jwplayer.events.PlayerStateEvent; |
|---|
| 6 | import com.longtailvideo.jwplayer.model.PlayerConfig; |
|---|
| 7 | import com.longtailvideo.jwplayer.model.PlaylistItem; |
|---|
| 8 | import com.longtailvideo.jwplayer.player.PlayerState; |
|---|
| 9 | import com.longtailvideo.jwplayer.utils.Stretcher; |
|---|
| 10 | |
|---|
| 11 | import flash.display.DisplayObject; |
|---|
| 12 | import flash.display.MovieClip; |
|---|
| 13 | import flash.display.Sprite; |
|---|
| 14 | import flash.events.Event; |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | /** |
|---|
| 18 | * Fired when a portion of the current media has been loaded into the buffer. |
|---|
| 19 | * |
|---|
| 20 | * @eventType com.longtailvideo.jwplayer.events.MediaEvent.JWPLAYER_MEDIA_BUFFER |
|---|
| 21 | */ |
|---|
| 22 | [Event(name="jwplayerMediaBuffer", type="com.longtailvideo.jwplayer.events.MediaEvent")] |
|---|
| 23 | /** |
|---|
| 24 | * Fired when the buffer is full. |
|---|
| 25 | * |
|---|
| 26 | * @eventType com.longtailvideo.jwplayer.events.MediaEvent.JWPLAYER_MEDIA_BUFFER_FULL |
|---|
| 27 | */ |
|---|
| 28 | [Event(name="jwplayerMediaBufferFull", type="com.longtailvideo.jwplayer.events.MediaEvent")] |
|---|
| 29 | /** |
|---|
| 30 | * Fired if an error occurs in the course of media playback. |
|---|
| 31 | * |
|---|
| 32 | * @eventType com.longtailvideo.jwplayer.events.MediaEvent.JWPLAYER_MEDIA_ERROR |
|---|
| 33 | */ |
|---|
| 34 | [Event(name="jwplayerMediaError", type="com.longtailvideo.jwplayer.events.MediaEvent")] |
|---|
| 35 | /** |
|---|
| 36 | * Fired after the MediaProvider has successfully set up a connection to the media. |
|---|
| 37 | * |
|---|
| 38 | * @eventType com.longtailvideo.jwplayer.events.MediaEvent.JWPLAYER_MEDIA_LOADED |
|---|
| 39 | */ |
|---|
| 40 | [Event(name="jwplayerMediaLoaded", type="com.longtailvideo.jwplayer.events.MediaEvent")] |
|---|
| 41 | /** |
|---|
| 42 | * Sends the position and duration of the currently playing media. |
|---|
| 43 | * |
|---|
| 44 | * @eventType com.longtailvideo.jwplayer.events.MediaEvent.JWPLAYER_MEDIA_TIME |
|---|
| 45 | */ |
|---|
| 46 | [Event(name="jwplayerMediaTime", type="com.longtailvideo.jwplayer.events.MediaEvent")] |
|---|
| 47 | /** |
|---|
| 48 | * Fired after a volume change. |
|---|
| 49 | * |
|---|
| 50 | * @eventType com.longtailvideo.jwplayer.events.MediaEvent.JWPLAYER_MEDIA_VOLUME |
|---|
| 51 | */ |
|---|
| 52 | [Event(name="jwplayerMediaVolume", type="com.longtailvideo.jwplayer.events.MediaEvent")] |
|---|
| 53 | /** |
|---|
| 54 | * Fired when the currently playing media has completed its playback. |
|---|
| 55 | * |
|---|
| 56 | * @eventType com.longtailvideo.jwplayer.events.MediaEvent.JWPLAYER_MEDIA_COMPLETE |
|---|
| 57 | */ |
|---|
| 58 | [Event(name="jwplayerMediaComplete", type="com.longtailvideo.jwplayer.events.MediaEvent")] |
|---|
| 59 | /** |
|---|
| 60 | * Sent when the playback state has changed. |
|---|
| 61 | * |
|---|
| 62 | * @eventType com.longtailvideo.jwplayer.events.PlayerStateEvent.JWPLAYER_PLAYER_STATE |
|---|
| 63 | */ |
|---|
| 64 | [Event(name="jwplayerPlayerState", type="com.longtailvideo.jwplayer.events.PlayerStateEvent")] |
|---|
| 65 | |
|---|
| 66 | public class MediaProvider extends Sprite implements IGlobalEventDispatcher { |
|---|
| 67 | /** Reference to the player configuration. **/ |
|---|
| 68 | private var _config:PlayerConfig; |
|---|
| 69 | /** Name of the MediaProvider **/ |
|---|
| 70 | private var _provider:String; |
|---|
| 71 | /** Reference to the currently active playlistitem. **/ |
|---|
| 72 | protected var _item:PlaylistItem; |
|---|
| 73 | /** The current position inside the file. **/ |
|---|
| 74 | protected var _position:Number; |
|---|
| 75 | /** The current volume of the audio output stream **/ |
|---|
| 76 | private var _volume:Number; |
|---|
| 77 | /** The playback state for the currently loaded media. @see com.longtailvideo.jwplayer.model.ModelStates **/ |
|---|
| 78 | private var _state:String; |
|---|
| 79 | /** Clip containing graphical representation of the currently playing media **/ |
|---|
| 80 | private var _media:MovieClip; |
|---|
| 81 | /** Most recent buffer data **/ |
|---|
| 82 | private var _bufferPercent:Number; |
|---|
| 83 | /** Handles event dispatching **/ |
|---|
| 84 | private var _dispatcher:GlobalEventDispatcher; |
|---|
| 85 | |
|---|
| 86 | protected var _width:Number; |
|---|
| 87 | protected var _height:Number; |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | public function MediaProvider(provider:String) { |
|---|
| 91 | _provider = provider; |
|---|
| 92 | _dispatcher = new GlobalEventDispatcher(); |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | public function initializeMediaProvider(cfg:PlayerConfig):void { |
|---|
| 97 | _config = cfg; |
|---|
| 98 | _state = PlayerState.IDLE; |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | /** |
|---|
| 103 | * Load a new playlist item |
|---|
| 104 | * @param itm The playlistItem to load |
|---|
| 105 | **/ |
|---|
| 106 | public function load(itm:PlaylistItem):void { |
|---|
| 107 | _item = itm; |
|---|
| 108 | dispatchEvent(new MediaEvent(MediaEvent.JWPLAYER_MEDIA_LOADED)); |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | /** Resume playback of the item. **/ |
|---|
| 113 | public function play():void { |
|---|
| 114 | setState(PlayerState.PLAYING); |
|---|
| 115 | if (_media) { |
|---|
| 116 | _media.visible = true; |
|---|
| 117 | } |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | /** Pause playback of the item. **/ |
|---|
| 122 | public function pause():void { |
|---|
| 123 | setState(PlayerState.PAUSED); |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | /** |
|---|
| 128 | * Seek to a certain position in the item. |
|---|
| 129 | * |
|---|
| 130 | * @param pos The position in seconds. |
|---|
| 131 | **/ |
|---|
| 132 | public function seek(pos:Number):void { |
|---|
| 133 | _position = pos; |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | /** Stop playing and loading the item. **/ |
|---|
| 138 | public function stop():void { |
|---|
| 139 | setState(PlayerState.IDLE); |
|---|
| 140 | _position = 0; |
|---|
| 141 | if (_media) { |
|---|
| 142 | _media.visible = false; |
|---|
| 143 | } |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | /** |
|---|
| 148 | * Change the playback volume of the item. |
|---|
| 149 | * |
|---|
| 150 | * @param vol The new volume (0 to 100). |
|---|
| 151 | **/ |
|---|
| 152 | public function setVolume(vol:Number):void { |
|---|
| 153 | sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_VOLUME, {'volume': vol}); |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | /** |
|---|
| 158 | * Changes the mute state of the item. |
|---|
| 159 | * |
|---|
| 160 | * @param mute The new mute state. |
|---|
| 161 | **/ |
|---|
| 162 | public function mute(mute:Boolean):void { |
|---|
| 163 | mute == true ? setVolume(0) : setVolume(_config.volume); |
|---|
| 164 | sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_MUTE, {'mute': mute}); |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | |
|---|
| 168 | /** |
|---|
| 169 | * Resizes the display. |
|---|
| 170 | * |
|---|
| 171 | * @param width The new width of the display. |
|---|
| 172 | * @param height The new height of the display. |
|---|
| 173 | **/ |
|---|
| 174 | public function resize(width:Number, height:Number):void { |
|---|
| 175 | _width = width; |
|---|
| 176 | _height = height; |
|---|
| 177 | if (_media) { |
|---|
| 178 | Stretcher.stretch(_media, width, height, _config.stretching); |
|---|
| 179 | } |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | /** Graphical representation of media **/ |
|---|
| 184 | public function get display():DisplayObject { |
|---|
| 185 | return _media; |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | |
|---|
| 189 | /** Name of the MediaProvider. */ |
|---|
| 190 | public function get provider():String { |
|---|
| 191 | return _provider; |
|---|
| 192 | } |
|---|
| 193 | |
|---|
| 194 | |
|---|
| 195 | /** |
|---|
| 196 | * Current state of the MediaProvider. |
|---|
| 197 | * @see PlayerStates |
|---|
| 198 | */ |
|---|
| 199 | public function get state():String { |
|---|
| 200 | return _state; |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | |
|---|
| 204 | /** Currently playing PlaylistItem **/ |
|---|
| 205 | public function get item():PlaylistItem { |
|---|
| 206 | return _item; |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | |
|---|
| 210 | /** Current position, in seconds **/ |
|---|
| 211 | public function get position():Number { |
|---|
| 212 | return _position; |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | |
|---|
| 216 | /** |
|---|
| 217 | * The current volume of the playing media |
|---|
| 218 | * <p>Range: 0-100</p> |
|---|
| 219 | */ |
|---|
| 220 | public function get volume():Number { |
|---|
| 221 | return _volume; |
|---|
| 222 | } |
|---|
| 223 | |
|---|
| 224 | |
|---|
| 225 | /** Puts the video into a buffer state **/ |
|---|
| 226 | protected function buffer():void { |
|---|
| 227 | |
|---|
| 228 | } |
|---|
| 229 | |
|---|
| 230 | |
|---|
| 231 | /** Completes video playback **/ |
|---|
| 232 | protected function complete():void { |
|---|
| 233 | stop(); |
|---|
| 234 | sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_COMPLETE); |
|---|
| 235 | } |
|---|
| 236 | |
|---|
| 237 | |
|---|
| 238 | /** Dispatches error notifications **/ |
|---|
| 239 | protected function error(message:String):void { |
|---|
| 240 | stop(); |
|---|
| 241 | sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_ERROR, {message: message}); |
|---|
| 242 | } |
|---|
| 243 | |
|---|
| 244 | |
|---|
| 245 | /** |
|---|
| 246 | * Sets the current state to a new state and sends a PlayerStateEvent |
|---|
| 247 | * @param newState A state from ModelStates. |
|---|
| 248 | */ |
|---|
| 249 | protected function setState(newState:String):void { |
|---|
| 250 | if (state != newState) { |
|---|
| 251 | var evt:PlayerStateEvent = new PlayerStateEvent(PlayerStateEvent.JWPLAYER_PLAYER_STATE, newState, state); |
|---|
| 252 | _state = newState; |
|---|
| 253 | dispatchEvent(evt); |
|---|
| 254 | } |
|---|
| 255 | } |
|---|
| 256 | |
|---|
| 257 | |
|---|
| 258 | /** |
|---|
| 259 | * Sends a MediaEvent, simultaneously setting a property |
|---|
| 260 | * @param type |
|---|
| 261 | * @param property |
|---|
| 262 | * @param value |
|---|
| 263 | */ |
|---|
| 264 | protected function sendMediaEvent(type:String, properties:Object=null):void { |
|---|
| 265 | var newEvent:MediaEvent = new MediaEvent(type); |
|---|
| 266 | for (var property:String in properties) { |
|---|
| 267 | if (newEvent.hasOwnProperty(property)) { |
|---|
| 268 | newEvent[property] = properties[property]; |
|---|
| 269 | } |
|---|
| 270 | } |
|---|
| 271 | dispatchEvent(newEvent); |
|---|
| 272 | } |
|---|
| 273 | |
|---|
| 274 | |
|---|
| 275 | /** Dispatches buffer change notifications **/ |
|---|
| 276 | protected function sendBufferEvent(bufferPercent:Number, offset:Number=0):void { |
|---|
| 277 | if (_bufferPercent != bufferPercent) { |
|---|
| 278 | _bufferPercent = bufferPercent; |
|---|
| 279 | sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_BUFFER, {'bufferPercent': _bufferPercent, |
|---|
| 280 | 'offset': offset, 'duration': _item.duration}); |
|---|
| 281 | } |
|---|
| 282 | } |
|---|
| 283 | |
|---|
| 284 | |
|---|
| 285 | /** |
|---|
| 286 | * The current config |
|---|
| 287 | */ |
|---|
| 288 | protected function get config():PlayerConfig { |
|---|
| 289 | return _config; |
|---|
| 290 | } |
|---|
| 291 | |
|---|
| 292 | |
|---|
| 293 | /** |
|---|
| 294 | * Gets a property from the player configuration |
|---|
| 295 | * |
|---|
| 296 | * @param property The property to be retrieved. |
|---|
| 297 | * **/ |
|---|
| 298 | protected function getConfigProperty(property:String):* { |
|---|
| 299 | if (item.hasOwnProperty(_provider + "." + property)) { |
|---|
| 300 | return item[_provider + "." + property]; |
|---|
| 301 | } else { |
|---|
| 302 | return _config.pluginConfig(provider)[property]; |
|---|
| 303 | } |
|---|
| 304 | } |
|---|
| 305 | |
|---|
| 306 | /** |
|---|
| 307 | * Gets the graphical representation of the media. |
|---|
| 308 | * |
|---|
| 309 | */ |
|---|
| 310 | protected function get media():DisplayObject { |
|---|
| 311 | return _media; |
|---|
| 312 | } |
|---|
| 313 | |
|---|
| 314 | |
|---|
| 315 | /** |
|---|
| 316 | * Sets the graphical representation of the media. |
|---|
| 317 | * |
|---|
| 318 | */ |
|---|
| 319 | protected function set media(m:DisplayObject):void { |
|---|
| 320 | if (m) { |
|---|
| 321 | _media = new MovieClip(); |
|---|
| 322 | _media.visible = false; |
|---|
| 323 | _media.addChild(m); |
|---|
| 324 | if (_width * _height > 0) { |
|---|
| 325 | Stretcher.stretch(_media, _width, _height, _config.stretching); |
|---|
| 326 | } |
|---|
| 327 | } else { |
|---|
| 328 | _media = null; |
|---|
| 329 | } |
|---|
| 330 | } |
|---|
| 331 | |
|---|
| 332 | |
|---|
| 333 | /////////////////////////////////////////// |
|---|
| 334 | /// IGlobalEventDispatcher implementation |
|---|
| 335 | /////////////////////////////////////////// |
|---|
| 336 | /** |
|---|
| 337 | * @inheritDoc |
|---|
| 338 | */ |
|---|
| 339 | public function addGlobalListener(listener:Function):void { |
|---|
| 340 | _dispatcher.addGlobalListener(listener); |
|---|
| 341 | } |
|---|
| 342 | |
|---|
| 343 | |
|---|
| 344 | /** |
|---|
| 345 | * @inheritDoc |
|---|
| 346 | */ |
|---|
| 347 | public function removeGlobalListener(listener:Function):void { |
|---|
| 348 | _dispatcher.removeGlobalListener(listener); |
|---|
| 349 | } |
|---|
| 350 | |
|---|
| 351 | |
|---|
| 352 | /** |
|---|
| 353 | * @inheritDoc |
|---|
| 354 | */ |
|---|
| 355 | public override function dispatchEvent(event:Event):Boolean { |
|---|
| 356 | _dispatcher.dispatchEvent(event); |
|---|
| 357 | return super.dispatchEvent(event); |
|---|
| 358 | } |
|---|
| 359 | } |
|---|
| 360 | } |
|---|