PageRenderTime 174ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/Jaris/src/jaris/player/newcontrols/NewControls.hx

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