@@ -1,787 +1,9 @@ . ====== Open Source Flash Projects ====== ~~DISCUSSION:off~~ - /* - ************************************* - * Multi Video Player with Play List - * http://www.FlepStudio.org import mx.events.EventDispatcher; - import mx.utils.Delegate; - import YouTubePlayerEvent; - - class YouTubePlayer - { - public var autoPlay:Boolean = true; - public var chromeless:Boolean = false; - public var pars:String = 'autoplay=0&loop=0&rel=0&showsearch=0&hd=1'; - public var playerWidth:Number = 425; - public var playerHeight:Number = 344; - - public var addEventListener:Function; - - private var intervals:Array = [ ]; - - private var parent:MovieClip; - private var loader:MovieClipLoader; - private var player:MovieClip; - private var videoId:String; - private var removePlayer:Boolean; - private var dispatchEvent:Function; - - public function YouTubePlayer(parent:MovieClip) - { - EventDispatcher.initialize( this ); - - System.security.allowDomain( '*' ); - System.security.allowDomain( 'www.youtube.com' ); - System.security.allowDomain( 'youtube.com' ); - System.security.allowDomain( 's.ytimg.com' ); - System.security.allowDomain( 'i.ytimg.com' ); - - this.parent = parent; - - loader = new MovieClipLoader(); - } - - public function init(videoId:String):Void - { - this.videoId = videoId; - - if ( player ) - { - removePlayer = true; - - if ( player.getPlayerState() != 1 ) - { - destroyPlayer(); - } - else - { - player.stopVideo(); - } - } - else - { - loadVideo(); - } - } - - private function loadVideo():Void - { - var url:String = ( chromeless ? 'http://www.youtube.com/apiplayer' : 'http://www.youtube.com/v/' + videoId + '&' + pars ); - - player = parent.createEmptyMovieClip( 'Player' + ( new Date().getTime() ), parent.getNextHighestDepth() ); - - parent._visible = false; - - loader.addListener({ onLoadInit: loadInit() }); - - loader.unloadClip( player ); - - loader.loadClip( url, player ); - } - - private function loadInit():Void - { - intervals.push( setInterval( Delegate.create( this, checkPlayerLoaded ) , 500 ) ); - } - - private function checkPlayerLoaded():Void - { - if ( player.isPlayerLoaded() ) - { - if ( chromeless ) - { - player.loadVideoById( videoId ); - } - - player.addEventListener( YouTubePlayerEvent.STATE_CHANGED, Delegate.create( this, onPlayerStateChanged ) ); - player.addEventListener( YouTubePlayerEvent.ERROR, Delegate.create( this, onPlayerError ) ); - - resizePlayer( playerWidth, playerHeight ); - - parent._visible = true; - - for ( var i:Number = 0; i < intervals.length; i++ ) - { - clearInterval( intervals[ i ] ); - } - - onPlayerReady(); - } - } - - private function onPlayerStateChanged(state:Number):Void - { - switch ( state ) - { - case 0: - // ended - - if ( removePlayer ) - { - destroyPlayer(); - } - - dispatchEvent( new YouTubePlayerEvent( YouTubePlayerEvent.PLAYER_ENDED ) ); - - break; - - case 1: - // playing - - dispatchEvent( new YouTubePlayerEvent( YouTubePlayerEvent.PLAYER_PLAYING ) ); - - break; - - case 2: - // paused - - dispatchEvent( new YouTubePlayerEvent( YouTubePlayerEvent.PLAYER_PAUSED ) ); - - break; - - case 3: - // buffering - - dispatchEvent( new YouTubePlayerEvent( YouTubePlayerEvent.PLAYER_BUFFERING ) ); - - break; - - case 5: - // video queued - - dispatchEvent( new YouTubePlayerEvent( YouTubePlayerEvent.PLAYER_QUEUED ) ); - - break; - } - } - - private function onPlayerError():Void - { - dispatchEvent( new YouTubePlayerEvent( YouTubePlayerEvent.ERROR ) ); - } - - public function resizePlayer(width:Number, height:Number):Void - { - if ( player.isPlayerLoaded() ) - { - player.setSize( width, height ); - } - } - - private function onPlayerReady():Void - { - if ( autoPlay ) - { - player.playVideo(); - } - } - - private function destroyPlayer():Void - { - intervals.push( setInterval( Delegate.create( this, checkPlayerDestroyed ), 500 ) ); - - removePlayer = false; - } - - private function checkPlayerDestroyed():Void - { - if ( !player ) - { - for ( var i:Number = 0; i < intervals.length; i++ ) - { - clearInterval( intervals[ i ] ); - } - - init( videoId ); - } - else - { - for ( var players:String in parent ) - { - parent[ players ].destroy(); - - removeMovieClip( parent[ players ] ); - unloadMovie( parent[ players ] ); - } - - player = null; - } - } - - public function stop():Void - { - removePlayer = false; - - player.stopVideo(); - } - } - * © Author: Filippo Lughi - * version 1.0 - ************************************* - */ - package org.FlepStudio.VideoPlayer - { - import flash.display.*; - import flash.net.*; - import flash.media.*; - import flash.events.*; - import flash.utils.*; - import flash.geom.*; - import caurina.transitions.Tweener; - - public class Player extends MovieClip - { - private var fla:MovieClip; - - public var myVideo:Video; - private var nc:NetConnection; - private var ns:NetStream; - - private var apple:Apple; - - private var client:Object=new Object; - - private var check_click:String; - - private var timer_video:Timer; - - private var rect:Rectangle; - - private var duration:Number; - private var video_duration:Number; - private var video_position:Number; - private var offset_scrub:Number; - private var firstMouseX:Number; - - private var boo_play:Boolean=true; - private var boo_pause:Boolean=false; - - // CONSTRUCTOR - public function Player():void - { - addEventListener(Event.ADDED_TO_STAGE,init); - } - - // This method is called when VideoList class add this class as a child and inits the class logics - private function init(evt:Event):void - { - removeEventListener(Event.ADDED_TO_STAGE,init); - - fla=parent as MovieClip; - - // Disable the buttons untill buffering has stopped - disablePlayAndPauseButtons(); - fla.consolle_mc.play_mc.gotoAndStop(2); - fla.consolle_mc.pause_mc.gotoAndStop(1); - fla.bar_mc.scrub_mc.mouseEnabled=true; - - // Add buffering preloader - addApple(); - // Create the connection - createConnectionAndStream(); - // Enable the play button and the pause button - initPlayButtonListener(); - initPauseButtonListener(); - // Add the listeners that check the volume of video - initVolumeListeners(); - // Enable the button that returns the user at the video list - initListButtonListener(); - // Enable the listener of the scrub of the volume scroller - addCheckScrubListener(); - // Add a listener that checks the position of the video - addPositionListener(); - // Add the listener that checks how many bytes ahs been loaded by streaming - addLoaderListener(); - // Enable the scrub of the timeline - initScrub(); - } - - // Create a connection using NetConnection,NetStream and Video classes. Also add a listener that checks the status events of the stream (NET_STATUS) - private function createConnectionAndStream():void - { - myVideo=new Video(); - myVideo.smoothing=true; - nc=new NetConnection(); - nc.connect(null); - ns=new NetStream(nc); - ns.bufferTime=Main.buffering_time; - client.onMetaData=onMetaData; - ns.client=client; - myVideo.attachNetStream(ns); - ns.addEventListener(NetStatusEvent.NET_STATUS,onNetStatus); - myVideo.width=410; - myVideo.height=260; - myVideo.x=-5; - myVideo.y=-4; - myVideo.alpha=0; - fla.display_mc.addChild(myVideo); - } - - // Enable the play button by adding the listener - private function initPlayButtonListener():void - { - setChildrenAndMode(fla.consolle_mc.play_mc); - fla.consolle_mc.play_mc.addEventListener(MouseEvent.MOUSE_DOWN,switchPlayButton); - } - - // Enable the stop button by adding the listener - private function initPauseButtonListener():void - { - setChildrenAndMode(fla.consolle_mc.pause_mc); - fla.consolle_mc.pause_mc.addEventListener(MouseEvent.MOUSE_DOWN,switchPauseButton); - } - - // Switch the status of the play button everytime the user clicks it - private function switchPlayButton(evt:MouseEvent):void - { - switch(boo_play) - { - case true: - evt.target.gotoAndStop(1); - timer_video.stop(); - fla.bar_mc.scrub_mc.x=9; - fla.bar_mc.position_mc.width=0; - ns.pause(); - ns.seek(0); - break; - - case false: - evt.target.gotoAndStop(2); - timer_video.start(), - ns.resume(); - break; - } - - boo_play=!boo_play; - } - - // Switch the status of the pause button everytime the user clicks it - private function switchPauseButton(evt:MouseEvent):void - { - switch(boo_pause) - { - case true: - evt.target.gotoAndStop(1); - ns.resume(); - break; - - case false: - evt.target.gotoAndStop(2); - ns.pause(); - break; - } - - boo_pause=!boo_pause; - } - - // Enable the arrow button to go back at video list - private function initListButtonListener():void - { - setChildrenAndMode(fla.consolle_mc.arrow_mc); - fla.consolle_mc.arrow_mc.addEventListener(MouseEvent.MOUSE_DOWN,onListDown); - } - - // This method is called when the arrow button has been called. Also this method destroy the strem connection of the video - private function onListDown(evt:MouseEvent):void - { - destroy(); - } - - // Enable mouse events of the timeline scrub of the video - private function initScrub():void - { - offset_scrub=fla.bar_mc.scrub_mc.x; - fla.bar_mc.scrub_mc.mouseChildren=false; - fla.bar_mc.scrub_mc.buttonMode=true; - fla.bar_mc.scrub_mc.addEventListener(MouseEvent.MOUSE_DOWN,setScrumbDown); - fla.bar_mc.scrub_mc.addEventListener(MouseEvent.MOUSE_UP,setScrumbUp); - } - - // Enable mouse events of the timeline scrub of the video. This scrub will seek the video by dragging it - private function addPositionListener():void - { - fla.bar_mc.position_mc.addEventListener(MouseEvent.MOUSE_DOWN,setPositionDown); - fla.bar_mc.position_mc.addEventListener(MouseEvent.MOUSE_UP,setPositionUp); - } - - // Add a listener that checks if the mouse clicks the streaming bar and point the video to that position - private function addLoaderListener():void - { - fla.bar_mc.loader_mc.addEventListener(MouseEvent.MOUSE_DOWN,setPositionDown); - fla.bar_mc.loader_mc.addEventListener(MouseEvent.MOUSE_UP,setPositionUp); - } - - // This method is called when the scrub is dragged - private function setPositionDown(evt:MouseEvent):void - { - firstMouseX=mouseX; - - check_click="bar"; - - hideApple(); - video_position=ns.time; - timer_video.stop(); - ns.togglePause(); - } - - // This method is called when the scrub is dropped - private function setPositionUp(evt:MouseEvent):void - { - if(check_click=="bar") - { - var ratio:Number=(video_duration*(firstMouseX-fla.bar_mc.scrub_mc.width))/412; - ns.seek(ratio); - ns.togglePause(); - timer_video.start(); - } - - check_click=""; - } - - // Add listeners that manages the scrub of the volume - private function initVolumeListeners():void - { - setChildrenAndMode(fla.consolle_mc.scrub_mc); - fla.consolle_mc.scrub_mc.addEventListener(MouseEvent.MOUSE_DOWN,setVolumeDown); - fla.consolle_mc.scrub_mc.addEventListener(MouseEvent.MOUSE_UP,setVolumeUp); - } - - // This method is called when the volume scrub has been clicked - private function setVolumeDown(evt:MouseEvent):void - { - check_click="volume"; - var rectangle:Rectangle=new Rectangle(fla.consolle_mc.volume_bar_mc.x+fla.consolle_mc.volume_bar_mc.width,evt.target.y, - -fla.consolle_mc.volume_bar_mc.width,0); - evt.target.startDrag(false,rectangle); - evt.target.addEventListener(Event.ENTER_FRAME,volumeControl); - } - - // This method is called meantime the volume scrub is dragged - private function volumeControl(evt:Event):void - { - var distance:Number=fla.consolle_mc.volume_bar_mc.width-3; - var p:Number=evt.target.x-fla.consolle_mc.volume_bar_mc.x; - var percentage:Number=Math.ceil((p/distance)*100); - var sf:SoundTransform=new SoundTransform(); - sf.volume=-0.05+percentage/100; - if(sf.volume<0) - sf.volume=0; - ns.soundTransform=sf; - - if(sf.volume>.8) - { - fla.consolle_mc.sound_mc.clip_0_mc.visible=true; - fla.consolle_mc.sound_mc.clip_1_mc.visible=true; - fla.consolle_mc.sound_mc.clip_2_mc.visible=true; - } - if(sf.volume<.7&&sf.volume>.5) - { - fla.consolle_mc.sound_mc.clip_0_mc.visible=true; - fla.consolle_mc.sound_mc.clip_1_mc.visible=true; - fla.consolle_mc.sound_mc.clip_2_mc.visible=false; - } - if(sf.volume<.4&&sf.volume>.15) - { - fla.consolle_mc.sound_mc.clip_0_mc.visible=true; - fla.consolle_mc.sound_mc.clip_1_mc.visible=false; - fla.consolle_mc.sound_mc.clip_2_mc.visible=false; - } - if(sf.volume<.15) - { - fla.consolle_mc.sound_mc.clip_0_mc.visible=false; - fla.consolle_mc.sound_mc.clip_1_mc.visible=false; - fla.consolle_mc.sound_mc.clip_2_mc.visible=false; - } - } - - // This method is called when the scrub of the volume is dropped - private function setVolumeUp(evt:MouseEvent):void - { - check_click=""; - evt.target.stopDrag(); - } - - // This video calls the methods that will play the video - private function callPlayVideo(evt:MouseEvent):void - { - playVideo(); - } - - // This method starts playing the video - public function playVideo():void - { - fla.bar_mc.buffer_mc.scaleX=0; - fla.bar_mc.position_mc.width=0; - fla.bar_mc.loader_mc.scaleX=0; - addEventListener(Event.ENTER_FRAME,checkBuffer); - - timer_video=new Timer(5,0); - timer_video.addEventListener(TimerEvent.TIMER,checkVideoPosition); - timer_video.start(); - - ns.play(Main.currentSelectedVideo); - } - - // This method checks the status of the connection - private function onNetStatus(nsevent:NetStatusEvent):void - { - if(nsevent.info.level=="error") - ns.close(); - switch(nsevent.info.code) - { - case "NetStream.Play.Start": - showApple(); - break; - - case "NetStream.Buffer.Empty": - if(fla.bar_mc.position_mc.width<407) - { - disablePlayAndPauseButtons(); - showApple(); - } - break; - - case "NetStream.Buffer.Flush": - trace("flush"); - break; - - case "NetStream.Buffer.Full": - showConsolle(); - Tweener.addTween(myVideo,{alpha:.95,time:1,transition:"regular"}); - fla.bar_mc.scrub_mc.visible=true; - hideApple(); - removeEventListener(Event.ENTER_FRAME,checkBuffer); - enablePlayAndPauseButtons(); - addEventListener(Event.ENTER_FRAME,checkBytesLoaded); - fla.bar_mc.buffer_mc.scaleX=0; - break; - - case "NetStream.Play.Stop": - hideApple(); - break; - - //default: trace("not used code: "+nsevent.info.code); - } - } - - // This method is called when the buffering time is full - private function onMetaData(data:Object):void - { - video_duration=data.duration; - } - - // This method checks the buffering of the connection - private function checkBuffer(evt:Event):void - { - var ratio:Number=ns.bufferLength/ns.bufferTime; - fla.bar_mc.buffer_mc.scaleX=ratio; - if(fla.bar_mc.buffer_mc.scaleX>1) - fla.bar_mc.buffer_mc.scaleX=1; - } - - // This method checks the bytes loaded of the video - private function checkBytesLoaded(evt:Event):void - { - var ratio:Number=ns.bytesLoaded/ns.bytesTotal; - fla.bar_mc.loader_mc.scaleX=ratio; - if(fla.bar_mc.loader_mc.scaleX>=0.99) - { - fla.bar_mc.loader_mc.scaleX=1; - removeEventListener(Event.ENTER_FRAME,checkBytesLoaded); - } - } - - // This method checks the position of the video - private function checkVideoPosition(evt:TimerEvent):void - { - var ratio:Number=ns.time*412/video_duration; - video_position=ns.time; - fla.bar_mc.position_mc.width=ratio; - fla.bar_mc.scrub_mc.x=ratio; - if(fla.bar_mc.position_mc.width>=421) - { - fla.bar_mc.position_mc.width=412; - timer_video.stop(); - hideApple(); - } - } - - // This method is called when the scrub of the video has been clicked - private function setScrumbDown(evt:MouseEvent):void - { - check_click="scrub"; - disableBars(); - - hideApple(); - video_position=ns.time; - timer_video.stop(); - ns.togglePause(); - - rect=new Rectangle(fla.bar_mc.position_mc.x,evt.target.y,fla.bar_mc.loader_mc.width,0); - evt.target.startDrag(false,rect); - - ns.removeEventListener(NetStatusEvent.NET_STATUS,onNetStatus); - evt.target.addEventListener(Event.ENTER_FRAME,seekTheVideo); - } - - // This method seeks the video during the scrub dragging - private function seekTheVideo(evt:Event):void - { - var ratio:Number=(video_duration*(evt.target.x-offset_scrub))/412; - fla.bar_mc.position_mc.width=evt.target.x; - ns.seek(ratio); - } - - // This method is called when the scrub of the video has been dropped - private function setScrumbUp(evt:MouseEvent):void - { - check_click=""; - enableBars(); - - evt.target.removeEventListener(Event.ENTER_FRAME,seekTheVideo); - evt.target.stopDrag(); - ns.togglePause(); - timer_video.start(); - } - - // This method add a listener that checks if the mouse is released out of the area of the scrub video or scrub volume - private function addCheckScrubListener():void - { - fla.stage.addEventListener(MouseEvent.MOUSE_UP,checkScrub); - } - - // This method checks which scrub has been clicked - private function checkScrub(evt:MouseEvent):void - { - switch(check_click) - { - case "scrub": - fla.bar_mc.scrub_mc.removeEventListener(Event.ENTER_FRAME,seekTheVideo); - fla.bar_mc.scrub_mc.stopDrag(); - ns.addEventListener(NetStatusEvent.NET_STATUS,onNetStatus); - ns.togglePause(); - timer_video.start(); - break; - - case "bar": - var ratio:Number=(video_duration*(firstMouseX-fla.bar_mc.scrub_mc.width))/412; - ns.seek(ratio); - ns.togglePause(); - timer_video.start(); - break; - - case "volume": - fla.consolle_mc.scrub_mc.stopDrag(); - break; - } - - check_click=""; - enableBars(); - } - - // This method disable the clicks over the position and loader bars - private function disableBars():void - { - fla.bar_mc.position_mc.mouseEnabled=false; - fla.bar_mc.loader_mc.mouseEnabled=false; - } - - // This method enable the clicks over the position and loader bars - private function enableBars():void - { - fla.bar_mc.position_mc.mouseEnabled=true; - fla.bar_mc.loader_mc.mouseEnabled=true; - } - - // This method enable the play and pause buttons - private function enablePlayAndPauseButtons():void - { - fla.consolle_mc.play_mc.mouseEnabled=true; - fla.consolle_mc.pause_mc.mouseEnabled=true; - } - - // This method disable the play and pause buttons - private function disablePlayAndPauseButtons():void - { - fla.consolle_mc.play_mc.mouseEnabled=false; - fla.consolle_mc.pause_mc.mouseEnabled=false; - } - - // This method add the preloader - private function addApple():void - { - apple=new Apple(); - apple.alpha=0; - apple.width=apple.height=35; - apple.x=220-apple.width/2; - apple.y=145-apple.height/2; - addChild(apple); - } - - // This method shows the preloader during buffer time - private function showApple():void - { - apple.play(); - Tweener.addTween(apple,{alpha:1,time:.5,transition:"regular"}); - } - - // This method hide the preloader each time the buffer time is empty - private function hideApple():void - { - apple.stop(); - Tweener.addTween(apple,{alpha:0,time:.5,transition:"regular"}); - } - - // This method shows the consolle of the control buttons - private function showConsolle():void - { - fla.consolle_mc.visible=true; - } - - // This method hides the consolle of the control buttons - private function hideConsolle():void - { - fla.consolle_mc.visible=false; - } - - // This method set the mouseChildren and buttonMode of the MovieClip that is passed to this method - private function setChildrenAndMode(mov:MovieClip):void - { - mov.mouseChildren=false; - mov.buttonMode=true; - } - - // This method destroy all the listeners of this class. - private function destroy():void - { - hideConsolle(); - hideApple(); - fla.bar_mc.scrub_mc.x=8; - fla.bar_mc.scrub_mc.mouseEnabled=false; - Tweener.addTween(fla.bar_mc.buffer_mc,{scaleX:0,time:1,transition:"regular"}); - Tweener.addTween(fla.bar_mc.loader_mc,{scaleX:0,time:1,transition:"regular"}); - Tweener.addTween(fla.bar_mc.position_mc,{width:0,time:1,transition:"regular"}); - ns.close(); - ns.removeEventListener(NetStatusEvent.NET_STATUS,onNetStatus); - timer_video.stop(); - timer_video.removeEventListener(TimerEvent.TIMER,checkVideoPosition); - removeEventListener(Event.ENTER_FRAME,checkBuffer); - removeEventListener(Event.ENTER_FRAME,checkBytesLoaded); - Tweener.addTween(myVideo,{alpha:0,time:.5,transition:"regular",onComplete:removeMe}); - } - - // This method remove this class from VideoList class - private function removeMe():void - { - fla.video_list.showList(); - fla.removeChild(this); - } - } - } ===== Flash IDE Alternatives ===== * [[AjaxAnimator]] - An **online, collaborative, web-based animation suite** with support for exporting to **Flash, Silverlight, GIFs, and more** [[http://antimatter15.com|Homepage]] [[http://antimatter15.com/ajaxanimator|Live Application]] - "Last release - Wed Feb 17 2010" - Ray James