PageRenderTime 142ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 1ms

/public/vendor/npm/webshim@1.15.8/swfs/jaris/src/jaris/player/newcontrols/NewControls.hx

https://github.com/yeban/sequenceserver
Haxe | 936 lines | 602 code | 131 blank | 203 comment | 52 complexity | f231b9422b800deced6dccb51b545627 MD5 | raw file
  1. /**
  2. * @author Jefferson González
  3. * @copyright 2010 Jefferson González
  4. *
  5. * @license
  6. * This file is part of Jaris FLV Player.
  7. *
  8. * Jaris FLV Player is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License or GNU LESSER GENERAL
  10. * PUBLIC LICENSE as published by the Free Software Foundation, either version
  11. * 3 of the License, or (at your option) any later version.
  12. *
  13. * Jaris FLV Player is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License and
  19. * GNU LESSER GENERAL PUBLIC LICENSE along with Jaris FLV Player. If not,
  20. * see <http://www.gnu.org/licenses/>.
  21. */
  22. package jaris.player.newcontrols;
  23. //{Libraries
  24. import flash.display.GradientType;
  25. import flash.events.Event;
  26. import flash.events.TimerEvent;
  27. import flash.geom.Matrix;
  28. import flash.Lib;
  29. import flash.events.MouseEvent;
  30. import flash.display.MovieClip;
  31. import flash.net.NetStream;
  32. import flash.geom.Rectangle;
  33. import flash.text.AntiAliasType;
  34. import flash.text.TextField;
  35. import flash.text.TextFieldAutoSize;
  36. import flash.text.TextFormat;
  37. import flash.utils.Timer;
  38. import jaris.animation.Animation;
  39. import jaris.events.PlayerEvents;
  40. import jaris.player.newcontrols.Loader;
  41. import jaris.player.newcontrols.FullscreenIcon;
  42. import jaris.player.newcontrols.PauseIcon;
  43. import jaris.player.newcontrols.PlayIcon;
  44. import jaris.player.newcontrols.VolumeIcon;
  45. import jaris.player.Player;
  46. import flash.display.Sprite;
  47. import flash.display.Stage;
  48. import jaris.utils.Utils;
  49. //}
  50. /**
  51. * Default controls for jaris player
  52. */
  53. class NewControls extends MovieClip {
  54. //{Member Variables
  55. private var _thumb:Sprite;
  56. private var _track:Sprite;
  57. private var _trackDownloaded:Sprite;
  58. private var _scrubbing:Bool;
  59. private var _stage:Stage;
  60. private var _movieClip:MovieClip;
  61. private var _player:Player;
  62. private var _darkColor:UInt;
  63. private var _brightColor:UInt;
  64. private var _seekColor:UInt;
  65. private var _controlColor:UInt;
  66. private var _controlSize:Int;
  67. private var _hoverColor:UInt;
  68. private var _hideControlsTimer:Timer;
  69. private var _hideAspectRatioLabelTimer:Timer;
  70. private var _currentPlayTimeLabel:TextField;
  71. private var _totalPlayTimeLabel:TextField;
  72. private var _percentLoaded:Float;
  73. private var _controlsVisible:Bool;
  74. private var _seekBar:Sprite;
  75. public var _controlsBar:Sprite;
  76. private var _playControl:PlayIcon;
  77. private var _pauseControl:PauseIcon;
  78. private var _fullscreenControl:FullscreenIcon;
  79. private var _volumeIcon:VolumeIcon;
  80. private var _volumeTrack:Sprite;
  81. private var _volumeSlider:Sprite;
  82. private var _loader:Loader;
  83. private var _aspectRatioLabelContainer:Sprite;
  84. private var _aspectRatioLabel:TextField;
  85. private var _textFormat:TextFormat;
  86. private var _forceHideControls:Bool;
  87. private var _shouldBeVisible:Bool;
  88. private var _nodeName:String;
  89. private var _showLoader:Bool;
  90. //}
  91. //{Constructor
  92. public function new(player:Player, forceHideControls, nodeName, showLoader )
  93. {
  94. super();
  95. //{Main variables
  96. _nodeName = nodeName;
  97. _stage = Lib.current.stage;
  98. _movieClip = Lib.current;
  99. _player = player;
  100. _darkColor = 0x000000;
  101. _brightColor = 0x4c4c4c;
  102. _controlColor = 0xFFFFFF;
  103. _hoverColor = 0x67A8C1;
  104. _seekColor = 0x7c7c7c;
  105. _controlSize = 40;
  106. _percentLoaded = 0.0;
  107. _hideControlsTimer = new Timer(500);
  108. _hideAspectRatioLabelTimer = new Timer(500);
  109. _controlsVisible = false;
  110. _showLoader = showLoader;
  111. _textFormat = new TextFormat();
  112. _textFormat.font = "arial";
  113. _textFormat.color = _controlColor;
  114. _textFormat.size = 14;
  115. //}
  116. //{Playing controls initialization
  117. _controlsBar = new Sprite();
  118. _controlsBar.visible = true;
  119. addChild(_controlsBar);
  120. _playControl = new PlayIcon(0, 0, 0, 0, _controlColor, _hoverColor);
  121. _controlsBar.addChild(_playControl);
  122. _pauseControl = new PauseIcon(0, 0, 0, 0, _controlColor, _hoverColor);
  123. _pauseControl.visible = false;
  124. _controlsBar.addChild(_pauseControl);
  125. if(_nodeName == 'video'){
  126. _fullscreenControl = new FullscreenIcon(0, 0, 0, 0, _controlColor, _hoverColor);
  127. _controlsBar.addChild(_fullscreenControl);
  128. }
  129. _volumeIcon = new VolumeIcon(0, 0, 0, 0, _controlColor, _hoverColor);
  130. _controlsBar.addChild(_volumeIcon);
  131. _volumeSlider = new Sprite();
  132. _volumeSlider.visible = false;
  133. _controlsBar.addChild(_volumeSlider);
  134. _volumeTrack = new Sprite();
  135. _volumeTrack.visible = false;
  136. _volumeTrack.buttonMode = true;
  137. _volumeTrack.useHandCursor = true;
  138. _volumeTrack.tabEnabled = false;
  139. _controlsBar.addChild(_volumeTrack);
  140. //}
  141. //{Seeking Controls initialization
  142. _seekBar = new Sprite();
  143. _controlsBar.addChild(_seekBar);
  144. _trackDownloaded = new Sprite( );
  145. _trackDownloaded.tabEnabled = false;
  146. _seekBar.addChild(_trackDownloaded);
  147. _track = new Sprite( );
  148. _track.tabEnabled = false;
  149. _track.buttonMode = true;
  150. _track.useHandCursor = true;
  151. _seekBar.addChild(_track);
  152. _thumb = new Sprite( );
  153. _thumb.buttonMode = true;
  154. _thumb.useHandCursor = true;
  155. _thumb.tabEnabled = false;
  156. _seekBar.addChild(_thumb);
  157. _currentPlayTimeLabel = new TextField();
  158. _currentPlayTimeLabel.autoSize = TextFieldAutoSize.LEFT;
  159. _currentPlayTimeLabel.text = "00:00:00";
  160. _currentPlayTimeLabel.tabEnabled = false;
  161. _currentPlayTimeLabel.setTextFormat(_textFormat);
  162. _seekBar.addChild(_currentPlayTimeLabel);
  163. _totalPlayTimeLabel = new TextField();
  164. _totalPlayTimeLabel.autoSize = TextFieldAutoSize.LEFT;
  165. _totalPlayTimeLabel.text = "00:00:00";
  166. _totalPlayTimeLabel.tabEnabled = false;
  167. _totalPlayTimeLabel.setTextFormat(_textFormat);
  168. _seekBar.addChild(_totalPlayTimeLabel);
  169. //}
  170. //{Aspect ratio label
  171. _aspectRatioLabelContainer = new Sprite();
  172. addChild(_aspectRatioLabelContainer);
  173. _aspectRatioLabel = new TextField();
  174. _aspectRatioLabel.autoSize = TextFieldAutoSize.CENTER;
  175. _aspectRatioLabel.text = "original";
  176. _aspectRatioLabel.tabEnabled = false;
  177. _aspectRatioLabelContainer.addChild(_aspectRatioLabel);
  178. //}
  179. redrawControls();
  180. _forceHideControls = forceHideControls == "false";
  181. //{Loader bar
  182. if ( _forceHideControls && _showLoader || !_forceHideControls ) {
  183. _loader = new Loader();
  184. _loader.hide();
  185. var loaderColors:Array <String> = ["", "", "", ""];
  186. loaderColors[0] = Std.string(_darkColor);
  187. loaderColors[1] = Std.string(_controlColor);
  188. loaderColors[2] = Std.string(_seekColor);
  189. _loader.setColors(loaderColors);
  190. addChild(_loader);
  191. }
  192. //}
  193. //{event Listeners
  194. _movieClip.addEventListener(Event.ENTER_FRAME, onEnterFrame);
  195. _thumb.addEventListener(MouseEvent.MOUSE_DOWN, onThumbMouseDown);
  196. _thumb.addEventListener(MouseEvent.MOUSE_UP, onThumbMouseUp);
  197. _thumb.addEventListener(MouseEvent.MOUSE_OVER, onThumbHover);
  198. _thumb.addEventListener(MouseEvent.MOUSE_OUT, onThumbMouseOut);
  199. _track.addEventListener(MouseEvent.CLICK, onTrackClick);
  200. _playControl.addEventListener(MouseEvent.CLICK, onPlayClick);
  201. _pauseControl.addEventListener(MouseEvent.CLICK, onPauseClick);
  202. if(_nodeName == 'video'){
  203. _fullscreenControl.addEventListener(MouseEvent.CLICK, onFullscreenClick);
  204. }
  205. _volumeIcon.addEventListener(MouseEvent.CLICK, onVolumeIconClick);
  206. _volumeTrack.addEventListener(MouseEvent.CLICK, onVolumeTrackClick);
  207. _player.addEventListener(PlayerEvents.MOUSE_HIDE, onPlayerMouseHide);
  208. _player.addEventListener(PlayerEvents.MOUSE_SHOW, onPlayerMouseShow);
  209. _player.addEventListener(PlayerEvents.MEDIA_INITIALIZED, onPlayerMediaInitialized);
  210. _player.addEventListener(PlayerEvents.BUFFERING, onPlayerBuffering);
  211. _player.addEventListener(PlayerEvents.NOT_BUFFERING, onPlayerNotBuffering);
  212. _player.addEventListener(PlayerEvents.RESIZE, onPlayerResize);
  213. _player.addEventListener(PlayerEvents.PLAY_PAUSE, onPlayerPlayPause);
  214. _player.addEventListener(PlayerEvents.PLAYBACK_FINISHED, onPlayerPlaybackFinished);
  215. _player.addEventListener(PlayerEvents.CONNECTION_FAILED, onPlayerStreamNotFound);
  216. _player.addEventListener(PlayerEvents.ASPECT_RATIO, onPlayerAspectRatio);
  217. _stage.addEventListener(MouseEvent.MOUSE_UP, onThumbMouseUp);
  218. _stage.addEventListener(MouseEvent.MOUSE_OUT, onThumbMouseUp);
  219. _stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
  220. _stage.addEventListener(Event.RESIZE, onStageResize);
  221. _hideControlsTimer.addEventListener(TimerEvent.TIMER, hideControlsTimer);
  222. _hideAspectRatioLabelTimer.addEventListener(TimerEvent.TIMER, hideAspectRatioLabelTimer);
  223. _hideControlsTimer.start();
  224. if (_forceHideControls)
  225. {
  226. this.hideControls();
  227. }
  228. _shouldBeVisible = true;
  229. //}
  230. }
  231. //}
  232. //{Timers
  233. /**
  234. * Hides the playing controls when not moving mouse.
  235. * @param event The timer event associated
  236. */
  237. private function hideControlsTimer(event:TimerEvent):Void
  238. {
  239. if (_player.isPlaying())
  240. {
  241. if (_controlsVisible)
  242. {
  243. if (_stage.mouseX < _controlsBar.x ||
  244. _stage.mouseX >= _stage.stageWidth - 1 ||
  245. _stage.mouseY >= _stage.stageHeight - 1 ||
  246. _stage.mouseY <= 1
  247. )
  248. {
  249. _controlsVisible = false;
  250. }
  251. }
  252. else
  253. {
  254. hideControls();
  255. _hideControlsTimer.stop();
  256. }
  257. }
  258. }
  259. /**
  260. * Hides aspect ratio label
  261. * @param event
  262. */
  263. private function hideAspectRatioLabelTimer(event:TimerEvent):Void
  264. {
  265. //wait till fade in effect finish
  266. if (_aspectRatioLabelContainer.alpha >= 1)
  267. {
  268. Animation.fadeOut(_aspectRatioLabelContainer, 300);
  269. _hideAspectRatioLabelTimer.stop();
  270. }
  271. }
  272. //}
  273. //{Events
  274. /**
  275. * Keeps syncronized various elements of the controls like the thumb and download track bar
  276. * @param event
  277. */
  278. private function onEnterFrame(event:Event):Void
  279. {
  280. if(_player.getDuration() > 0) {
  281. if (_scrubbing)
  282. {
  283. _player.seek(((_thumb.x - _track.x) / _track.width) * _player.getDuration());
  284. }
  285. else
  286. {
  287. _currentPlayTimeLabel.text = Utils.formatTime(_player.getCurrentTime());
  288. _currentPlayTimeLabel.setTextFormat(_textFormat);
  289. _thumb.x = _player.getCurrentTime() / _player.getDuration() * (_track.width-_thumb.width) + _track.x;
  290. }
  291. }
  292. _volumeSlider.height = _volumeTrack.height * (_player.getVolume() / 1.0);
  293. _volumeSlider.y = (_volumeTrack.y + _volumeTrack.height) - _volumeSlider.height;
  294. drawDownloadProgress();
  295. }
  296. /**
  297. * Show playing controls on mouse movement.
  298. * @param event
  299. */
  300. private function onMouseMove(event:MouseEvent):Void
  301. {
  302. if(!_forceHideControls){
  303. if (_stage.mouseX >= _controlsBar.x)
  304. {
  305. if (!_hideControlsTimer.running)
  306. {
  307. _hideControlsTimer.start();
  308. }
  309. _controlsVisible = true;
  310. showControls();
  311. }
  312. }
  313. }
  314. /**
  315. * Function fired by a stage resize eventthat redraws the player controls
  316. * @param event
  317. */
  318. private function onStageResize(event:Event):Void
  319. {
  320. if(!_forceHideControls){
  321. redrawControls();
  322. }
  323. }
  324. /**
  325. * Toggles pause or play
  326. * @param event
  327. */
  328. private function onPlayClick(event:MouseEvent):Void
  329. {
  330. _player.togglePlay();
  331. _playControl.visible = !_player.isPlaying();
  332. _pauseControl.visible = _player.isPlaying();
  333. }
  334. /**
  335. * Toggles pause or play
  336. * @param event
  337. */
  338. private function onPauseClick(event:MouseEvent):Void
  339. {
  340. _player.togglePlay();
  341. _playControl.visible = !_player.isPlaying();
  342. _pauseControl.visible = _player.isPlaying();
  343. }
  344. /**
  345. * Toggles between window and fullscreen mode
  346. * @param event
  347. */
  348. private function onFullscreenClick(event:MouseEvent):Void
  349. {
  350. _player.toggleFullscreen();
  351. }
  352. /**
  353. * Toggles between mute and unmute
  354. * @param event
  355. */
  356. private function onVolumeIconClick(event: MouseEvent):Void
  357. {
  358. if (_volumeSlider.visible) {
  359. _volumeSlider.visible = false;
  360. _volumeTrack.visible = false;
  361. } else {
  362. _volumeSlider.visible = true;
  363. _volumeTrack.visible = true;
  364. }
  365. }
  366. /**
  367. * Detect user click on volume track control and change volume according
  368. * @param event
  369. */
  370. private function onVolumeTrackClick(event:MouseEvent):Void
  371. {
  372. var percent:Float = _volumeTrack.height - _volumeTrack.mouseY;
  373. var volume:Float = 1.0 * (percent / _volumeTrack.height);
  374. _player.setVolume(volume);
  375. }
  376. /**
  377. * Display not found message
  378. * @param event
  379. */
  380. private function onPlayerStreamNotFound(event:PlayerEvents):Void
  381. {
  382. //todo: to work on this
  383. }
  384. /**
  385. * Shows the loader bar when buffering
  386. * @param event
  387. */
  388. private function onPlayerBuffering(event:PlayerEvents):Void
  389. {
  390. if ( !_forceHideControls ) _loader.show();
  391. }
  392. /**
  393. * Hides loader bar when not buffering
  394. * @param event
  395. */
  396. private function onPlayerNotBuffering(event:PlayerEvents):Void
  397. {
  398. _loader.hide();
  399. }
  400. /**
  401. * Show the selected aspect ratio
  402. * @param event
  403. */
  404. private function onPlayerAspectRatio(event:PlayerEvents):Void
  405. {
  406. _hideAspectRatioLabelTimer.stop();
  407. _aspectRatioLabel.text = _player.getAspectRatioString();
  408. drawAspectRatioLabel();
  409. while (_aspectRatioLabelContainer.visible)
  410. {
  411. //wait till fade out finishes
  412. }
  413. Animation.fadeIn(_aspectRatioLabelContainer, 1);
  414. _hideAspectRatioLabelTimer.start();
  415. }
  416. /**
  417. * Monitors playbeack when finishes tu update controls
  418. * @param event
  419. */
  420. private function onPlayerPlaybackFinished(event:PlayerEvents):Void
  421. {
  422. _playControl.visible = !_player.isPlaying();
  423. _pauseControl.visible = _player.isPlaying();
  424. showControls();
  425. }
  426. /**
  427. * Monitors keyboard play pause actions to update icons
  428. * @param event
  429. */
  430. private function onPlayerPlayPause(event:PlayerEvents):Void
  431. {
  432. _playControl.visible = !_player.isPlaying();
  433. _pauseControl.visible = _player.isPlaying();
  434. }
  435. /**
  436. * Resizes the video player on windowed mode substracting the seekbar height
  437. * @param event
  438. */
  439. private function onPlayerResize(event:PlayerEvents):Void
  440. {
  441. }
  442. /**
  443. * Updates media total time duration.
  444. * @param event
  445. */
  446. private function onPlayerMediaInitialized(event:PlayerEvents):Void
  447. {
  448. _totalPlayTimeLabel.text = Utils.formatTime(event.duration);
  449. _totalPlayTimeLabel.setTextFormat(_textFormat);
  450. _playControl.visible = !_player.isPlaying();
  451. _pauseControl.visible = _player.isPlaying();
  452. }
  453. /**
  454. * Hides seekbar if on fullscreen.
  455. * @param event
  456. */
  457. private function onPlayerMouseHide(event:PlayerEvents):Void
  458. {
  459. if (_controlsBar.visible && _player.isFullscreen())
  460. {
  461. hideControls();
  462. }
  463. }
  464. /**
  465. * Shows seekbar
  466. * @param event
  467. */
  468. private function onPlayerMouseShow(event:PlayerEvents):Void
  469. {
  470. if (!_controlsBar.visible)
  471. {
  472. if (!_forceHideControls) {
  473. _controlsBar.visible = true;
  474. } else {
  475. _shouldBeVisible = true;
  476. }
  477. }
  478. }
  479. /**
  480. * Translates a user click in to time and seeks to it
  481. * @param event
  482. */
  483. private function onTrackClick(event:MouseEvent):Void
  484. {
  485. var clickPosition:Float = _track.mouseX;
  486. _player.seek(_player.getDuration() * (clickPosition / _track.width));
  487. }
  488. /**
  489. * Enables dragging of thumb for seeking media
  490. * @param event
  491. */
  492. private function onThumbMouseDown(event:MouseEvent):Void
  493. {
  494. _scrubbing = true;
  495. var rectangle:Rectangle = new Rectangle(_track.x, _track.y, _track.width-_thumb.width, 0);
  496. _thumb.startDrag(false, rectangle);
  497. }
  498. /**
  499. * Changes thumb seek control to hover color
  500. * @param event
  501. */
  502. private function onThumbHover(event:MouseEvent):Void
  503. {
  504. _thumb.graphics.lineStyle();
  505. _thumb.graphics.beginFill(_hoverColor);
  506. _thumb.graphics.drawRoundRect(0, (_seekBar.height/2)-(11/2), 11, 11, 10, 10);
  507. _thumb.graphics.endFill();
  508. }
  509. /**
  510. * Changes thumb seek control to control color
  511. * @param event
  512. */
  513. private function onThumbMouseOut(event:MouseEvent):Void
  514. {
  515. var matrix:Matrix = new Matrix( );
  516. matrix.createGradientBox(11, 11, Utils.degreesToRadians(-90), 11, 0);
  517. var colors:Array<UInt> = [_controlColor, _controlColor];
  518. var alphas:Array<Float> = [0.75, 1];
  519. var ratios:Array<UInt> = [0, 255];
  520. _thumb.graphics.beginGradientFill(GradientType.LINEAR, colors, alphas, ratios, matrix);
  521. _thumb.graphics.drawRoundRect(0, (_seekBar.height / 2) - (11 / 2), 11, 11, 10, 10);
  522. _thumb.graphics.endFill();
  523. }
  524. /**
  525. * Disables dragging of thumb
  526. * @param event
  527. */
  528. private function onThumbMouseUp(event:MouseEvent):Void
  529. {
  530. _scrubbing = false;
  531. _thumb.stopDrag( );
  532. }
  533. //}
  534. //{Drawing functions
  535. /**
  536. * Clears all current graphics a draw new ones
  537. */
  538. private function redrawControls():Void
  539. {
  540. drawControls();
  541. drawAspectRatioLabel();
  542. }
  543. /**
  544. * Draws the download progress track bar
  545. */
  546. private function drawDownloadProgress():Void
  547. {
  548. if (_player.getBytesTotal() > 0)
  549. {
  550. var bytesLoaded:Float = _player.getBytesLoaded();
  551. var bytesTotal:Float = _player.getBytesTotal();
  552. _percentLoaded = bytesLoaded / bytesTotal;
  553. }
  554. var position:Float = _player.getStartTime() / _player.getDuration();
  555. var startPosition:Float = (position > 0?(position * _track.width):0) + _track.x;
  556. _trackDownloaded.graphics.clear();
  557. _trackDownloaded.graphics.lineStyle();
  558. _trackDownloaded.x = startPosition;
  559. _trackDownloaded.graphics.beginFill(_seekColor, 0.5);
  560. _trackDownloaded.graphics.drawRoundRect(0, (_seekBar.height / 2) - (5 / 2), ((_track.width + _track.x) - _trackDownloaded.x) * _percentLoaded, 5, 3, 3);
  561. _trackDownloaded.graphics.endFill();
  562. }
  563. /**
  564. * Draws NEW control bar player/seek controls
  565. */
  566. private function drawControls():Void
  567. {
  568. //Reset sprites for redraw
  569. _controlsBar.graphics.clear();
  570. _volumeTrack.graphics.clear();
  571. _volumeSlider.graphics.clear();
  572. _volumeSlider.visible = false;
  573. _volumeTrack.visible = false;
  574. //Reset sprites for redraw
  575. _seekBar.graphics.clear();
  576. _track.graphics.clear();
  577. _thumb.graphics.clear();
  578. //Draw controls bar
  579. var barMargin = 10;
  580. var barWidth = _stage.stageWidth;
  581. var barHeight = _controlSize;
  582. var barCenter = barWidth / 2;
  583. var buttonSize = Std.int(((80 / 100) * (barHeight - (barMargin*2))));
  584. var buttonCount = (_nodeName == 'video') ? 2 : 1;
  585. _controlsBar.x = 0;
  586. _controlsBar.y = (_stage.stageHeight - barHeight);
  587. var matrix:Matrix = new Matrix( );
  588. matrix.createGradientBox(barWidth, barHeight, Utils.degreesToRadians(-90), barWidth, 0);
  589. var colors:Array<UInt> = [_brightColor, _darkColor];
  590. var alphas:Array<Float> = [1.0, 1];
  591. var ratios:Array<UInt> = [0, 255];
  592. _controlsBar.graphics.lineStyle();
  593. _controlsBar.graphics.beginGradientFill(GradientType.LINEAR, colors, alphas, ratios, matrix);
  594. _controlsBar.graphics.drawRect(0, 2, barWidth, barHeight-2);
  595. _controlsBar.graphics.endFill();
  596. _controlsBar.graphics.beginFill(_darkColor, 1);
  597. _controlsBar.graphics.drawRect(0, 0, barWidth, 1);
  598. _controlsBar.graphics.endFill();
  599. _controlsBar.graphics.beginFill(_brightColor, 1);
  600. _controlsBar.graphics.drawRect(0, 1, barWidth, 1);
  601. _controlsBar.graphics.endFill();
  602. //Draw seek bar
  603. var _seekBarWidth = barWidth - (buttonSize+barMargin)*buttonCount - (_playControl.x + _playControl.width + barMargin) - barMargin;
  604. var _seekBarHeight = barHeight;
  605. _seekBar.x = _playControl.x + _playControl.width + barMargin;
  606. _seekBar.y = 0;
  607. _seekBar.graphics.lineStyle();
  608. _seekBar.graphics.beginFill(_darkColor, 0);
  609. _seekBar.graphics.drawRect(0, 0, _seekBarWidth, _seekBarHeight);
  610. _seekBar.graphics.endFill();
  611. //Draw playbutton
  612. _playControl.setNormalColor(_controlColor);
  613. _playControl.setHoverColor(_hoverColor);
  614. _playControl.setPosition(barMargin, barMargin);
  615. _playControl.setSize(buttonSize+5, buttonSize+5);
  616. //Draw pausebutton
  617. _pauseControl.setNormalColor(_controlColor);
  618. _pauseControl.setHoverColor(_hoverColor);
  619. _pauseControl.setPosition(_playControl.x, _playControl.y);
  620. _pauseControl.setSize(buttonSize+5, buttonSize+5);
  621. //Draw current play time label
  622. _textFormat.color = _seekColor;
  623. _currentPlayTimeLabel.x = 0;
  624. _currentPlayTimeLabel.y = _seekBarHeight - (_seekBarHeight / 2) - (_currentPlayTimeLabel.height / 2);
  625. _currentPlayTimeLabel.antiAliasType = AntiAliasType.ADVANCED;
  626. _currentPlayTimeLabel.setTextFormat(_textFormat);
  627. //Draw total play time label
  628. _totalPlayTimeLabel.x = _seekBarWidth - _totalPlayTimeLabel.width;
  629. _totalPlayTimeLabel.y = _seekBarHeight - (_seekBarHeight / 2) - (_totalPlayTimeLabel.height / 2);
  630. _totalPlayTimeLabel.antiAliasType = AntiAliasType.ADVANCED;
  631. _totalPlayTimeLabel.setTextFormat(_textFormat);
  632. //Draw download progress
  633. drawDownloadProgress();
  634. //Draw track place holder for drag
  635. _track.x = _currentPlayTimeLabel.x + _currentPlayTimeLabel.width + barMargin;
  636. _track.graphics.lineStyle();
  637. _track.graphics.beginFill(_seekColor, 0);
  638. _track.graphics.drawRect(0, (_seekBarHeight / 2) - ((buttonSize+barMargin) / 2), _totalPlayTimeLabel.x - _totalPlayTimeLabel.width - barMargin - barMargin, buttonSize + barMargin);
  639. _track.graphics.endFill();
  640. _track.graphics.lineStyle();
  641. _track.graphics.beginFill(_seekColor, 0.3);
  642. _track.graphics.drawRoundRect(0, (_seekBarHeight / 2) - (5 / 2), _totalPlayTimeLabel.x - _totalPlayTimeLabel.width - barMargin - barMargin, 5, 3, 3);
  643. _track.graphics.endFill();
  644. //Draw thumb
  645. var matrix:Matrix = new Matrix( );
  646. matrix.createGradientBox(11, 11, Utils.degreesToRadians(-90), 11, 0);
  647. var colors:Array<UInt> = [_controlColor, _controlColor];
  648. var alphas:Array<Float> = [0.75, 1];
  649. var ratios:Array<UInt> = [0, 255];
  650. _thumb.x = _currentPlayTimeLabel.width + _currentPlayTimeLabel.x + barMargin;
  651. _thumb.graphics.lineStyle();
  652. _thumb.graphics.beginGradientFill(GradientType.LINEAR, colors, alphas, ratios, matrix);
  653. //_thumb.graphics.beginFill(_controlColor);
  654. _thumb.graphics.drawRoundRect(0, (_seekBarHeight/2)-(11/2), 11, 11, 10, 10);
  655. _thumb.graphics.endFill();
  656. //Draw volume icon
  657. _volumeIcon.setNormalColor(_controlColor);
  658. _volumeIcon.setHoverColor(_hoverColor);
  659. _volumeIcon.setPosition(_seekBar.x + _seekBar.width + barMargin, _playControl.y+1);
  660. _volumeIcon.setSize(buttonSize, buttonSize);
  661. //Draw fullscreen button
  662. if(_nodeName == 'video'){
  663. _fullscreenControl.setNormalColor(_controlColor);
  664. _fullscreenControl.setHoverColor(_hoverColor);
  665. _fullscreenControl.setPosition(_volumeIcon.x + _volumeIcon.width + barMargin, _playControl.y+1);
  666. _fullscreenControl.setSize(buttonSize, buttonSize);
  667. }
  668. //Draw volume track
  669. _volumeTrack.x = _controlsBar.width-(buttonSize+barMargin)*buttonCount;
  670. _volumeTrack.y = -_controlsBar.height-2;
  671. _volumeTrack.graphics.lineStyle(1, _controlColor);
  672. _volumeTrack.graphics.beginFill(0x000000, 0);
  673. _volumeTrack.graphics.drawRect(0, 0, buttonSize, _controlsBar.height);
  674. _volumeTrack.graphics.endFill();
  675. //Draw volume slider
  676. var matrix:Matrix = new Matrix( );
  677. matrix.createGradientBox(_volumeTrack.width, _volumeTrack.height, Utils.degreesToRadians(-90), _volumeTrack.width, 0);
  678. var colors:Array<UInt> = [_hoverColor, _hoverColor];
  679. var alphas:Array<Float> = [0.75, 1];
  680. var ratios:Array<UInt> = [0, 255];
  681. _volumeSlider.x = _volumeTrack.x;
  682. _volumeSlider.y = _volumeTrack.y;
  683. _volumeSlider.graphics.lineStyle();
  684. _volumeSlider.graphics.beginGradientFill(GradientType.LINEAR, colors, alphas, ratios, matrix);
  685. //_volumeSlider.graphics.beginFill(_hoverColor, 1);
  686. _volumeSlider.graphics.drawRect(0, 0, _volumeTrack.width, _volumeTrack.height);
  687. _volumeSlider.graphics.endFill();
  688. }
  689. private function drawAspectRatioLabel():Void
  690. {
  691. _aspectRatioLabelContainer.graphics.clear();
  692. _aspectRatioLabelContainer.visible = false;
  693. //Update aspect ratio label
  694. var textFormat:TextFormat = new TextFormat();
  695. textFormat.font = "arial";
  696. textFormat.bold = true;
  697. textFormat.size = 40;
  698. textFormat.color = _controlColor;
  699. _aspectRatioLabel.setTextFormat(textFormat);
  700. _aspectRatioLabel.x = (_stage.stageWidth / 2) - (_aspectRatioLabel.width / 2);
  701. _aspectRatioLabel.y = (_stage.stageHeight / 2) - (_aspectRatioLabel.height / 2);
  702. //Draw aspect ratio label container
  703. _aspectRatioLabelContainer.x = _aspectRatioLabel.x - 10;
  704. _aspectRatioLabelContainer.y = _aspectRatioLabel.y - 10;
  705. _aspectRatioLabelContainer.graphics.lineStyle(0, _darkColor);
  706. _aspectRatioLabelContainer.graphics.beginFill(_darkColor, 1);
  707. _aspectRatioLabelContainer.graphics.drawRoundRect(0, 0, _aspectRatioLabel.width + 20, _aspectRatioLabel.height + 20, 15, 15);
  708. _aspectRatioLabelContainer.graphics.endFill();
  709. _aspectRatioLabel.x = 10;
  710. _aspectRatioLabel.y = 10;
  711. }
  712. //}
  713. //{Private Methods
  714. /**
  715. * Hide the play controls bar
  716. */
  717. private function hideControls():Void
  718. {
  719. if(_controlsBar.visible)
  720. {
  721. if (_forceHideControls) {
  722. _controlsBar.visible = false;
  723. } else if(_nodeName == 'video') {
  724. drawControls();
  725. Animation.slideOut(_controlsBar, "bottom", 800);
  726. }
  727. }
  728. _shouldBeVisible = false;
  729. }
  730. public function forceControls(controls:Bool):Void
  731. {
  732. if(controls){
  733. _forceHideControls = false;
  734. if(_shouldBeVisible){
  735. showControls();
  736. }
  737. } else {
  738. _forceHideControls = true;
  739. _controlsBar.visible = false;
  740. _loader.hide();
  741. }
  742. }
  743. /**
  744. * Shows play controls bar
  745. */
  746. private function showControls():Void
  747. {
  748. if(!_forceHideControls && !_controlsBar.visible)
  749. {
  750. drawControls();
  751. _controlsBar.visible = true;
  752. }
  753. _shouldBeVisible = true;
  754. }
  755. //}
  756. //{Setters
  757. /**
  758. * Sets the player colors and redraw them
  759. * @param colors Array of colors in the following order: darkColor, brightColor, controlColor, hoverColor
  760. */
  761. public function setControlColors(colors:Array<String>):Void
  762. {
  763. _darkColor = colors[0].length > 0? Std.parseInt("0x" + colors[0]) : 0x000000;
  764. _brightColor = colors[1].length > 0? Std.parseInt("0x" + colors[1]) : 0x4c4c4c;
  765. _controlColor = colors[2].length > 0? Std.parseInt("0x" + colors[2]) : 0xFFFFFF;
  766. _hoverColor = colors[3].length > 0? Std.parseInt("0x" + colors[3]) : 0x67A8C1;
  767. _seekColor = colors[4].length > 0? Std.parseInt("0x" + colors[4]) : 0x7c7c7c;
  768. var loaderColors:Array <String> = ["", ""];
  769. loaderColors[0] = colors[0];
  770. loaderColors[1] = colors[2];
  771. loaderColors[2] = colors[4];
  772. if ( _forceHideControls && _showLoader || !_forceHideControls ) _loader.setColors(loaderColors);
  773. redrawControls();
  774. }
  775. /**
  776. * Sets the player controls size (height)
  777. * @param size int: for e.g. 50
  778. */
  779. public function setControlSize(size:Int):Void
  780. {
  781. if (size == 0)
  782. return;
  783. _controlSize = size;
  784. redrawControls();
  785. }
  786. /**
  787. * To set the duration label when autostart parameter is false
  788. * @param duration in seconds or formatted string in format hh:mm:ss
  789. */
  790. public function setDurationLabel(duration:String):Void
  791. {
  792. //Person passed time already formatted
  793. if (duration.indexOf(":") != -1)
  794. {
  795. _totalPlayTimeLabel.text = duration;
  796. }
  797. //Time passed in seconds
  798. else
  799. {
  800. _totalPlayTimeLabel.text = Std.string(Utils.formatTime(Std.parseFloat(duration)));
  801. }
  802. _totalPlayTimeLabel.setTextFormat(_textFormat);
  803. }
  804. //}
  805. }