0
0

Player.as 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /*
  2. ╠═ f4.Player ══════════════════════════════════════════════════════════════
  3. Software: f4.Player - flash video player
  4. Version: alpha 2.1
  5. Support: http://f4player.org
  6. Author: goker cebeci
  7. Contact: goker [at] cebeci .name
  8. -------------------------------------------------------------------------
  9. License: Distributed under the Lesser General Public License (LGPL)
  10. http://www.gnu.org/copyleft/lesser.html
  11. This program is distributed in the hope that it will be useful - WITHOUT
  12. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. FITNESS FOR A PARTICULAR PURPOSE.
  14. ═══════════════════════════════════════════════════════════════════════════ */
  15. package f4 {
  16. import flash.display.StageScaleMode;
  17. import flash.display.StageAlign;
  18. import flash.display.StageDisplayState;
  19. import flash.display.MovieClip;
  20. import flash.display.Loader;
  21. import flash.display.SimpleButton;
  22. import flash.display.Bitmap;
  23. import flash.net.URLLoader;
  24. import flash.net.URLRequest;
  25. import flash.net.NetConnection;
  26. import flash.net.NetStream;
  27. import flash.media.Video;
  28. import flash.media.SoundTransform
  29. import flash.events.Event;
  30. import flash.events.MouseEvent;
  31. import flash.events.TimerEvent;
  32. import flash.events.NetStatusEvent;
  33. import flash.utils.Timer;
  34. import flash.geom.Rectangle;
  35. public class Player extends MovieClip {
  36. public var player:MovieClip;
  37. public var videourl:String;
  38. public var thumbnailurl:String;
  39. public var autoplay:Boolean;
  40. public var fullScreen:Boolean;
  41. public var X:int;
  42. public var Y:int;
  43. public var W:int;
  44. public var H:int;
  45. private static var padding:int = 13; // Navigation Bar padding
  46. private var barPadding:int;
  47. private var seekBarWidth:int; // seek-bar width
  48. private var barWidth:int;
  49. //-------------------------------------- ### ------------------------------------------
  50. private var ns:NetStream;
  51. private var netStatusCache:String;
  52. private var video:Video;
  53. private var thumbnail:Bitmap;
  54. private var meta:Object;
  55. private var progress:int;
  56. private var ready:Boolean = false;
  57. private var bufferFlush:Boolean = false;
  58. private var invalidTime:Boolean = false;
  59. private var seeking:Boolean = false;
  60. private var progressBarTimer:Timer = new Timer(250);
  61. private var playingBarTimer:Timer = new Timer(250);
  62. public function Player(videourl:String,thumbnailurl:String,W:int,H:int,skin:String,autoplay:Boolean=false,fullScreen:Boolean=true,Y:int=0,X:int=0) {
  63. this.videourl = videourl;
  64. this.thumbnailurl = thumbnailurl;
  65. this.autoplay = autoplay;
  66. this.fullScreen = fullScreen;
  67. this.X = X;
  68. this.Y = Y;
  69. this.W = W?(W<150?150:W):480;
  70. this.H = H?(H<150?150:H):270;
  71. skin = skin ? skin : 'skins/default.swf'
  72. var skinLoader:Loader = new Loader();
  73. skinLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, skinCompleteEvent);
  74. //skinLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, skinProgressEvent);
  75. skinLoader.load(new URLRequest(skin));
  76. }
  77. // EVENT ╠═════════════ # SKIN COMPLETE EVENT # ═════════════
  78. private function skinCompleteEvent(event:Event):void {
  79. player = event.currentTarget.content;
  80. this.addChild(player);
  81. //set skin [
  82. player.overBtn.visible = false;
  83. player.playBtn.visible = false;
  84. player.navigator.visible = false;
  85. var nav:MovieClip = player.navigator;
  86. seekBarWidth = nav.seekBar.width;
  87. nav.playBtn.x = padding;
  88. nav.pauseBtn.visible = false;
  89. nav.pauseBtn.y = nav.playBtn.y;
  90. nav.bar.x = nav.playBtn.width + padding * 2;
  91. nav.container.x = nav.bar.x + padding;
  92. barPadding = (nav.container.height - nav.playingBar.height)*.5;
  93. nav.progressBar.x = nav.playingBar.x = nav.seekBar.x = nav.container.x + barPadding;
  94. nav.progressBar.y = nav.playingBar.y = nav.seekBar.y = nav.container.y + barPadding;
  95. nav.seeker.x = nav.container.x+barPadding;
  96. nav.seeker.y = nav.container.y-barPadding;
  97. nav.playingBar.width = 0;
  98. setPosition();
  99. // ] set skin
  100. //-------------------------------------- ### ------------------------------------------
  101. //thumbnailurl [
  102. if(thumbnailurl){
  103. var tn = new Loader();
  104. var tnEvent:Function = function(event:Event):void {
  105. thumbnail = tn.content;
  106. var dim = 1;
  107. var vert:Boolean = (W/H) > 1;
  108. if(thumbnail.width > thumbnail.height && vert){
  109. dim = H / thumbnail.height;
  110. } else {
  111. dim = W / thumbnail.width;
  112. }
  113. thumbnail.width = thumbnail.width * dim;
  114. thumbnail.height = thumbnail.height * dim;
  115. thumbnail.x = (W - thumbnail.width) / 2 >> 0;
  116. thumbnail.y = (H - thumbnail.height) / 2 >> 0;
  117. player.screen.addChildAt(thumbnail,0);
  118. // screen size fix [
  119. //player.screen.width = image.width;
  120. //player.screen.height = image.height;
  121. // ]
  122. //image.x = player.screen.x;
  123. //image.y = player.screen.y;
  124. }
  125. tn.contentLoaderInfo.addEventListener(Event.COMPLETE, tnEvent);
  126. tn.load(new URLRequest(thumbnailurl));
  127. }
  128. // ] thumbnailurl
  129. //-------------------------------------- ### ------------------------------------------
  130. var client:Object = new Object();
  131. client.onMetaData = metaDataObject;
  132. var c:NetConnection = new NetConnection();
  133. c.connect(null);
  134. ns = new NetStream(c);
  135. ns.bufferTime = 5; // buffer time 5 sec.
  136. ns.client = client;
  137. ns.addEventListener(NetStatusEvent.NET_STATUS, netStatusEvent);
  138. //-------------------------------------- ### ------------------------------------------
  139. // PLAYER
  140. video = new Video();
  141. video.smoothing = true;
  142. //video.x = player.screen.x;
  143. //video.y = player.screen.y;
  144. //video.width = player.screen.width;
  145. //video.height = player.screen.height;
  146. video.attachNetStream(ns);
  147. player.addChildAt(video,1);
  148. player.playBtn.addEventListener(MouseEvent.CLICK, playEvent);
  149. // AUTOPLAY
  150. if(this.autoplay) {
  151. var clicker:Function = playEvent;
  152. clicker(new MouseEvent(MouseEvent.CLICK));
  153. } else player.playBtn.visible = true;
  154. //-------------------------------------- ### ------------------------------------------
  155. }
  156. // FUNC ╠═════════════ # SET POSITION # ═════════════
  157. private function setPosition():void{
  158. player.background.x = X;
  159. player.background.y = Y;
  160. player.background.width = W;
  161. player.background.height = H;
  162. var Width:int = W;
  163. var Height:int = H;
  164. var dim = 1;
  165. var vert:Boolean = (W/H) > 1;
  166. /*// VIDEO RATIO
  167. if(Width > int(Height * 1.3333)){ // 1.3333 => 4/3 || 1.7777 => 16/9 format
  168. player.screen.width = int(Height * 1.7777);
  169. player.screen.height = Height;
  170. } else {
  171. player.screen.width = Width;
  172. player.screen.height = int(Width * .75); // .75 => 4/3 || .5625 => 16/9 format
  173. }
  174. */
  175. //player.screen.width = Width;
  176. //player.screen.height = Height;
  177. player.overBtn.width = Width;
  178. player.overBtn.height = Height;
  179. var nav:MovieClip = player.navigator;
  180. player.overBtn.x = player.screen.x = nav.x = X;
  181. player.overBtn.y = player.screen.y = Y;
  182. player.bufferMovie.x = player.screen.x + (Width-player.bufferMovie.width)*.5;
  183. player.bufferMovie.y = player.screen.y + (Height-player.bufferMovie.height)*.5;
  184. player.playBtn.x = player.screen.x + (Width-player.playBtn.width)*.5;
  185. player.playBtn.y = player.screen.y + (Height-player.playBtn.height)*.5;
  186. //NAVIGATOR POSITION
  187. nav.y = player.screen.y + Height - nav.height - padding *.5;
  188. nav.bar.width = W - nav.bar.x - padding;
  189. var endPoint:int = nav.bar.x + nav.bar.width;
  190. if(fullScreen)
  191. endPoint = nav.fullScreen.x = endPoint - nav.fullScreen.width - padding;
  192. else nav.fullScreen.visible = false;
  193. endPoint = nav.volumeBar.x = endPoint - nav.volumeBar.width - padding;
  194. nav.container.width = endPoint - nav.container.x - padding;
  195. nav.progressBar.width = nav.playingBar.width = nav.seekBar.width = barWidth = nav.container.width - barPadding * 2;
  196. if(progress){ // progress bar size
  197. var newWidth:Number =((progress * barWidth *.01) >> 0);
  198. nav.progressBar.x = nav.seekBar.x + newWidth;
  199. nav.progressBar.width = barWidth - newWidth;
  200. }
  201. // VIDEO
  202. if(video){
  203. dim = 1;
  204. if(video.width > video.height && vert){
  205. dim = H / video.height;
  206. } else {
  207. dim = W / video.width;
  208. }
  209. video.width = video.width * dim;
  210. video.height = video.height * dim;
  211. video.x = (W - video.width) / 2 >> 0;
  212. video.y = (H - video.height) / 2 >> 0;
  213. }
  214. if(thumbnail){
  215. dim = 1;
  216. if(thumbnail.width > thumbnail.height && vert){
  217. dim = H / thumbnail.height;
  218. } else {
  219. dim = W / thumbnail.width;
  220. }
  221. thumbnail.width = thumbnail.width * dim;
  222. thumbnail.height = thumbnail.height * dim;
  223. thumbnail.x = (W - thumbnail.width) / 2 >> 0;
  224. thumbnail.y = (H - thumbnail.height) / 2 >> 0;
  225. }
  226. }
  227. // EVENT ╠═════════════ # RESIZE EVENT # ═════════════
  228. public function resizeEvent(event:Event):void {
  229. W = stage.stageWidth;
  230. H = stage.stageHeight;
  231. setPosition();
  232. }
  233. // EVENT ╠═════════════ # NET STATUS EVENT # ═════════════
  234. private function netStatusEvent(event:NetStatusEvent):void {
  235. if(netStatusCache != event.info.code){
  236. switch (event.info.code) {
  237. case "NetStream.Play.Start" :
  238. playingBarTimer.start();
  239. break;
  240. case "NetStream.Buffer.Empty" :
  241. break;
  242. case "NetStream.Buffer.Full" :
  243. break;
  244. case "NetStream.Buffer.Flush" :
  245. bufferFlush = true;
  246. break;
  247. case "NetStream.Seek.Notify" :
  248. invalidTime = false;
  249. break;
  250. case "NetStream.Seek.InvalidTime" :
  251. bufferFlush = false;
  252. invalidTime = true;
  253. break;
  254. case "NetStream.Play.Stop" :
  255. if(bufferFlush) STOP();
  256. break;
  257. }
  258. netStatusCache = event.info.code;
  259. }
  260. }
  261. // OBJECT ╠═════════════ # META DATA OBJECT # ═════════════
  262. private function metaDataObject(data:Object):void {
  263. meta = data;
  264. player.bufferMovie.visible = false;
  265. //video.scaleMode = VideoScaleMode.MAINTAIN_ASPECT_RATIO;
  266. playingBarTimer.addEventListener(TimerEvent.TIMER, playingBarTimerEvent);
  267. var nav:MovieClip = player.navigator;
  268. nav.playBtn.addEventListener(MouseEvent.CLICK, playPauseEvent);
  269. nav.pauseBtn.addEventListener(MouseEvent.CLICK, playPauseEvent);
  270. player.overBtn.visible = true;
  271. player.overBtn.buttonMode = true;
  272. player.overBtn.addEventListener(MouseEvent.CLICK, playPauseEvent);
  273. player.overBtn.addEventListener(MouseEvent.MOUSE_OVER, controlDisplayEvent);
  274. player.overBtn.addEventListener(MouseEvent.MOUSE_OUT, controlDisplayEvent);
  275. nav.addEventListener(MouseEvent.MOUSE_OVER, controlDisplayEvent);
  276. nav.addEventListener(MouseEvent.MOUSE_OUT, controlDisplayEvent);
  277. /// seeker [
  278. nav.seeker.buttonMode = true;
  279. nav.seeker.addEventListener(MouseEvent.MOUSE_DOWN, seekerEvent);
  280. this.stage.addEventListener(MouseEvent.MOUSE_UP, stageMouseUpEvent);
  281. nav.playingBar.buttonMode = true;
  282. nav.playingBar.addEventListener(MouseEvent.MOUSE_DOWN, playingBarEvent);
  283. nav.seekBar.buttonMode = true;
  284. nav.seekBar.addEventListener(MouseEvent.MOUSE_DOWN, seekBarEvent);
  285. // ] seeker
  286. // volume [
  287. volumeTransform = new SoundTransform();
  288. nav.volumeBar.mute.addEventListener(MouseEvent.CLICK, volumeEvent);
  289. nav.volumeBar.mute.addEventListener(MouseEvent.ROLL_OVER, volumeEvent);
  290. nav.volumeBar.volumeOne.addEventListener(MouseEvent.CLICK, volumeEvent);
  291. nav.volumeBar.volumeOne.addEventListener(MouseEvent.ROLL_OVER, volumeEvent);
  292. nav.volumeBar.volumeTwo.addEventListener(MouseEvent.CLICK, volumeEvent);
  293. nav.volumeBar.volumeTwo.addEventListener(MouseEvent.ROLL_OVER, volumeEvent);
  294. nav.volumeBar.volumeThree.addEventListener(MouseEvent.CLICK, volumeEvent);
  295. nav.volumeBar.volumeThree.addEventListener(MouseEvent.ROLL_OVER, volumeEvent);
  296. nav.volumeBar.volumeFour.addEventListener(MouseEvent.CLICK, volumeEvent);
  297. nav.volumeBar.volumeFour.addEventListener(MouseEvent.ROLL_OVER, volumeEvent);
  298. nav.volumeBar.volumeFive.addEventListener(MouseEvent.CLICK, volumeEvent);
  299. nav.volumeBar.volumeFive.addEventListener(MouseEvent.ROLL_OVER, volumeEvent);
  300. // ] volume
  301. // fullScreen [
  302. nav.fullScreen.buttonMode = true;
  303. nav.fullScreen.addEventListener(MouseEvent.CLICK, fullScreenHandler);
  304. // ] fullScreen
  305. }
  306. // EVENT ╠═════════════ # PROGRESSBAR TIMER EVENT # ═════════════
  307. private function progressBarTimerEvent(event:TimerEvent):void {
  308. progress = (( ns.bytesLoaded / ns.bytesTotal * 100 ) >> 0);
  309. var w:Number =((progress * barWidth *.01) >> 0);
  310. player.navigator.progressBar.x = player.navigator.seekBar.x + w;
  311. player.navigator.progressBar.width = barWidth - w;
  312. if(progress >= 100){
  313. progressBarTimer.stop();
  314. }
  315. }
  316. // EVENT ╠═════════════ # PLAYINGBAR TIMER EVENT # ═════════════
  317. private function playingBarTimerEvent(event:TimerEvent):void {
  318. player.navigator.seeker.currentTime.text = formatTime(ns.time);
  319. var percent:Number = (ns.time / meta.duration * barWidth).toFixed(2);
  320. player.navigator.playingBar.width = percent;
  321. player.navigator.seeker.x = player.navigator.playingBar.x + percent;
  322. }
  323. // FUNC ╠═════════════ # FORMAT TIME # ═════════════
  324. private function formatTime(time:Number):String {
  325. if(time > 0){
  326. var integer:String = String((time*.0166)>>0);
  327. var decimal:String = String((time%60)>>0);
  328. return ((integer.length<2)?"0"+integer:integer)+":"+((decimal.length<2)?"0"+decimal:decimal);
  329. } else return String("00:00");
  330. }
  331. // EVENT ╠═════════════ # PLAY EVENT # ═════════════
  332. private function playEvent(event:MouseEvent):void {
  333. ready = true;
  334. var dim = 1;
  335. var vert:Boolean = (W/H) > 1;
  336. if(video.width > video.height && vert){
  337. dim = H / video.height;
  338. } else {
  339. dim = W / video.width;
  340. }
  341. video.width = video.width * dim;
  342. video.height = video.height * dim;
  343. video.x = (W - video.width) / 2 >> 0;
  344. video.y = (H - video.height) / 2 >> 0;
  345. player.navigator.playBtn.visible = (player.navigator.playBtn.visible?false:true);
  346. player.navigator.pauseBtn.visible = (player.navigator.pauseBtn.visible?false:true);
  347. player.playBtn.visible = false;
  348. player.screen.visible = false;
  349. ns.play(videourl); // *** play video file
  350. // PROGRESS
  351. progressBarTimer.addEventListener(TimerEvent.TIMER, progressBarTimerEvent);
  352. progressBarTimer.start();
  353. }
  354. // EVENT ╠═════════════ # PLAY&PAUSE EVENT # ═════════════
  355. private function playPauseEvent(event:MouseEvent):void {
  356. TOGGLEPAUSE();
  357. }
  358. // EVENT ╠═════════════ # CONTROL DISPLAY EVENT # ═════════════
  359. private function controlDisplayEvent(event:MouseEvent):void {
  360. if(event.type == 'mouseOver'){
  361. player.navigator.visible = true;
  362. } else player.navigator.visible = false;
  363. }
  364. // EVENT ╠═════════════ # SEEKER EVENT # ═════════════
  365. private function seekerEvent(event:MouseEvent):void {
  366. seeking = true;
  367. var rectangle:Rectangle = new Rectangle(player.navigator.playingBar.x, player.navigator.seeker.y, barWidth, 0);
  368. player.navigator.seeker.startDrag(false, rectangle);
  369. TOGGLEPAUSE();
  370. this.stage.addEventListener(MouseEvent.MOUSE_MOVE, stageMouseMoveEvent);
  371. }
  372. // EVENT ╠═════════════ # STAGE MOUSE MOVE EVENT # ═════════════
  373. private function stageMouseMoveEvent(event:MouseEvent):void { // for seeker position
  374. if(meta.duration > 0) {
  375. if(seeking) {
  376. var point:int = (((player.navigator.seeker.x - player.navigator.playingBar.x) / barWidth) * meta.duration) >> 0;
  377. if(point <= 0 || point >= (meta.duration >> 0)) player.navigator.seeker.stopDrag();
  378. player.navigator.seeker.currentTime.text = formatTime(point);
  379. ns.seek(point);
  380. }
  381. }
  382. }
  383. // EVENT ╠═════════════ # STAGE MOUSE UP EVENT # ═════════════
  384. private function stageMouseUpEvent(event:MouseEvent):void { // for stop seeking
  385. if(seeking){
  386. seeking = false;
  387. player.navigator.seeker.stopDrag();
  388. TOGGLEPAUSE();
  389. }
  390. }
  391. // EVENT ╠═════════════ # PLAYINGBAR EVENT # ═════════════
  392. private function playingBarEvent(event:MouseEvent):void { //click to seek
  393. var point:Number = (meta.duration * (event.localX*player.navigator.playingBar.width/seekBarWidth) / barWidth);
  394. ns.seek(point);
  395. }
  396. // EVENT ╠═════════════ # SEEKBAR EVENT # ═════════════
  397. private function seekBarEvent(event:MouseEvent):void { //click to seek
  398. var point:Number = (event.localX * meta.duration / seekBarWidth);
  399. ns.seek(point);
  400. }
  401. // EVENT ╠═════════════ # VOLUME EVENT # ═════════════
  402. private function volumeEvent(event:MouseEvent):void {
  403. var thisMC:MovieClip = event.currentTarget as MovieClip;
  404. if(event.buttonDown || event.type == 'click')
  405. switch (event.currentTarget) {
  406. case player.navigator.volumeBar.mute :
  407. if(volumeTransform.volume > 0) {
  408. volumeCache = .1;
  409. setVolume(0);
  410. } else setVolume(volumeCache);
  411. break;
  412. case player.navigator.volumeBar.volumeOne : setVolume(.2); break;
  413. case player.navigator.volumeBar.volumeTwo : setVolume(.4); break;
  414. case player.navigator.volumeBar.volumeThree : setVolume(.6); break;
  415. case player.navigator.volumeBar.volumeFour : setVolume(.8); break;
  416. case player.navigator.volumeBar.volumeFive : setVolume(1); break;
  417. }
  418. }
  419. // FUNC ╠═════════════ # SET VOLUME # ═════════════
  420. public function setVolume(newVolume):void{
  421. volumeTransform.volume = newVolume;
  422. ns.soundTransform = volumeTransform;
  423. player.navigator.volumeBar.mute.gotoAndStop((newVolume > 0)?1:2);
  424. player.navigator.volumeBar.volumeOne.gotoAndStop((newVolume >= 0.2)?1:2);
  425. player.navigator.volumeBar.volumeTwo.gotoAndStop((newVolume >= 0.4)?1:2);
  426. player.navigator.volumeBar.volumeThree.gotoAndStop((newVolume >= 0.6)?1:2);
  427. player.navigator.volumeBar.volumeFour.gotoAndStop((newVolume >= 0.8)?1:2);
  428. player.navigator.volumeBar.volumeFive.gotoAndStop((newVolume == 1)?1:2);
  429. }
  430. // EVENT ╠═════════════ # FULLSCREEN EVENT # ═════════════
  431. private function fullScreenHandler(event:MouseEvent):void {
  432. if(this.stage.displayState == StageDisplayState.FULL_SCREEN){
  433. this.stage.displayState = StageDisplayState.NORMAL;
  434. } else {
  435. this.stage.displayState = StageDisplayState.FULL_SCREEN;
  436. }
  437. }
  438. // FUNC ╠═════════════ # TOGGLE PAUSE # ═════════════
  439. public function TOGGLEPAUSE():void {
  440. if(invalidTime){ ns.seek(0); invalidTime = false; }
  441. player.navigator.playBtn.visible = (player.navigator.playBtn.visible?false:true);
  442. player.navigator.pauseBtn.visible = (player.navigator.pauseBtn.visible?false:true);
  443. player.playBtn.visible = false;
  444. player.screen.visible = false;
  445. ns.togglePause();
  446. }
  447. // FUNC ╠═════════════ # STOP # ═════════════
  448. public function STOP():void {
  449. player.navigator.playBtn.visible = true;
  450. player.navigator.pauseBtn.visible = false;
  451. player.screen.visible = true;
  452. player.playBtn.visible = true;
  453. ns.seek(0);
  454. ns.pause();
  455. }
  456. }
  457. }