PageRenderTime 75ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/media-element/src/flash/htmlelements/YouTubeElement.as

https://gitlab.com/Gashler/sg
ActionScript | 373 lines | 271 code | 72 blank | 30 comment | 32 complexity | ffbdef28c1c2048b6c2d97246bc0d0af MD5 | raw file
  1. package htmlelements {
  2. import flash.display.Sprite;
  3. import flash.events.*;
  4. import flash.net.NetConnection;
  5. import flash.net.NetStream;
  6. import flash.media.Video;
  7. import flash.media.SoundTransform;
  8. import flash.utils.Timer;
  9. import flash.net.URLLoader;
  10. import flash.net.URLRequest;
  11. import flash.net.URLVariables;
  12. import flash.net.URLRequestMethod;
  13. import flash.display.MovieClip;
  14. import flash.display.Loader;
  15. import flash.display.DisplayObject;
  16. import FlashMediaElement;
  17. import HtmlMediaEvent;
  18. public class YouTubeElement extends Sprite implements IMediaElement {
  19. private var _currentUrl:String = "";
  20. private var _autoplay:Boolean = true;
  21. private var _preload:String = "";
  22. private var _element:FlashMediaElement;
  23. // event values
  24. private var _currentTime:Number = 0;
  25. private var _duration:Number = 0;
  26. private var _framerate:Number;
  27. private var _isPaused:Boolean = true;
  28. private var _isEnded:Boolean = false;
  29. private var _volume:Number = 1;
  30. private var _isMuted:Boolean = false;
  31. private var _bytesLoaded:Number = 0;
  32. private var _bytesTotal:Number = 0;
  33. private var _bufferedTime:Number = 0;
  34. private var _bufferEmpty:Boolean = false;
  35. private var _videoWidth:Number = -1;
  36. private var _videoHeight:Number = -1;
  37. private var _timer:Timer;
  38. // YouTube stuff
  39. private var _playerLoader:Loader;
  40. private var _player:DisplayObject = null;
  41. private var _playerIsLoaded:Boolean = false;
  42. private var _youTubeId:String = "";
  43. //http://code.google.com/p/gdata-samples/source/browse/trunk/ytplayer/actionscript3/com/google/youtube/examples/AS3Player.as
  44. private static const WIDESCREEN_ASPECT_RATIO:String = "widescreen";
  45. private static const QUALITY_TO_PLAYER_WIDTH:Object = {
  46. small: 320,
  47. medium: 640,
  48. large: 854,
  49. hd720: 1280
  50. };
  51. private static const STATE_ENDED:Number = 0;
  52. private static const STATE_PLAYING:Number = 1;
  53. private static const STATE_PAUSED:Number = 2;
  54. private static const STATE_CUED:Number = 5;
  55. public function get player():DisplayObject {
  56. return _player;
  57. }
  58. public function setSize(width:Number, height:Number):void {
  59. if (player != null) {
  60. player.setSize(width, height);
  61. } else {
  62. initHeight = height;
  63. initWidth = width;
  64. }
  65. }
  66. public function get videoHeight():Number {
  67. return _videoHeight;
  68. }
  69. public function get videoWidth():Number {
  70. return _videoWidth;
  71. }
  72. public function duration():Number {
  73. return _duration;
  74. }
  75. public function currentProgress():Number {
  76. if(_bytesTotal> 0) {
  77. return Math.round(_bytesLoaded/_bytesTotal*100);
  78. } else {
  79. return 0;
  80. }
  81. }
  82. public function currentTime():Number {
  83. return _currentTime;
  84. }
  85. public var initHeight:Number;
  86. public var initWidth:Number;
  87. // (1) load()
  88. // calls _connection.connect();
  89. // waits for NetConnection.Connect.Success
  90. // _stream gets created
  91. private var _isChromeless:Boolean = false;
  92. public function YouTubeElement(element:FlashMediaElement, autoplay:Boolean, preload:String, timerRate:Number, startVolume:Number):void {
  93. _element = element;
  94. _autoplay = autoplay;
  95. _volume = startVolume;
  96. _preload = preload;
  97. initHeight = 0;
  98. initWidth = 0;
  99. _playerLoader = new Loader();
  100. _playerLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, playerLoaderInitHandler);
  101. // chromeless
  102. if (_isChromeless) {
  103. _playerLoader.load(new URLRequest("//www.youtube.com/apiplayer?version=3&controls=1&rel=0&showinfo=0&iv_load_policy=1"));
  104. }
  105. _timer = new Timer(timerRate);
  106. _timer.addEventListener("timer", timerHandler);
  107. _timer.start();
  108. }
  109. private function playerLoaderInitHandler(event:Event):void {
  110. trace("yt player init");
  111. _element.addChild(_playerLoader.content);
  112. _element.setControlDepth();
  113. _playerLoader.content.addEventListener("onReady", onPlayerReady);
  114. _playerLoader.content.addEventListener("onError", onPlayerError);
  115. _playerLoader.content.addEventListener("onStateChange", onPlayerStateChange);
  116. _playerLoader.content.addEventListener("onPlaybackQualityChange", onVideoPlaybackQualityChange);
  117. }
  118. private function onPlayerReady(event:Event):void {
  119. _playerIsLoaded = true;
  120. _player = _playerLoader.content;
  121. if (initHeight > 0 && initWidth > 0)
  122. setSize(initWidth, initHeight);
  123. if (_youTubeId != "") { // && _isChromeless) {
  124. if (_autoplay) {
  125. player.loadVideoById(_youTubeId);
  126. } else {
  127. player.cueVideoById(_youTubeId);
  128. }
  129. _timer.start();
  130. }
  131. }
  132. private function onPlayerError(event:Event):void {
  133. // trace("Player error:", Object(event).data);
  134. }
  135. private function onPlayerStateChange(event:Event):void {
  136. trace("State is", Object(event).data);
  137. _duration = player.getDuration();
  138. switch (Object(event).data) {
  139. case STATE_ENDED:
  140. _isEnded = true;
  141. _isPaused = false;
  142. sendEvent(HtmlMediaEvent.ENDED);
  143. break;
  144. case STATE_PLAYING:
  145. _isEnded = false;
  146. _isPaused = false;
  147. sendEvent(HtmlMediaEvent.PLAY);
  148. sendEvent(HtmlMediaEvent.PLAYING);
  149. break;
  150. case STATE_PAUSED:
  151. _isEnded = false;
  152. _isPaused = true;
  153. sendEvent(HtmlMediaEvent.PAUSE);
  154. break;
  155. case STATE_CUED:
  156. sendEvent(HtmlMediaEvent.CANPLAY);
  157. // resize?
  158. break;
  159. }
  160. }
  161. private function onVideoPlaybackQualityChange(event:Event):void {
  162. trace("Current video quality:", Object(event).data);
  163. //resizePlayer(Object(event).data);
  164. }
  165. private function timerHandler(e:TimerEvent):void {
  166. if (_playerIsLoaded) {
  167. _bytesLoaded = player.getVideoBytesLoaded();
  168. _bytesTotal = player.getVideoBytesTotal();
  169. _currentTime = player.getCurrentTime();
  170. if (!_isPaused)
  171. sendEvent(HtmlMediaEvent.TIMEUPDATE);
  172. if (_bytesLoaded < _bytesTotal)
  173. sendEvent(HtmlMediaEvent.PROGRESS);
  174. }
  175. }
  176. private function getYouTubeId(url:String):String {
  177. // http://www.youtube.com/watch?feature=player_embedded&v=yyWWXSwtPP0
  178. // http://www.youtube.com/v/VIDEO_ID?version=3
  179. // http://youtu.be/Djd6tPrxc08
  180. url = unescape(url);
  181. var youTubeId:String = "";
  182. if (url.indexOf("?") > 0) {
  183. // assuming: http://www.youtube.com/watch?feature=player_embedded&v=yyWWXSwtPP0
  184. youTubeId = getYouTubeIdFromParam(url);
  185. // if it's http://www.youtube.com/v/VIDEO_ID?version=3
  186. if (youTubeId == "") {
  187. youTubeId = getYouTubeIdFromUrl(url);
  188. }
  189. } else {
  190. youTubeId = getYouTubeIdFromUrl(url);
  191. }
  192. return youTubeId;
  193. }
  194. // http://www.youtube.com/watch?feature=player_embedded&v=yyWWXSwtPP0
  195. private function getYouTubeIdFromParam(url:String):String {
  196. var youTubeId:String = "";
  197. var parts:Array = url.split('?');
  198. var parameters:Array = parts[1].split('&');
  199. for (var i:Number=0; i<parameters.length; i++) {
  200. var paramParts:Array = parameters[i].split('=');
  201. if (paramParts[0] == "v") {
  202. youTubeId = paramParts[1];
  203. break;
  204. }
  205. }
  206. return youTubeId;
  207. }
  208. // http://www.youtube.com/v/VIDEO_ID?version=3
  209. // http://youtu.be/Djd6tPrxc08
  210. private function getYouTubeIdFromUrl(url:String):String {
  211. var youTubeId:String = "";
  212. // remove any querystring elements
  213. var parts:Array = url.split('?');
  214. url = parts[0];
  215. youTubeId = url.substring(url.lastIndexOf("/")+1);
  216. return youTubeId;
  217. }
  218. // interface members
  219. public function setSrc(url:String):void {
  220. trace("yt setSrc()" + url );
  221. _currentUrl = url;
  222. _youTubeId = getYouTubeId(url);
  223. if (!_playerIsLoaded && !_isChromeless) {
  224. _playerLoader.load(new URLRequest("//www.youtube.com/v/" + _youTubeId + "?version=3&controls=0&rel=0&showinfo=0&iv_load_policy=1"));
  225. }
  226. }
  227. public function load():void {
  228. // do nothing
  229. trace("yt load()");
  230. if (_playerIsLoaded) {
  231. player.loadVideoById(_youTubeId);
  232. _timer.start();
  233. } else {
  234. /*
  235. if (!_isChromless && _youTubeId != "") {
  236. _playerLoader.load(new URLRequest("http://www.youtube.com/v/" + _youTubeId + "?version=3&controls=0&rel=0&showinfo=0&iv_load_policy=1"));
  237. }
  238. */
  239. }
  240. }
  241. public function play():void {
  242. if (_playerIsLoaded) {
  243. player.playVideo();
  244. }
  245. }
  246. public function pause():void {
  247. if (_playerIsLoaded) {
  248. player.pauseVideo();
  249. }
  250. }
  251. public function stop():void {
  252. if (_playerIsLoaded) {
  253. player.pauseVideo();
  254. }
  255. }
  256. public function setCurrentTime(pos:Number):void {
  257. //_player.seekTo(pos, false);
  258. player.seekTo(pos, true); // works in all places now
  259. }
  260. public function seekLimit():Number {
  261. return NaN;
  262. }
  263. public function setVolume(volume:Number):void {
  264. player.setVolume(volume*100);
  265. _volume = volume;
  266. }
  267. public function getVolume():Number {
  268. return player.getVolume()*100;
  269. }
  270. public function setMuted(muted:Boolean):void {
  271. if (muted) {
  272. player.mute();
  273. } else {
  274. player.unMute();
  275. }
  276. _isMuted = _player.isMuted();
  277. sendEvent(HtmlMediaEvent.VOLUMECHANGE);
  278. }
  279. private function sendEvent(eventName:String):void {
  280. // calculate this to mimic HTML5
  281. _bufferedTime = _bytesLoaded / _bytesTotal * _duration;
  282. // build JSON
  283. var values:String =
  284. "duration:" + _duration +
  285. ",framerate:" + _framerate +
  286. ",currentTime:" + _currentTime +
  287. ",muted:" + _isMuted +
  288. ",paused:" + _isPaused +
  289. ",ended:" + _isEnded +
  290. ",volume:" + _volume +
  291. ",src:\"" + _currentUrl + "\"" +
  292. ",bytesTotal:" + _bytesTotal +
  293. ",bufferedBytes:" + _bytesLoaded +
  294. ",bufferedTime:" + _bufferedTime +
  295. ",videoWidth:" + _videoWidth +
  296. ",videoHeight:" + _videoHeight +
  297. "";
  298. _element.sendEvent(eventName, values);
  299. }
  300. }
  301. }