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

/lib/Jaris/src/jaris/player/Player.hx

https://gitlab.com/dali99/shimmie2-Material-Theme
Haxe | 1788 lines | 1209 code | 227 blank | 352 comment | 257 complexity | d5ad724ddeb9a5141838e430d3c6d6df 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;
  23. import flash.display.Loader;
  24. import flash.display.MovieClip;
  25. import flash.display.Sprite;
  26. import flash.display.Stage;
  27. import flash.display.StageDisplayState;
  28. import flash.events.AsyncErrorEvent;
  29. import flash.events.Event;
  30. import flash.events.EventDispatcher;
  31. import flash.events.FullScreenEvent;
  32. import flash.events.IOErrorEvent;
  33. import flash.events.KeyboardEvent;
  34. import flash.events.MouseEvent;
  35. import flash.events.NetStatusEvent;
  36. import flash.events.ProgressEvent;
  37. import flash.events.TimerEvent;
  38. import flash.geom.Rectangle;
  39. import flash.Lib;
  40. import flash.media.ID3Info;
  41. import flash.media.Sound;
  42. import flash.media.SoundChannel;
  43. import flash.media.SoundTransform;
  44. import flash.media.Video;
  45. import flash.net.NetConnection;
  46. import flash.net.NetStream;
  47. import flash.net.URLRequest;
  48. import flash.system.Capabilities;
  49. import flash.system.Security;
  50. import flash.ui.Keyboard;
  51. import flash.ui.Mouse;
  52. import flash.utils.Timer;
  53. import jaris.events.PlayerEvents;
  54. import jaris.utils.Utils;
  55. import jaris.player.AspectRatio;
  56. import jaris.player.UserSettings;
  57. /**
  58. * Jaris main video player
  59. */
  60. class Player extends EventDispatcher
  61. {
  62. //{Member variables
  63. private var _stage:Stage;
  64. private var _movieClip:MovieClip;
  65. private var _connection:NetConnection;
  66. private var _stream:NetStream;
  67. private var _fullscreen:Bool;
  68. private var _soundMuted:Bool;
  69. private var _volume:Float;
  70. private var _bufferTime:Float;
  71. private var _mouseVisible:Bool;
  72. private var _mediaLoaded:Bool;
  73. private var _hideMouseTimer:Timer;
  74. private var _checkAudioTimer:Timer;
  75. private var _mediaSource:String;
  76. private var _type:String;
  77. private var _streamType:String;
  78. private var _server:String; //For future use on rtmp
  79. private var _sound:Sound;
  80. private var _soundChannel:SoundChannel;
  81. private var _id3Info:ID3Info;
  82. private var _video:Video;
  83. private var _videoWidth:Float;
  84. private var _videoHeight:Float;
  85. private var _videoMask:Sprite;
  86. private var _videoQualityHigh:Bool;
  87. private var _mediaDuration:Float;
  88. private var _lastTime:Float;
  89. private var _lastProgress:Float;
  90. private var _isPlaying:Bool;
  91. private var _aspectRatio:Float;
  92. private var _currentAspectRatio:String;
  93. private var _originalAspectRatio:Float;
  94. private var _mediaEndReached:Bool;
  95. private var _seekPoints:Array<Float>;
  96. private var _downloadCompleted:Bool;
  97. private var _startTime:Float;
  98. private var _firstLoad:Bool;
  99. private var _stopped:Bool;
  100. private var _useHardWareScaling:Bool;
  101. private var _youtubeLoader:Loader;
  102. private var _userSettings:UserSettings;
  103. //}
  104. //{Constructor
  105. public function new()
  106. {
  107. super();
  108. //{Main Variables Init
  109. _stage = Lib.current.stage;
  110. _movieClip = Lib.current;
  111. _mouseVisible = true;
  112. _soundMuted = false;
  113. _volume = 1.0;
  114. _bufferTime = 10;
  115. _fullscreen = false;
  116. _mediaLoaded = false;
  117. _hideMouseTimer = new Timer(1500);
  118. _checkAudioTimer = new Timer(100);
  119. _seekPoints = new Array();
  120. _downloadCompleted = false;
  121. _startTime = 0;
  122. _firstLoad = true;
  123. _stopped = false;
  124. _videoQualityHigh = false;
  125. _isPlaying = false;
  126. _streamType = StreamType.FILE;
  127. _type = InputType.VIDEO;
  128. _server = "";
  129. _currentAspectRatio = "original";
  130. _aspectRatio = 0;
  131. _lastTime = 0;
  132. _lastProgress = 0;
  133. _userSettings = new UserSettings();
  134. //}
  135. //{Initialize sound object
  136. _sound = new Sound();
  137. _sound.addEventListener(Event.COMPLETE, onSoundComplete);
  138. _sound.addEventListener(Event.ID3, onSoundID3);
  139. _sound.addEventListener(IOErrorEvent.IO_ERROR, onSoundIOError);
  140. _sound.addEventListener(ProgressEvent.PROGRESS, onSoundProgress);
  141. //}
  142. //{Initialize video and connection objects
  143. _connection = new NetConnection();
  144. _connection.client = this;
  145. _connection.connect(null);
  146. _stream = new NetStream(_connection);
  147. _video = new Video(_stage.stageWidth, _stage.stageHeight);
  148. _movieClip.addChild(_video);
  149. //}
  150. //Video mask so that custom menu items work
  151. _videoMask = new Sprite();
  152. _movieClip.addChild(_videoMask);
  153. //Set initial rendering to high quality
  154. toggleQuality();
  155. //{Initialize system event listeners
  156. _movieClip.addEventListener(Event.ENTER_FRAME, onEnterFrame);
  157. _stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
  158. _stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
  159. _stage.addEventListener(FullScreenEvent.FULL_SCREEN, onFullScreen);
  160. _stage.addEventListener(Event.RESIZE, onResize);
  161. _hideMouseTimer.addEventListener(TimerEvent.TIMER, hideMouseTimer);
  162. _checkAudioTimer.addEventListener(TimerEvent.TIMER, checkAudioTimer);
  163. _connection.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);
  164. _connection.addEventListener(AsyncErrorEvent.ASYNC_ERROR, onAsyncError);
  165. //}
  166. }
  167. //}
  168. //{Timers
  169. /**
  170. * Timer that hides the mouse pointer when it is idle and dispatch the PlayerEvents.MOUSE_HIDE
  171. * @param event
  172. */
  173. private function hideMouseTimer(event:TimerEvent):Void
  174. {
  175. if (_fullscreen)
  176. {
  177. if (_mouseVisible)
  178. {
  179. _mouseVisible = false;
  180. }
  181. else
  182. {
  183. Mouse.hide();
  184. callEvents(PlayerEvents.MOUSE_HIDE);
  185. _hideMouseTimer.stop();
  186. }
  187. }
  188. }
  189. /**
  190. * To check if the sound finished playing
  191. * @param event
  192. */
  193. private function checkAudioTimer(event:TimerEvent):Void
  194. {
  195. if (_soundChannel.position + 100 >= _sound.length)
  196. {
  197. _isPlaying = false;
  198. _mediaEndReached = true;
  199. callEvents(PlayerEvents.PLAYBACK_FINISHED);
  200. _checkAudioTimer.stop();
  201. }
  202. }
  203. //}
  204. //{Events
  205. /**
  206. * Callback after bandwidth calculation for rtmp streams
  207. */
  208. private function onBWDone():Void
  209. {
  210. //Need to study this more
  211. }
  212. /**
  213. * Triggers error event on rtmp connections
  214. * @param event
  215. */
  216. private function onAsyncError(event:AsyncErrorEvent):Void
  217. {
  218. //TODO: Should trigger event for controls to display error message
  219. trace(event.error);
  220. }
  221. /**
  222. * Checks if connection failed or succeed
  223. * @param event
  224. */
  225. private function onNetStatus(event:NetStatusEvent):Void
  226. {
  227. switch (event.info.code)
  228. {
  229. case "NetConnection.Connect.Success":
  230. if (_streamType == StreamType.RTMP)
  231. {
  232. _stream = new NetStream(_connection);
  233. _stream.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);
  234. _stream.bufferTime = 10;
  235. _stream.play(Utils.rtmpSourceParser(_mediaSource), true);
  236. _stream.client = this;
  237. if(_type == InputType.VIDEO) {_video.attachNetStream(_stream); }
  238. }
  239. callEvents(PlayerEvents.CONNECTION_SUCCESS);
  240. case "NetStream.Play.StreamNotFound":
  241. trace("Stream not found: " + _mediaSource); //Replace with a dispatch for error event
  242. callEvents(PlayerEvents.CONNECTION_FAILED);
  243. case "NetStream.Play.Stop":
  244. if (_streamType != StreamType.RTMP)
  245. {
  246. if (_isPlaying) { _stream.togglePause(); }
  247. _isPlaying = false;
  248. _mediaEndReached = true;
  249. callEvents(PlayerEvents.PLAYBACK_FINISHED);
  250. }
  251. case "NetStream.Play.Start":
  252. _isPlaying = true;
  253. _mediaEndReached = false;
  254. if (_stream.bytesLoaded != _stream.bytesTotal || _streamType == StreamType.RTMP)
  255. {
  256. callEvents(PlayerEvents.BUFFERING);
  257. }
  258. case "NetStream.Seek.Notify":
  259. _mediaEndReached = false;
  260. if (_streamType == StreamType.RTMP)
  261. {
  262. _isPlaying = true;
  263. callEvents(PlayerEvents.PLAY_PAUSE);
  264. callEvents(PlayerEvents.BUFFERING);
  265. }
  266. case "NetStream.Buffer.Empty":
  267. if (_stream.bytesLoaded != _stream.bytesTotal)
  268. {
  269. callEvents(PlayerEvents.BUFFERING);
  270. }
  271. case "NetStream.Buffer.Full":
  272. callEvents(PlayerEvents.NOT_BUFFERING);
  273. case "NetStream.Buffer.Flush":
  274. if (_stream.bytesLoaded == _stream.bytesTotal)
  275. {
  276. _downloadCompleted = true;
  277. }
  278. }
  279. }
  280. /**
  281. * Proccess keyboard shortcuts
  282. * @param event
  283. */
  284. private function onKeyDown(event:KeyboardEvent):Void
  285. {
  286. var F_KEY:UInt = 70;
  287. var M_KEY:UInt = 77;
  288. var X_KEY:UInt = 88;
  289. switch(event.keyCode)
  290. {
  291. case Keyboard.TAB:
  292. toggleAspectRatio();
  293. case F_KEY:
  294. toggleFullscreen();
  295. case M_KEY:
  296. toggleMute();
  297. case Keyboard.UP:
  298. volumeUp();
  299. case Keyboard.DOWN:
  300. volumeDown();
  301. case Keyboard.RIGHT:
  302. forward();
  303. case Keyboard.LEFT:
  304. rewind();
  305. case Keyboard.SPACE:
  306. togglePlay();
  307. case X_KEY:
  308. stopAndClose();
  309. }
  310. }
  311. /**
  312. * IF player is full screen shows the mouse when gets hide
  313. * @param event
  314. */
  315. private function onMouseMove(event:MouseEvent):Void
  316. {
  317. if (_fullscreen && !_mouseVisible)
  318. {
  319. if (!_hideMouseTimer.running)
  320. {
  321. _hideMouseTimer.start();
  322. }
  323. _mouseVisible = true;
  324. Mouse.show();
  325. callEvents(PlayerEvents.MOUSE_SHOW);
  326. }
  327. }
  328. /**
  329. * Resize video player
  330. * @param event
  331. */
  332. private function onResize(event:Event):Void
  333. {
  334. resizeAndCenterPlayer();
  335. }
  336. /**
  337. * Dispath a full screen event to listeners as redraw player an takes care of some other aspects
  338. * @param event
  339. */
  340. private function onFullScreen(event:FullScreenEvent):Void
  341. {
  342. _fullscreen = event.fullScreen;
  343. if (!event.fullScreen)
  344. {
  345. Mouse.show();
  346. callEvents(PlayerEvents.MOUSE_SHOW);
  347. _mouseVisible = true;
  348. }
  349. else
  350. {
  351. _mouseVisible = true;
  352. _hideMouseTimer.start();
  353. }
  354. resizeAndCenterPlayer();
  355. callEvents(PlayerEvents.FULLSCREEN);
  356. }
  357. /**
  358. * Sits for any cue points available
  359. * @param data
  360. * @note Planned future implementation
  361. */
  362. private function onCuePoint(data:Dynamic):Void
  363. {
  364. }
  365. /**
  366. * After a video is loaded this callback gets the video information at start and stores it on variables
  367. * @param data
  368. */
  369. private function onMetaData(data:Dynamic):Void
  370. {
  371. if (_firstLoad)
  372. {
  373. _isPlaying = true;
  374. _firstLoad = false;
  375. if (data.width)
  376. {
  377. _videoWidth = data.width;
  378. _videoHeight = data.height;
  379. }
  380. else
  381. {
  382. _videoWidth = _video.width;
  383. _videoHeight = _video.height;
  384. }
  385. //Store seekpoints times
  386. if (data.hasOwnProperty("seekpoints")) //MP4
  387. {
  388. for (position in Reflect.fields(data.seekpoints))
  389. {
  390. _seekPoints.push(Reflect.field(data.seekpoints, position).time);
  391. }
  392. }
  393. else if (data.hasOwnProperty("keyframes")) //FLV
  394. {
  395. for (position in Reflect.fields(data.keyframes.times))
  396. {
  397. _seekPoints.push(Reflect.field(data.keyframes.times, position));
  398. }
  399. }
  400. _mediaLoaded = true;
  401. _mediaDuration = data.duration;
  402. _originalAspectRatio = AspectRatio.getAspectRatio(_videoWidth, _videoHeight);
  403. if (_aspectRatio <= 0)
  404. {
  405. _aspectRatio = _originalAspectRatio;
  406. }
  407. callEvents(PlayerEvents.MEDIA_INITIALIZED);
  408. resizeAndCenterPlayer();
  409. //Retrieve the volume that user selected last time
  410. setVolume(_userSettings.getVolume());
  411. }
  412. }
  413. /**
  414. * Dummy function invoked for pseudostream servers
  415. * @param data
  416. */
  417. private function onLastSecond(data:Dynamic):Void
  418. {
  419. trace("last second pseudostream");
  420. }
  421. /**
  422. * Broadcast Timeupdate and Duration
  423. */
  424. private function onEnterFrame(event:Event):Void
  425. {
  426. if (getDuration() > 0 && _lastTime != getCurrentTime())
  427. {
  428. _lastTime = getCurrentTime();
  429. callEvents(PlayerEvents.TIME);
  430. }
  431. if (getBytesLoaded() > 0 && _lastProgress < getBytesLoaded())
  432. {
  433. _lastProgress = getBytesLoaded();
  434. callEvents(PlayerEvents.PROGRESS);
  435. }
  436. }
  437. /**
  438. * Triggers when playbacks end on rtmp streaming server
  439. */
  440. private function onPlayStatus(info:Dynamic):Void
  441. {
  442. _isPlaying = false;
  443. _mediaEndReached = true;
  444. callEvents(PlayerEvents.PLAYBACK_FINISHED);
  445. }
  446. /**
  447. * When sound finished downloading
  448. * @param event
  449. */
  450. private function onSoundComplete(event:Event)
  451. {
  452. _mediaDuration = _sound.length / 1000;
  453. _downloadCompleted = true;
  454. callEvents(PlayerEvents.MEDIA_INITIALIZED);
  455. }
  456. /**
  457. * Mimic stream onMetaData
  458. * @param event
  459. */
  460. private function onSoundID3(event:Event)
  461. {
  462. if (_firstLoad)
  463. {
  464. _soundChannel = _sound.play();
  465. _checkAudioTimer.start();
  466. _isPlaying = true;
  467. _firstLoad = false;
  468. _mediaLoaded = true;
  469. _mediaDuration = ((_sound.bytesTotal / _sound.bytesLoaded) * _sound.length) / 1000;
  470. _aspectRatio = AspectRatio.getAspectRatio(_videoWidth, _videoHeight);
  471. _originalAspectRatio = _aspectRatio;
  472. _id3Info = _sound.id3;
  473. callEvents(PlayerEvents.CONNECTION_SUCCESS);
  474. callEvents(PlayerEvents.MEDIA_INITIALIZED);
  475. resizeAndCenterPlayer();
  476. //Retrieve the volume that user selected last time
  477. setVolume(_userSettings.getVolume());
  478. }
  479. }
  480. /**
  481. * Dispatch connection failed event on error
  482. * @param event
  483. */
  484. private function onSoundIOError(event:IOErrorEvent)
  485. {
  486. callEvents(PlayerEvents.CONNECTION_FAILED);
  487. }
  488. /**
  489. * Monitor sound download progress
  490. * @param event
  491. */
  492. private function onSoundProgress(event:ProgressEvent)
  493. {
  494. if (_sound.isBuffering)
  495. {
  496. callEvents(PlayerEvents.BUFFERING);
  497. }
  498. else
  499. {
  500. callEvents(PlayerEvents.NOT_BUFFERING);
  501. }
  502. _mediaDuration = ((_sound.bytesTotal / _sound.bytesLoaded) * _sound.length) / 1000;
  503. callEvents(PlayerEvents.MEDIA_INITIALIZED);
  504. }
  505. /**
  506. * Initializes the youtube loader object
  507. * @param event
  508. */
  509. private function onYouTubeLoaderInit(event:Event):Void
  510. {
  511. _youtubeLoader.content.addEventListener("onReady", onYoutubeReady);
  512. _youtubeLoader.content.addEventListener("onError", onYoutubeError);
  513. _youtubeLoader.content.addEventListener("onStateChange", onYoutubeStateChange);
  514. _youtubeLoader.content.addEventListener("onPlaybackQualityChange", onYoutubePlaybackQualityChange);
  515. }
  516. /**
  517. * This event is fired when the player is loaded and initialized, meaning it is ready to receive API calls.
  518. */
  519. private function onYoutubeReady(event:Event):Void
  520. {
  521. _movieClip.addChild(_youtubeLoader.content);
  522. _movieClip.setChildIndex(_youtubeLoader.content, 0);
  523. Reflect.field(_youtubeLoader.content, "setSize")(_stage.stageWidth, _stage.stageHeight);
  524. Reflect.field(_youtubeLoader.content, "loadVideoByUrl")(Utils.youtubeSourceParse(_mediaSource));
  525. callEvents(PlayerEvents.BUFFERING);
  526. }
  527. /**
  528. * This event is fired whenever the player's state changes. Possible values are unstarted (-1), ended (0),
  529. * playing (1), paused (2), buffering (3), video cued (5). When the SWF is first loaded it will broadcast
  530. * an unstarted (-1) event. When the video is cued and ready to play it will broadcast a video cued event (5).
  531. * @param event
  532. */
  533. private function onYoutubeStateChange(event:Event):Void
  534. {
  535. var status:UInt = Std.parseInt(Reflect.field(event, "data"));
  536. switch(status)
  537. {
  538. case -1:
  539. callEvents(PlayerEvents.BUFFERING);
  540. case 0:
  541. _isPlaying = false;
  542. _mediaEndReached = true;
  543. callEvents(PlayerEvents.PLAYBACK_FINISHED);
  544. case 1:
  545. if (_firstLoad)
  546. {
  547. _isPlaying = true;
  548. _videoWidth = _stage.stageWidth;
  549. _videoHeight = _stage.stageHeight;
  550. _firstLoad = false;
  551. _mediaLoaded = true;
  552. _mediaDuration = Reflect.field(_youtubeLoader.content, "getDuration")();
  553. _aspectRatio = AspectRatio.getAspectRatio(_videoWidth, _videoHeight);
  554. _originalAspectRatio = _aspectRatio;
  555. callEvents(PlayerEvents.CONNECTION_SUCCESS);
  556. callEvents(PlayerEvents.MEDIA_INITIALIZED);
  557. resizeAndCenterPlayer();
  558. //Retrieve the volume that user selected last time
  559. setVolume(_userSettings.getVolume());
  560. }
  561. callEvents(PlayerEvents.NOT_BUFFERING);
  562. case 2:
  563. callEvents(PlayerEvents.NOT_BUFFERING);
  564. case 3:
  565. callEvents(PlayerEvents.BUFFERING);
  566. case 5:
  567. callEvents(PlayerEvents.NOT_BUFFERING);
  568. }
  569. }
  570. /**
  571. * This event is fired whenever the video playback quality changes. For example, if you call the
  572. * setPlaybackQuality(suggestedQuality) function, this event will fire if the playback quality actually
  573. * changes. Your code should respond to the event and should not assume that the quality will automatically
  574. * change when the setPlaybackQuality(suggestedQuality) function is called. Similarly, your code should not
  575. * assume that playback quality will only change as a result of an explicit call to setPlaybackQuality or any
  576. * other function that allows you to set a suggested playback quality.
  577. *
  578. * The value that the event broadcasts is the new playback quality. Possible values are "small", "medium",
  579. * "large" and "hd720".
  580. * @param event
  581. */
  582. private function onYoutubePlaybackQualityChange(event:Event):Void
  583. {
  584. //trace(Reflect.field(event, "data"));
  585. }
  586. /**
  587. * This event is fired when an error in the player occurs. The possible error codes are 100, 101,
  588. * and 150. The 100 error code is broadcast when the video requested is not found. This occurs when
  589. * a video has been removed (for any reason), or it has been marked as private. The 101 error code is
  590. * broadcast when the video requested does not allow playback in the embedded players. The error code
  591. * 150 is the same as 101, it's just 101 in disguise!
  592. * @param event
  593. */
  594. private function onYoutubeError(event:Event):Void
  595. {
  596. trace(Reflect.field(event, "data"));
  597. }
  598. //}
  599. //{Private Methods
  600. /**
  601. * Function used each time is needed to dispatch an event
  602. * @param type
  603. */
  604. private function callEvents(type:String):Void
  605. {
  606. var playerEvent:PlayerEvents = new PlayerEvents(type, true);
  607. playerEvent.aspectRatio = getAspectRatio();
  608. playerEvent.duration = getDuration();
  609. playerEvent.fullscreen = isFullscreen();
  610. playerEvent.mute = getMute();
  611. playerEvent.volume = getVolume();
  612. playerEvent.width = _video.width;
  613. playerEvent.height = _video.height;
  614. playerEvent.stream = getNetStream();
  615. playerEvent.sound = getSound();
  616. playerEvent.time = getCurrentTime();
  617. playerEvent.id3Info = getId3Info();
  618. dispatchEvent(playerEvent);
  619. }
  620. /**
  621. * Reposition and resizes the video player to fit on screen
  622. */
  623. private function resizeAndCenterPlayer():Void
  624. {
  625. if (_streamType != StreamType.YOUTUBE)
  626. {
  627. _video.height = _stage.stageHeight;
  628. _video.width = _video.height * _aspectRatio;
  629. _video.x = (_stage.stageWidth / 2) - (_video.width / 2);
  630. _video.y = 0;
  631. if (_video.width > _stage.stageWidth && _aspectRatio == _originalAspectRatio)
  632. {
  633. var aspectRatio:Float = _videoHeight / _videoWidth;
  634. _video.width = _stage.stageWidth;
  635. _video.height = aspectRatio * _video.width;
  636. _video.x = 0;
  637. _video.y = (_stage.stageHeight / 2) - (_video.height / 2);
  638. }
  639. _videoMask.graphics.clear();
  640. _videoMask.graphics.lineStyle();
  641. _videoMask.graphics.beginFill(0x000000, 0);
  642. _videoMask.graphics.drawRect(_video.x, _video.y, _video.width, _video.height);
  643. _videoMask.graphics.endFill();
  644. }
  645. else
  646. {
  647. Reflect.field(_youtubeLoader.content, "setSize")(_stage.stageWidth, _stage.stageHeight);
  648. _videoMask.graphics.clear();
  649. _videoMask.graphics.lineStyle();
  650. _videoMask.graphics.beginFill(0x000000, 0);
  651. _videoMask.graphics.drawRect(0, 0, _stage.stageWidth, _stage.stageHeight);
  652. _videoMask.graphics.endFill();
  653. }
  654. callEvents(PlayerEvents.RESIZE);
  655. }
  656. /**
  657. * Check the best seek point available if the seekpoints array is available
  658. * @param time time in seconds
  659. * @return best seek point in seconds or given one if no seekpoints array is available
  660. */
  661. private function getBestSeekPoint(time:Float):Float
  662. {
  663. if (_seekPoints.length > 0)
  664. {
  665. var timeOne:String="0";
  666. var timeTwo:String="0";
  667. for(prop in Reflect.fields(_seekPoints))
  668. {
  669. if(Reflect.field(_seekPoints,prop) < time)
  670. {
  671. timeOne = prop;
  672. }
  673. else
  674. {
  675. timeTwo = prop;
  676. break;
  677. }
  678. }
  679. if(time - _seekPoints[Std.parseInt(timeOne)] < _seekPoints[Std.parseInt(timeTwo)] - time)
  680. {
  681. return _seekPoints[Std.parseInt(timeOne)];
  682. }
  683. else
  684. {
  685. return _seekPoints[Std.parseInt(timeTwo)];
  686. }
  687. }
  688. return time;
  689. }
  690. /**
  691. * Checks if the given seek time is already buffered
  692. * @param time time in seconds
  693. * @return true if can seek false if not in buffer
  694. */
  695. private function canSeek(time:Float):Bool
  696. {
  697. if (_type == InputType.VIDEO)
  698. {
  699. time = getBestSeekPoint(time);
  700. }
  701. var cacheTotal = Math.floor((getDuration() - _startTime) * (getBytesLoaded() / getBytesTotal())) - 1;
  702. if(time >= _startTime && time < _startTime + cacheTotal)
  703. {
  704. return true;
  705. }
  706. return false;
  707. }
  708. //}
  709. //{Public methods
  710. /**
  711. * Loads a video and starts playing it
  712. * @param video video url to load
  713. */
  714. public function load(source:String, type:String="video", streamType:String="file", server:String=""):Void
  715. {
  716. stopAndClose();
  717. _type = type;
  718. _streamType = streamType;
  719. _mediaSource = source;
  720. _stopped = false;
  721. _mediaLoaded = false;
  722. _firstLoad = true;
  723. _startTime = 0;
  724. _downloadCompleted = false;
  725. _seekPoints = new Array();
  726. _server = server;
  727. callEvents(PlayerEvents.BUFFERING);
  728. if (_streamType == StreamType.YOUTUBE)
  729. {
  730. Security.allowDomain("*");
  731. Security.allowDomain("www.youtube.com");
  732. Security.allowDomain("youtube.com");
  733. Security.allowDomain("s.ytimg.com");
  734. Security.allowDomain("i.ytimg.com");
  735. _youtubeLoader = new Loader();
  736. _youtubeLoader.contentLoaderInfo.addEventListener(Event.INIT, onYouTubeLoaderInit);
  737. _youtubeLoader.load(new URLRequest("http://www.youtube.com/apiplayer?version=3"));
  738. }
  739. else if (_type == InputType.VIDEO && (_streamType == StreamType.FILE || _streamType == StreamType.PSEUDOSTREAM))
  740. {
  741. _connection.connect(null);
  742. _stream = new NetStream(_connection);
  743. _stream.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);
  744. _stream.bufferTime = _bufferTime;
  745. _stream.play(source);
  746. _stream.client = this;
  747. _video.attachNetStream(_stream);
  748. }
  749. else if (_type == InputType.VIDEO && _streamType == StreamType.RTMP)
  750. {
  751. _connection.connect(_server);
  752. }
  753. else if (_type == InputType.AUDIO && _streamType == StreamType.RTMP)
  754. {
  755. _connection.connect(_server);
  756. }
  757. else if(_type == InputType.AUDIO && _streamType == StreamType.FILE)
  758. {
  759. _sound.load(new URLRequest(source));
  760. }
  761. }
  762. /**
  763. * Closes the connection and makes player available for another video
  764. */
  765. public function stopAndClose():Void
  766. {
  767. if (_mediaLoaded)
  768. {
  769. _mediaLoaded = false;
  770. _isPlaying = false;
  771. _stopped = true;
  772. _startTime = 0;
  773. if (_streamType == StreamType.YOUTUBE)
  774. {
  775. Reflect.field(_youtubeLoader.content, "destroy")();
  776. }
  777. else if (_type == InputType.VIDEO)
  778. {
  779. _stream.close();
  780. }
  781. else
  782. {
  783. _soundChannel.stop();
  784. _sound.close();
  785. }
  786. }
  787. callEvents(PlayerEvents.STOP_CLOSE);
  788. }
  789. /**
  790. * Seeks 8 seconds forward from the current position.
  791. * @return current play time after forward
  792. */
  793. public function forward():Float
  794. {
  795. var seekTime = (getCurrentTime() + 8) + _startTime;
  796. if (getDuration() > seekTime)
  797. {
  798. seekTime = seek(seekTime);
  799. }
  800. return seekTime;
  801. }
  802. /**
  803. * Seeks 8 seconds back from the current position.
  804. * @return current play time after rewind
  805. */
  806. public function rewind():Float
  807. {
  808. var seekTime = (getCurrentTime() - 8) + _startTime;
  809. if (seekTime >= _startTime)
  810. {
  811. seekTime = seek(seekTime);
  812. }
  813. return seekTime;
  814. }
  815. /**
  816. * Seeks video player to a given time in seconds
  817. * @param seekTime time in seconds to seek
  818. * @return current play time after seeking
  819. */
  820. public function seek(seekTime:Float):Float
  821. {
  822. if (_startTime <= 1 && _downloadCompleted)
  823. {
  824. if (_type == InputType.VIDEO || _streamType == StreamType.RTMP)
  825. {
  826. _stream.seek(seekTime);
  827. }
  828. else if (_type == InputType.AUDIO)
  829. {
  830. _soundChannel.stop();
  831. _soundChannel = _sound.play(seekTime * 1000);
  832. if (!_isPlaying)
  833. {
  834. _soundChannel.stop();
  835. }
  836. setVolume(_userSettings.getVolume());
  837. }
  838. }
  839. else if(_seekPoints.length > 0 && _streamType == StreamType.PSEUDOSTREAM)
  840. {
  841. seekTime = getBestSeekPoint(seekTime);
  842. if (canSeek(seekTime))
  843. {
  844. _stream.seek(seekTime - _startTime);
  845. }
  846. else if(seekTime != _startTime)
  847. {
  848. _startTime = seekTime;
  849. var url:String;
  850. if (_mediaSource.indexOf("?") != -1)
  851. {
  852. url = _mediaSource + "&start=" + seekTime;
  853. }
  854. else
  855. {
  856. url = _mediaSource + "?start=" + seekTime;
  857. }
  858. _stream.play(url);
  859. }
  860. }
  861. else if (_streamType == StreamType.YOUTUBE)
  862. {
  863. if (!canSeek(seekTime))
  864. {
  865. _startTime = seekTime;
  866. Reflect.field(_youtubeLoader.content, "seekTo")(seekTime);
  867. }
  868. else
  869. {
  870. Reflect.field(_youtubeLoader.content, "seekTo")(seekTime);
  871. }
  872. }
  873. else if (_streamType == StreamType.RTMP)
  874. {
  875. // seekTime = getBestSeekPoint(seekTime); //Not Needed?
  876. _stream.seek(seekTime);
  877. }
  878. else if(canSeek(seekTime))
  879. {
  880. if (_type == InputType.VIDEO || _streamType == StreamType.RTMP)
  881. {
  882. _stream.seek(seekTime);
  883. }
  884. else if (_type == InputType.AUDIO)
  885. {
  886. _soundChannel.stop();
  887. _soundChannel = _sound.play(seekTime * 1000);
  888. if (!_isPlaying)
  889. {
  890. _soundChannel.stop();
  891. }
  892. setVolume(_userSettings.getVolume());
  893. }
  894. }
  895. callEvents(PlayerEvents.SEEK);
  896. return seekTime;
  897. }
  898. /**
  899. * To check wheter the media is playing
  900. * @return true if is playing false otherwise
  901. */
  902. public function isPlaying():Bool
  903. {
  904. return _isPlaying;
  905. }
  906. /**
  907. * Cycle betewen aspect ratios
  908. * @return new aspect ratio in use
  909. */
  910. public function toggleAspectRatio():Float
  911. {
  912. switch(_currentAspectRatio)
  913. {
  914. case "original":
  915. _aspectRatio = AspectRatio._1_1;
  916. _currentAspectRatio = "1:1";
  917. case "1:1":
  918. _aspectRatio = AspectRatio._3_2;
  919. _currentAspectRatio = "3:2";
  920. case "3:2":
  921. _aspectRatio = AspectRatio._4_3;
  922. _currentAspectRatio = "4:3";
  923. case "4:3":
  924. _aspectRatio = AspectRatio._5_4;
  925. _currentAspectRatio = "5:4";
  926. case "5:4":
  927. _aspectRatio = AspectRatio._14_9;
  928. _currentAspectRatio = "14:9";
  929. case "14:9":
  930. _aspectRatio = AspectRatio._14_10;
  931. _currentAspectRatio = "14:10";
  932. case "14:10":
  933. _aspectRatio = AspectRatio._16_9;
  934. _currentAspectRatio = "16:9";
  935. case "16:9":
  936. _aspectRatio = AspectRatio._16_10;
  937. _currentAspectRatio = "16:10";
  938. case "16:10":
  939. _aspectRatio = _originalAspectRatio;
  940. _currentAspectRatio = "original";
  941. default:
  942. _aspectRatio = _originalAspectRatio;
  943. _currentAspectRatio = "original";
  944. }
  945. resizeAndCenterPlayer();
  946. callEvents(PlayerEvents.ASPECT_RATIO);
  947. //Store aspect ratio into user settings
  948. if (_aspectRatio == _originalAspectRatio)
  949. {
  950. _userSettings.setAspectRatio(0.0);
  951. }
  952. else
  953. {
  954. _userSettings.setAspectRatio(_aspectRatio);
  955. }
  956. return _aspectRatio;
  957. }
  958. /**
  959. * Swithces between play and pause
  960. */
  961. public function togglePlay():Bool
  962. {
  963. if (_mediaLoaded)
  964. {
  965. if (_mediaEndReached)
  966. {
  967. _mediaEndReached = false;
  968. if (_streamType == StreamType.YOUTUBE)
  969. {
  970. Reflect.field(_youtubeLoader.content, "seekTo")(0);
  971. Reflect.field(_youtubeLoader.content, "playVideo")();
  972. }
  973. else if (_type == InputType.VIDEO || _streamType == StreamType.RTMP)
  974. {
  975. _stream.seek(0);
  976. _stream.togglePause();
  977. }
  978. else if (_type == InputType.AUDIO)
  979. {
  980. _checkAudioTimer.start();
  981. _soundChannel = _sound.play();
  982. setVolume(_userSettings.getVolume());
  983. }
  984. }
  985. else if (_mediaLoaded)
  986. {
  987. if (_streamType == StreamType.YOUTUBE)
  988. {
  989. if (_isPlaying)
  990. {
  991. Reflect.field(_youtubeLoader.content, "pauseVideo")();
  992. }
  993. else
  994. {
  995. Reflect.field(_youtubeLoader.content, "playVideo")();
  996. }
  997. }
  998. else if (_type == InputType.VIDEO || _streamType == StreamType.RTMP)
  999. {
  1000. _stream.togglePause();
  1001. }
  1002. else if (_type == InputType.AUDIO)
  1003. {
  1004. if (_isPlaying)
  1005. {
  1006. _soundChannel.stop();
  1007. }
  1008. else
  1009. {
  1010. //If end of audio reached start from beggining
  1011. if (_soundChannel.position + 100 >= _sound.length)
  1012. {
  1013. _soundChannel = _sound.play();
  1014. }
  1015. else
  1016. {
  1017. _soundChannel = _sound.play(_soundChannel.position);
  1018. }
  1019. setVolume(_userSettings.getVolume());
  1020. }
  1021. }
  1022. }
  1023. else if (_stopped)
  1024. {
  1025. load(_mediaSource, _type, _streamType, _server);
  1026. }
  1027. _isPlaying = !_isPlaying;
  1028. callEvents(PlayerEvents.PLAY_PAUSE);
  1029. return _isPlaying;
  1030. }
  1031. else if(_mediaSource != "")
  1032. {
  1033. load(_mediaSource, _type, _streamType, _server);
  1034. callEvents(PlayerEvents.BUFFERING);
  1035. return true;
  1036. }
  1037. callEvents(PlayerEvents.PLAY_PAUSE);
  1038. return false;
  1039. }
  1040. /**
  1041. * Switches on or off fullscreen
  1042. * @return true if fullscreen otherwise false
  1043. */
  1044. public function toggleFullscreen():Bool
  1045. {
  1046. if (_fullscreen)
  1047. {
  1048. _stage.displayState = StageDisplayState.NORMAL;
  1049. _stage.focus = _stage;
  1050. return false;
  1051. }
  1052. else
  1053. {
  1054. if (_useHardWareScaling)
  1055. {
  1056. //Match full screen aspec ratio to desktop
  1057. var aspectRatio = Capabilities.screenResolutionY / Capabilities.screenResolutionX;
  1058. _stage.fullScreenSourceRect = new Rectangle(0, 0, _videoWidth, _videoWidth * aspectRatio);
  1059. }
  1060. else
  1061. {
  1062. //Use desktop resolution
  1063. _stage.fullScreenSourceRect = new Rectangle(0, 0, Capabilities.screenResolutionX ,Capabilities.screenResolutionY);
  1064. }
  1065. _stage.displayState = StageDisplayState.FULL_SCREEN;
  1066. _stage.focus = _stage;
  1067. return true;
  1068. }
  1069. }
  1070. /**
  1071. * Toggles betewen high and low quality image rendering
  1072. * @return true if quality high false otherwise
  1073. */
  1074. public function toggleQuality():Bool
  1075. {
  1076. if (_videoQualityHigh)
  1077. {
  1078. _video.smoothing = false;
  1079. _video.deblocking = 1;
  1080. }
  1081. else
  1082. {
  1083. _video.smoothing = true;
  1084. _video.deblocking = 5;
  1085. }
  1086. _videoQualityHigh = _videoQualityHigh?false:true;
  1087. return _videoQualityHigh;
  1088. }
  1089. /**
  1090. * Mutes or unmutes the sound
  1091. * @return true if muted false if unmuted
  1092. */
  1093. public function toggleMute():Bool
  1094. {
  1095. var soundTransform:SoundTransform = new SoundTransform();
  1096. var isMute:Bool;
  1097. //unmute sound
  1098. if (_soundMuted)
  1099. {
  1100. _soundMuted = false;
  1101. if (_volume > 0)
  1102. {
  1103. soundTransform.volume = _volume;
  1104. }
  1105. else
  1106. {
  1107. _volume = 1.0;
  1108. soundTransform.volume = _volume;
  1109. }
  1110. isMute = false;
  1111. }
  1112. //mute sound
  1113. else
  1114. {
  1115. _soundMuted = true;
  1116. _volume = _stream.soundTransform.volume;
  1117. soundTransform.volume = 0;
  1118. _stream.soundTransform = soundTransform;
  1119. isMute = true;
  1120. }
  1121. if (_streamType == StreamType.YOUTUBE)
  1122. {
  1123. Reflect.field(_youtubeLoader.content, "setVolume")(soundTransform.volume * 100);
  1124. }
  1125. else if (_type == InputType.VIDEO || _streamType == StreamType.RTMP)
  1126. {
  1127. _stream.soundTransform = soundTransform;
  1128. }
  1129. else if (_type == InputType.AUDIO)
  1130. {
  1131. _soundChannel.soundTransform = soundTransform;
  1132. setVolume(_userSettings.getVolume());
  1133. }
  1134. callEvents(PlayerEvents.MUTE);
  1135. return isMute;
  1136. }
  1137. /**
  1138. * Check if player is running on fullscreen mode
  1139. * @return true if fullscreen false if not
  1140. */
  1141. public function isFullscreen():Bool
  1142. {
  1143. return _stage.displayState == StageDisplayState.FULL_SCREEN;
  1144. }
  1145. /**
  1146. * Raises the volume
  1147. * @return volume value after raising
  1148. */
  1149. public function volumeUp():Float
  1150. {
  1151. var soundTransform:SoundTransform = new SoundTransform();
  1152. if (_soundMuted)
  1153. {
  1154. _soundMuted = false;
  1155. }
  1156. //raise volume if not already at max
  1157. if (_volume < 1)
  1158. {
  1159. if (_streamType == StreamType.YOUTUBE)
  1160. {
  1161. _volume = (Reflect.field(_youtubeLoader.content, "getVolume")() + 10) / 100;
  1162. Reflect.field(_youtubeLoader.content, "setVolume")(_volume * 100);
  1163. }
  1164. else if (_type == InputType.VIDEO || _streamType == StreamType.RTMP)
  1165. {
  1166. _volume = _stream.soundTransform.volume + (10/100);
  1167. soundTransform.volume = _volume;
  1168. _stream.soundTransform = soundTransform;
  1169. }
  1170. else if (_type == InputType.AUDIO)
  1171. {
  1172. _volume = _soundChannel.soundTransform.volume + (10/100);
  1173. soundTransform.volume = _volume;
  1174. _soundChannel.soundTransform = soundTransform;
  1175. }
  1176. }
  1177. //reset volume to 1.0 if already reached max
  1178. if (_volume >= 1)
  1179. {
  1180. _volume = 1.0;
  1181. }
  1182. //Store volume into user settings
  1183. _userSettings.setVolume(_volume);
  1184. callEvents(PlayerEvents.VOLUME_UP);
  1185. return _volume;
  1186. }
  1187. /**
  1188. * Lower the volume
  1189. * @return volume value after lowering
  1190. */
  1191. public function volumeDown():Float
  1192. {
  1193. var soundTransform:SoundTransform = new SoundTransform();
  1194. //lower sound
  1195. if(!_soundMuted)
  1196. {
  1197. if (_streamType == StreamType.YOUTUBE)
  1198. {
  1199. _volume = (Reflect.field(_youtubeLoader.content, "getVolume")() - 10) / 100;
  1200. Reflect.field(_youtubeLoader.content, "setVolume")(_volume * 100);
  1201. }
  1202. else if (_type == InputType.VIDEO || _streamType == StreamType.RTMP)
  1203. {
  1204. _volume = _stream.soundTransform.volume - (10/100);
  1205. soundTransform.volume = _volume;
  1206. _stream.soundTransform = soundTransform;
  1207. }
  1208. else if (_type == InputType.AUDIO)
  1209. {
  1210. _volume = _soundChannel.soundTransform.volume - (10/100);
  1211. soundTransform.volume = _volume;
  1212. _soundChannel.soundTransform = soundTransform;
  1213. }
  1214. //if volume reached min is muted
  1215. if (_volume <= 0)
  1216. {
  1217. _soundMuted = true;
  1218. _volume = 0;
  1219. }
  1220. }
  1221. //Store volume into user settings
  1222. _userSettings.setVolume(_volume);
  1223. callEvents(PlayerEvents.VOLUME_DOWN);
  1224. return _volume;
  1225. }
  1226. //}
  1227. //{Setters
  1228. /**
  1229. * Set input type
  1230. * @param type Allowable values are audio, video
  1231. */
  1232. public function setType(type:String):Void
  1233. {
  1234. _type = type;
  1235. }
  1236. /**
  1237. * Set streaming type
  1238. * @param streamType Allowable values are file, http, rmtp
  1239. */
  1240. public function setStreamType(streamType:String):Void
  1241. {
  1242. _streamType = streamType;
  1243. }
  1244. /**
  1245. * Sets the server url for rtmp streams
  1246. * @param server
  1247. */
  1248. public function setServer(server:String):Void
  1249. {
  1250. _server = server;
  1251. }
  1252. /**
  1253. * To set the video source in case we dont want to start downloading at first so when use tooglePlay the
  1254. * media is loaded automatically
  1255. * @param source
  1256. */
  1257. public function setSource(source):Void
  1258. {
  1259. _mediaSource = source;
  1260. }
  1261. /**
  1262. * Changes the current volume
  1263. * @param volume
  1264. */
  1265. public function setVolume(volume:Float):Void
  1266. {
  1267. var soundTransform:SoundTransform = new SoundTransform();
  1268. if (volume > _volume) {
  1269. callEvents(PlayerEvents.VOLUME_UP);
  1270. }
  1271. if (volume < _volume) {
  1272. callEvents(PlayerEvents.VOLUME_DOWN);
  1273. }
  1274. if (volume > 0)
  1275. {
  1276. _soundMuted = false;
  1277. _volume = volume;
  1278. }
  1279. else
  1280. {
  1281. _soundMuted = true;
  1282. _volume = 1.0;
  1283. }
  1284. soundTransform.volume = volume;
  1285. if (!_firstLoad) //To prevent errors if objects aren't initialized
  1286. {
  1287. if (_streamType == StreamType.YOUTUBE)
  1288. {
  1289. Reflect.field(_youtubeLoader.content, "setVolume")(soundTransform.volume * 100);
  1290. }
  1291. else if (_type == InputType.VIDEO || _streamType == StreamType.RTMP)
  1292. {
  1293. _stream.soundTransform = soundTransform;
  1294. }
  1295. else if (_type == InputType.AUDIO)
  1296. {
  1297. _soundChannel.soundTransform = soundTransform;
  1298. }
  1299. }
  1300. //Store volume into user settings
  1301. _userSettings.setVolume(_volume);
  1302. callEvents(PlayerEvents.VOLUME_CHANGE);
  1303. }
  1304. /**
  1305. * Changes the buffer time for local and pseudo streaming
  1306. * @param time in seconds
  1307. */
  1308. public function setBufferTime(time:Float):Void
  1309. {
  1310. if (time > 0)
  1311. {
  1312. _bufferTime = time;
  1313. }
  1314. }
  1315. /**
  1316. * Changes the aspec ratio of current playing media and resizes video player
  1317. * @param aspectRatio new aspect ratio value
  1318. */
  1319. public function setAspectRatio(aspectRatio:Float):Void
  1320. {
  1321. _aspectRatio = aspectRatio;
  1322. switch(_aspectRatio)
  1323. {
  1324. case 0.0:
  1325. _currentAspectRatio = "original";
  1326. case AspectRatio._1_1:
  1327. _currentAspectRatio = "1:1";
  1328. case AspectRatio._3_2:
  1329. _currentAspectRatio = "3:2";
  1330. case AspectRatio._4_3:
  1331. _currentAspectRatio = "4:3";
  1332. case AspectRatio._5_4:
  1333. _currentAspectRatio = "5:4";
  1334. case AspectRatio._14_9:
  1335. _currentAspectRatio = "14:9";
  1336. case AspectRatio._14_10:
  1337. _currentAspectRatio = "14:10";
  1338. case AspectRatio._16_9:
  1339. _currentAspectRatio = "16:9";
  1340. case AspectRatio._16_10:
  1341. _currentAspectRatio = "16:10";
  1342. }
  1343. resizeAndCenterPlayer();
  1344. //Store aspect ratio into user settings
  1345. _userSettings.setAspectRatio(_aspectRatio);
  1346. }
  1347. /**
  1348. * Enable or disable hardware scaling
  1349. * @param value true to enable false to disable
  1350. */
  1351. public function setHardwareScaling(value:Bool):Void
  1352. {
  1353. _useHardWareScaling = value;
  1354. }
  1355. //}
  1356. //{Getters
  1357. /**
  1358. * Gets the volume amount 0.0 to 1.0
  1359. * @return
  1360. */
  1361. public function getVolume():Float
  1362. {
  1363. return _volume;
  1364. }
  1365. /**
  1366. * The current aspect ratio of the loaded Player
  1367. * @return
  1368. */
  1369. public function getAspectRatio():Float
  1370. {
  1371. return _aspectRatio;
  1372. }
  1373. /**
  1374. * The current aspect ratio of the loaded Player in string format
  1375. * @return
  1376. */
  1377. public function getAspectRatioString():String
  1378. {
  1379. return _currentAspectRatio;
  1380. }
  1381. /**
  1382. * Original aspect ratio of the video
  1383. * @return original aspect ratio
  1384. */
  1385. public function getOriginalAspectRatio():Float
  1386. {
  1387. return _originalAspectRatio;
  1388. }
  1389. /**
  1390. * Total duration time of the loaded media
  1391. * @return time in seconds
  1392. */
  1393. public function getDuration():Float
  1394. {
  1395. return _mediaDuration;
  1396. }
  1397. /**
  1398. * The time in seconds where the player started downloading
  1399. * @return time in seconds
  1400. */
  1401. public function getStartTime():Float
  1402. {
  1403. return _startTime;
  1404. }
  1405. /**
  1406. * The stream associated with the player
  1407. * @return netstream object
  1408. */
  1409. public function getNetStream():NetStream
  1410. {
  1411. return _stream;
  1412. }
  1413. /**
  1414. * Video object associated to the player
  1415. * @return video object for further manipulation
  1416. */
  1417. public function getVideo():Video
  1418. {
  1419. return _video;
  1420. }
  1421. /**
  1422. * Sound object associated to the player
  1423. * @return sound object for further manipulation
  1424. */
  1425. public function getSound():Sound
  1426. {
  1427. return _sound;
  1428. }
  1429. /**
  1430. * The id3 info of sound object
  1431. * @return
  1432. */
  1433. public function getId3Info():ID3Info
  1434. {
  1435. return _id3Info;
  1436. }
  1437. /**
  1438. * The current sound state
  1439. * @return true if mute otherwise false
  1440. */
  1441. public function getMute():Bool
  1442. {
  1443. return _soundMuted;
  1444. }
  1445. /**
  1446. * The amount of total bytes
  1447. * @return amount of bytes
  1448. */
  1449. public function getBytesTotal():Float
  1450. {
  1451. var bytesTotal:Float = 0;
  1452. if (_streamType == StreamType.YOUTUBE)
  1453. {
  1454. if(_youtubeLoader != null && _mediaLoaded)
  1455. bytesTotal = Reflect.field(_youtubeLoader.content, "getVideoBytesTotal")();
  1456. }
  1457. else if (_type == InputType.VIDEO || _streamType == StreamType.RTMP)
  1458. {
  1459. bytesTotal = _stream.bytesTotal;
  1460. }
  1461. else if (_type == InputType.AUDIO)
  1462. {
  1463. bytesTotal = _sound.bytesTotal;
  1464. }
  1465. return bytesTotal;
  1466. }
  1467. /**
  1468. * The amount of bytes loaded
  1469. * @return amount of bytes
  1470. */
  1471. public function getBytesLoaded():Float
  1472. {
  1473. var bytesLoaded:Float = 0;
  1474. if (_streamType == StreamType.YOUTUBE)
  1475. {
  1476. if(_youtubeLoader != null && _mediaLoaded)
  1477. bytesLoaded = Reflect.field(_youtubeLoader.content, "getVideoBytesLoaded")();
  1478. }
  1479. else if (_type == InputType.VIDEO || _streamType == StreamType.RTMP)
  1480. {
  1481. bytesLoaded = _stream.bytesLoaded;
  1482. }
  1483. else if (_type == InputType.AUDIO)
  1484. {
  1485. bytesLoaded = _sound.bytesLoaded;
  1486. }
  1487. return bytesLoaded;
  1488. }
  1489. /**
  1490. * Current playing file type
  1491. * @return audio or video
  1492. */
  1493. public function getType():String
  1494. {
  1495. return _type;
  1496. }
  1497. /**
  1498. * The stream method for the current playing media
  1499. * @return
  1500. */
  1501. public function getStreamType():String
  1502. {
  1503. return _streamType;
  1504. }
  1505. /**
  1506. * The server url for current rtmp stream
  1507. * @return
  1508. */
  1509. public function getServer():String
  1510. {
  1511. return _server;
  1512. }
  1513. /**
  1514. * To check current quality mode
  1515. * @return true if high quality false if low
  1516. */
  1517. public function getQuality():Bool
  1518. {
  1519. return _videoQualityHigh;
  1520. }
  1521. /**
  1522. * The current playing time
  1523. * @return current playing time in seconds
  1524. */
  1525. public function getCurrentTime():Float
  1526. {
  1527. var time:Float = 0;
  1528. if (_streamType == StreamType.YOUTUBE)
  1529. {
  1530. if(_youtubeLoader != null)
  1531. {
  1532. time = Reflect.field(_youtubeLoader.content, "getCurrentTime")();
  1533. }
  1534. else
  1535. {
  1536. time = 0;
  1537. }
  1538. }
  1539. else if (_streamType == StreamType.PSEUDOSTREAM)
  1540. {
  1541. time = getStartTime() + _stream.time;
  1542. }
  1543. else if (_type == InputType.VIDEO || _streamType == StreamType.RTMP)
  1544. {
  1545. time = _stream.time;
  1546. }
  1547. else if (_type == InputType.AUDIO)
  1548. {
  1549. if(_soundChannel != null)
  1550. {
  1551. time = _soundChannel.position / 1000;
  1552. }
  1553. else
  1554. {
  1555. time = 0;
  1556. }
  1557. }
  1558. return time;
  1559. }
  1560. //}
  1561. }