PageRenderTime 89ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Jaris/src/jaris/player/controls/Controls.hx

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