| 1 | package com.longtailvideo.jwplayer.view.components { |
|---|
| 2 | import com.longtailvideo.jwplayer.events.*; |
|---|
| 3 | import com.longtailvideo.jwplayer.model.*; |
|---|
| 4 | import com.longtailvideo.jwplayer.player.*; |
|---|
| 5 | import com.longtailvideo.jwplayer.utils.*; |
|---|
| 6 | import com.longtailvideo.jwplayer.view.interfaces.*; |
|---|
| 7 | |
|---|
| 8 | import flash.display.*; |
|---|
| 9 | import flash.events.*; |
|---|
| 10 | import flash.external.ExternalInterface; |
|---|
| 11 | import flash.geom.*; |
|---|
| 12 | import flash.net.*; |
|---|
| 13 | import flash.text.*; |
|---|
| 14 | import flash.utils.*; |
|---|
| 15 | |
|---|
| 16 | /** |
|---|
| 17 | * Sent when the display icon begins to become visible |
|---|
| 18 | * |
|---|
| 19 | * @eventType com.longtailvideo.jwplayer.events.ComponentEvent.JWPLAYER_COMPONENT_SHOW |
|---|
| 20 | */ |
|---|
| 21 | [Event(name="jwPlayerComponentShow", type="com.longtailvideo.jwplayer.events.ComponentEvent")] |
|---|
| 22 | /** |
|---|
| 23 | * Sent when the display icon begins to hide |
|---|
| 24 | * |
|---|
| 25 | * @eventType com.longtailvideo.jwplayer.events.ComponentEvent.JWPLAYER_COMPONENT_HIDE |
|---|
| 26 | */ |
|---|
| 27 | [Event(name="jwPlayerComponentHide", type="com.longtailvideo.jwplayer.events.ComponentEvent")] |
|---|
| 28 | |
|---|
| 29 | public class DisplayComponent extends CoreComponent implements IDisplayComponent { |
|---|
| 30 | protected var _icon:DisplayIcon; |
|---|
| 31 | protected var _iconArea:Rectangle; |
|---|
| 32 | protected var _background:MovieClip; |
|---|
| 33 | protected var _overlay:Sprite; |
|---|
| 34 | protected var _icons:Object; |
|---|
| 35 | protected var _youtubeMask:MovieClip; |
|---|
| 36 | |
|---|
| 37 | protected var _bufferStateTimer:Timer; |
|---|
| 38 | protected var _playStateTimer:Timer; |
|---|
| 39 | protected var _previousState:String; |
|---|
| 40 | protected var _textFormat:TextFormat; |
|---|
| 41 | protected var _textOverFormat:TextFormat; |
|---|
| 42 | |
|---|
| 43 | protected var _errorState:Boolean = false; |
|---|
| 44 | |
|---|
| 45 | /** Setting defaults **/ |
|---|
| 46 | protected var _bufferRotationTime:Number = 100; |
|---|
| 47 | protected var _bufferRotationAngle:Number = 15; |
|---|
| 48 | |
|---|
| 49 | protected var _forced:String = ""; |
|---|
| 50 | |
|---|
| 51 | public function DisplayComponent(player:IPlayer) { |
|---|
| 52 | super(player, "display"); |
|---|
| 53 | addListeners(); |
|---|
| 54 | setupDisplayObjects(); |
|---|
| 55 | setupIcons(); |
|---|
| 56 | |
|---|
| 57 | // Override defaults |
|---|
| 58 | if (!isNaN(getConfigParam('bufferRotation'))) _bufferRotationAngle = Number(getConfigParam('bufferrotation')); |
|---|
| 59 | if (!isNaN(getConfigParam('bufferInterval'))) _bufferRotationTime = Number(getConfigParam('bufferinterval')); |
|---|
| 60 | |
|---|
| 61 | _bufferStateTimer = new Timer(50, 1); |
|---|
| 62 | _bufferStateTimer.addEventListener(TimerEvent.TIMER_COMPLETE, showBufferIcon); |
|---|
| 63 | |
|---|
| 64 | _playStateTimer = new Timer(50, 1); |
|---|
| 65 | _playStateTimer.addEventListener(TimerEvent.TIMER_COMPLETE, showPlayIcon); |
|---|
| 66 | |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | private function itemHandler(evt:PlaylistEvent):void { |
|---|
| 71 | _playStateTimer.delay = (_icon ? 10 : 50); |
|---|
| 72 | _playStateTimer.start(); |
|---|
| 73 | if (background) { |
|---|
| 74 | if (_player.playlist.currentItem && _player.playlist.currentItem.provider == "youtube") { |
|---|
| 75 | background.mask = _youtubeMask; |
|---|
| 76 | } else { |
|---|
| 77 | background.mask = null; |
|---|
| 78 | } |
|---|
| 79 | } |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | private function addListeners():void { |
|---|
| 84 | player.addEventListener(PlayerStateEvent.JWPLAYER_PLAYER_STATE, stateHandler); |
|---|
| 85 | player.addEventListener(PlayerEvent.JWPLAYER_ERROR, errorHandler); |
|---|
| 86 | player.addEventListener(PlaylistEvent.JWPLAYER_PLAYLIST_ITEM, itemHandler); |
|---|
| 87 | addEventListener(MouseEvent.CLICK, clickHandler); |
|---|
| 88 | this.buttonMode = true; |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | private function setupDisplayObjects():void { |
|---|
| 93 | _background = new MovieClip(); |
|---|
| 94 | background.name = "background"; |
|---|
| 95 | addChildAt(background, 0); |
|---|
| 96 | background.graphics.beginFill(0, 0); |
|---|
| 97 | background.graphics.drawRect(0, 0, 1, 1); |
|---|
| 98 | background.graphics.endFill(); |
|---|
| 99 | |
|---|
| 100 | _overlay = new Sprite(); |
|---|
| 101 | _overlay.name = "overlay"; |
|---|
| 102 | addChildAt(_overlay, 1); |
|---|
| 103 | |
|---|
| 104 | _textFormat = new TextFormat("_sans", fontSize ? fontSize : 15, fontColor ? fontColor.toString() : 0, (fontWeight == "bold")); |
|---|
| 105 | _textOverFormat = new TextFormat("_sans", fontSize ? fontSize : 15, fontOverColor ? fontOverColor.toString() : 0, (fontWeight == "bold")); |
|---|
| 106 | |
|---|
| 107 | _youtubeMask = new MovieClip(); |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | protected function setupIcons():void { |
|---|
| 112 | _icons = {}; |
|---|
| 113 | setupIcon('buffer'); |
|---|
| 114 | setupIcon('play'); |
|---|
| 115 | setupIcon('error'); |
|---|
| 116 | setupIcon('replay'); |
|---|
| 117 | if (!_icons.replay) { |
|---|
| 118 | _icons.replay = _icons.play; |
|---|
| 119 | } |
|---|
| 120 | if (!_icons.error) { |
|---|
| 121 | _icons.error = setupIcon(); |
|---|
| 122 | } |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | protected function setupIcon(name:String=null):DisplayIcon { |
|---|
| 127 | if (!name) { |
|---|
| 128 | return new DisplayIcon({ |
|---|
| 129 | background: getSkinElement("background"), |
|---|
| 130 | backgroundOver: getSkinElement("backgroundOver"), |
|---|
| 131 | capLeft: getSkinElement("capLeft"), |
|---|
| 132 | capLeftOver: getSkinElement("capLeftOver"), |
|---|
| 133 | capRight: getSkinElement("capRight"), |
|---|
| 134 | capRightOver: getSkinElement("capRightOver") |
|---|
| 135 | }, _textFormat, _textOverFormat); |
|---|
| 136 | } else { |
|---|
| 137 | if (!getSkinElement(name+"Icon")) { return null; } |
|---|
| 138 | |
|---|
| 139 | var newIcon:DisplayIcon = new DisplayIcon({ |
|---|
| 140 | icon: getSkinElement(name+"Icon"), |
|---|
| 141 | iconOver: getSkinElement(name+"IconOver"), |
|---|
| 142 | background: getSkinElement("background"), |
|---|
| 143 | backgroundOver: getSkinElement("backgroundOver"), |
|---|
| 144 | capLeft: getSkinElement("capLeft"), |
|---|
| 145 | capLeftOver: getSkinElement("capLeftOver"), |
|---|
| 146 | capRight: getSkinElement("capRight"), |
|---|
| 147 | capRightOver: getSkinElement("capRightOver") |
|---|
| 148 | }, _textFormat, _textOverFormat); |
|---|
| 149 | } |
|---|
| 150 | _icons[name] = newIcon; |
|---|
| 151 | return newIcon; |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | override public function resize(width:Number, height:Number):void { |
|---|
| 155 | _background.width = width; |
|---|
| 156 | _background.height = height; |
|---|
| 157 | |
|---|
| 158 | _youtubeMask.graphics.clear(); |
|---|
| 159 | _youtubeMask.graphics.beginFill(0x00AA00, 0.3); |
|---|
| 160 | _youtubeMask.graphics.drawRect(0, 0, width, height - 100); |
|---|
| 161 | _youtubeMask.graphics.endFill(); |
|---|
| 162 | |
|---|
| 163 | positionIcon(); |
|---|
| 164 | stateHandler(); |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | |
|---|
| 168 | protected function setIcon(displayIcon:DisplayIcon):void { |
|---|
| 169 | var sendShowEvent:Boolean = false; |
|---|
| 170 | var sendHideEvent:Boolean = false; |
|---|
| 171 | try { |
|---|
| 172 | if (_icon && _icon.parent == _overlay) { |
|---|
| 173 | _overlay.removeChild(_icon); |
|---|
| 174 | _icon = null; |
|---|
| 175 | sendHideEvent = !_hiding; |
|---|
| 176 | } else { |
|---|
| 177 | sendShowEvent = !_hiding; |
|---|
| 178 | } |
|---|
| 179 | } catch (err:Error) { |
|---|
| 180 | } |
|---|
| 181 | if (_fullscreen != _player.config.fullscreen) { |
|---|
| 182 | _fullscreen = _player.config.fullscreen; |
|---|
| 183 | sendShowEvent = true; |
|---|
| 184 | } |
|---|
| 185 | if (displayIcon && _player.config.icons && (getConfigParam("icons") === true || typeof(getConfigParam("icons")) == "undefined")) { |
|---|
| 186 | _icon = displayIcon; |
|---|
| 187 | _overlay.addChild(_icon); |
|---|
| 188 | positionIcon(); |
|---|
| 189 | _iconArea = _icon.getRect(_overlay); |
|---|
| 190 | |
|---|
| 191 | if (sendShowEvent) { |
|---|
| 192 | sendShow(); |
|---|
| 193 | } |
|---|
| 194 | } else { |
|---|
| 195 | if (sendHideEvent) { |
|---|
| 196 | sendHide(); |
|---|
| 197 | } |
|---|
| 198 | _iconArea = null; |
|---|
| 199 | } |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | |
|---|
| 203 | private function positionIcon():void { |
|---|
| 204 | if (_icon) { |
|---|
| 205 | _icon.x = Math.round((background.width - _icon.width) / 2); |
|---|
| 206 | _icon.y = Math.round((background.height - _icon.height) / 2); |
|---|
| 207 | } |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | protected function setDisplay(displayIcon:DisplayIcon, displayText:String = null):void { |
|---|
| 211 | if (displayIcon) displayIcon.text = displayText; |
|---|
| 212 | setIcon(displayIcon); |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | |
|---|
| 216 | protected function clearDisplay():void { |
|---|
| 217 | setDisplay(null, ''); |
|---|
| 218 | } |
|---|
| 219 | |
|---|
| 220 | protected function get currentState():String { |
|---|
| 221 | return (_forced ? _forced : (_player ? _player.state : PlayerState.IDLE)); |
|---|
| 222 | } |
|---|
| 223 | |
|---|
| 224 | protected function stateHandler(event:PlayerEvent = null):void { |
|---|
| 225 | if (_previousState != currentState || !(event is PlayerStateEvent)) { |
|---|
| 226 | _previousState = currentState; |
|---|
| 227 | clearRotation(); |
|---|
| 228 | _bufferStateTimer.reset(); |
|---|
| 229 | _playStateTimer.reset(); |
|---|
| 230 | _bufferStateTimer.delay = (_icon ? 10 : 200); |
|---|
| 231 | _playStateTimer.delay = (_icon ? 10 : 10); |
|---|
| 232 | switch (currentState) { |
|---|
| 233 | case PlayerState.BUFFERING: |
|---|
| 234 | _errorState = false; |
|---|
| 235 | _bufferStateTimer.start(); |
|---|
| 236 | break; |
|---|
| 237 | case PlayerState.PAUSED: |
|---|
| 238 | case PlayerState.IDLE: |
|---|
| 239 | _playStateTimer.start(); |
|---|
| 240 | break; |
|---|
| 241 | case PlayerState.COMPLETED: |
|---|
| 242 | setDisplay(getIcon('replay')); |
|---|
| 243 | break; |
|---|
| 244 | default: |
|---|
| 245 | clearDisplay(); |
|---|
| 246 | } |
|---|
| 247 | } |
|---|
| 248 | } |
|---|
| 249 | |
|---|
| 250 | protected function showBufferIcon(evt:TimerEvent):void { |
|---|
| 251 | var icon:DisplayIcon = getIcon('buffer'); |
|---|
| 252 | setDisplay(icon, ''); |
|---|
| 253 | icon.setRotation(_bufferRotationAngle, _bufferRotationTime); |
|---|
| 254 | |
|---|
| 255 | } |
|---|
| 256 | |
|---|
| 257 | protected function showPlayIcon(evt:TimerEvent):void { |
|---|
| 258 | if (_errorState) return; |
|---|
| 259 | |
|---|
| 260 | var icon:DisplayIcon = getIcon('play'); |
|---|
| 261 | if (_player.state == PlayerState.IDLE) { |
|---|
| 262 | setDisplay(icon, _player.playlist.currentItem ? _player.playlist.currentItem.title : ""); |
|---|
| 263 | } else { |
|---|
| 264 | setDisplay(icon); |
|---|
| 265 | } |
|---|
| 266 | } |
|---|
| 267 | |
|---|
| 268 | protected function clearRotation():void { |
|---|
| 269 | getIcon('buffer').setRotation(0); |
|---|
| 270 | } |
|---|
| 271 | |
|---|
| 272 | |
|---|
| 273 | protected function errorHandler(event:PlayerEvent):void { |
|---|
| 274 | setDisplay(getIcon('error'), event.message); |
|---|
| 275 | _errorState = true; |
|---|
| 276 | } |
|---|
| 277 | |
|---|
| 278 | |
|---|
| 279 | protected function clickHandler(event:MouseEvent):void { |
|---|
| 280 | dispatchEvent(new ViewEvent(ViewEvent.JWPLAYER_VIEW_CLICK)); |
|---|
| 281 | if (currentState == PlayerState.PLAYING || currentState == PlayerState.BUFFERING) { |
|---|
| 282 | dispatchEvent(new ViewEvent(ViewEvent.JWPLAYER_VIEW_PAUSE)); |
|---|
| 283 | } else { |
|---|
| 284 | dispatchEvent(new ViewEvent(ViewEvent.JWPLAYER_VIEW_PLAY)); |
|---|
| 285 | } |
|---|
| 286 | } |
|---|
| 287 | |
|---|
| 288 | |
|---|
| 289 | protected function get background():MovieClip { |
|---|
| 290 | return _background; |
|---|
| 291 | } |
|---|
| 292 | |
|---|
| 293 | |
|---|
| 294 | /** Hide the display icon **/ |
|---|
| 295 | public override function hide():void { |
|---|
| 296 | if (_overlay) { |
|---|
| 297 | _overlay.visible = false; |
|---|
| 298 | } |
|---|
| 299 | if (!_hiding) { |
|---|
| 300 | if (_icon) { |
|---|
| 301 | sendHide(); |
|---|
| 302 | } |
|---|
| 303 | _hiding = true; |
|---|
| 304 | } |
|---|
| 305 | } |
|---|
| 306 | |
|---|
| 307 | /** Show the display icon **/ |
|---|
| 308 | public override function show():void { |
|---|
| 309 | if (_overlay) { |
|---|
| 310 | _overlay.visible = true; |
|---|
| 311 | } |
|---|
| 312 | if (_hiding) { |
|---|
| 313 | if (_icon) { |
|---|
| 314 | sendShow(); |
|---|
| 315 | } |
|---|
| 316 | _hiding = false; |
|---|
| 317 | } |
|---|
| 318 | } |
|---|
| 319 | |
|---|
| 320 | public function forceState(forcedState:String):void { |
|---|
| 321 | switch(forcedState) { |
|---|
| 322 | case PlayerState.BUFFERING: |
|---|
| 323 | case PlayerState.PAUSED: |
|---|
| 324 | case PlayerState.IDLE: |
|---|
| 325 | case PlayerState.PLAYING: |
|---|
| 326 | _forced = forcedState; |
|---|
| 327 | stateHandler(); |
|---|
| 328 | break; |
|---|
| 329 | default: |
|---|
| 330 | _forced = ""; |
|---|
| 331 | } |
|---|
| 332 | |
|---|
| 333 | } |
|---|
| 334 | |
|---|
| 335 | public function releaseState():void { |
|---|
| 336 | _forced = ""; |
|---|
| 337 | stateHandler(); |
|---|
| 338 | } |
|---|
| 339 | |
|---|
| 340 | protected override function get displayRect():Rectangle { |
|---|
| 341 | return _iconArea ? _iconArea : super.displayRect; |
|---|
| 342 | } |
|---|
| 343 | |
|---|
| 344 | protected function getIcon(name:String):DisplayIcon { |
|---|
| 345 | return _icons[name]; |
|---|
| 346 | } |
|---|
| 347 | |
|---|
| 348 | } |
|---|
| 349 | } |
|---|