package { import com.greensock.events.LoaderEvent; import com.greensock.loading.core.LoaderItem; import com.greensock.loading.LoaderMax; import com.greensock.loading.SWFLoader; import com.greensock.loading.VideoLoader; import com.greensock.TweenLite; import flash.display.MovieClip; import flash.display.Shape; import flash.events.Event; import flash.events.KeyboardEvent; import flash.media.Video ; import flash.net.NetStream; import flash.ui.Keyboard; /** * @author David Amey */ public class VideoPlayer extends MovieClip { /** @private **/ private var _queue:LoaderMax; /** @private **/ private var _currentVideo:VideoLoader; /** @private **/ private var _videos:Array; /** * CONSTRUCTOR */ public function VideoPlayer() { trace("VideoPlayer.VideoPlayer"); this._queue = new LoaderMax( { name:"videoQueue", maxConnections:2} ); this.addEventListener(Event.ADDED_TO_STAGE, createElements); } protected function createElements(e:Event):void { trace("VideoPlayer.createElements"); this.removeEventListener(Event.ADDED_TO_STAGE, createElements); this.graphics.beginFill(0x000000); this.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight); this.graphics.endFill(); stage.addEventListener(KeyboardEvent.KEY_DOWN, this.onKeyDown, false, 0, true); this.createLoadQueue(); } protected function createLoadQueue():void { trace("VideoPlayer.createLoadQueue"); this._queue.append(new VideoLoader("videos/vid1.f4v", { name:"item2", container:this, autoPlay:false, width:this.width, height:this.height})); this._queue.append(new VideoLoader("videos/vid2.f4v", { name:"item3", container:this, autoPlay:false, width:this.width, height:this.height})); this._queue.append(new VideoLoader("videos/vid3.f4v", { name:"item6", container:this, autoPlay:false, width:this.width, height:this.height})); //this._videos = this._queue.getChildren(); this._queue.load(); //this.nextVideo(); this.showVideo(this._queue.getLoader("item2")); } protected function showVideo(video:VideoLoader):void { trace("VideoPlayer.showVideo"); //if the new video is the one that's currently showing, do nothing. if (video == _currentVideo) { return; } //The first time through, the _currentVideo will be null. That's when we need to activate the user interface if (_currentVideo != null) { //remove the event listeners from the _currentVideo (which is now the old one that will be replaced) //_currentVideo.removeEventListener(VideoLoader.VIDEO_COMPLETE, nextVideo); this.removeChild(this._currentVideo.content); } //now swap the _currentLoader variable so it refers to the new video. _currentVideo = video; //listen for a VIDEO_COMPLETE event so that we can automatically advance to the next video. //_currentVideo.addEventListener(VideoLoader.VIDEO_COMPLETE, nextVideo, false, 0, true); //if the video hasn't fully loaded yet and is still buffering, show the preloader if (_currentVideo.progress < 1 && _currentVideo.bufferProgress < 1) { //prioritizing the video ensures that it moves to the top of the LoaderMax gueue and any other loaders that were loading are canceled to maximize bandwidth available for the new video. _currentVideo.prioritize(true); } //start playing the video from its beginning _currentVideo.gotoVideoTime(0, true); //always start with the volume at 0, and fade it up to 1 if necessary. //_currentVideo.volume = 0; //when we addChild() the VideoLoader's content, it makes it rise to the top of the stacking order this.addChild(_currentVideo.content); //fade the VideoLoader's content alpha in. Remember, the "content" refers to the ContentDisplay Sprite that we see on the stage. //TweenLite.to(_currentVideo.content, 0.8, {alpha:1}); } /********************** * LISTENER FUNCTIONS * **********************/ private function onKeyDown(e:KeyboardEvent):void { switch (e.keyCode) { case (Keyboard.LEFT): this.previousVideo(); break; case (Keyboard.RIGHT): this.nextVideo(); break; case (Keyboard.END): this.destroyQueue(); break; } } protected function nextVideo(e:Event = null):void { /*var next:int = _videos.indexOf(_currentVideo) + 1; if (next >= _videos.length) { next = 0; } showVideo(_videos[next]);*/ } protected function previousVideo(e:Event = null):void { /*var prev:int = _videos.indexOf(_currentVideo) - 1; if (prev < 0) { prev = _videos.length - 1; } showVideo(_videos[prev]);*/ } protected function errorHandler(e:LoaderEvent):void { trace("VideoPlayer.onFail"); } /********************* * UTILITY FUNCTIONS * *********************/ private function rewindAndPause(video:VideoLoader):void { video.pauseVideo(); //rewind the video so that when we fade it in again later, it's already displaying the first frame and there's no delay skipping to it. video.gotoVideoTime(0); } /** * removes all loaders and content */ private function destroyQueue():void { trace("[VideoPlayer.destroy]"); this._queue.pause(); this._queue.empty(true, true); //this._queue.dispose(true); /*for each(var video:VideoLoader in this._videos) { if (video != null && video.content != null) { video.removeEventListener(VideoLoader.VIDEO_COMPLETE, nextVideo) if (video.content.parent != null) { video.content.parent.removeChild(video.content) ; } video.dispose(true); //this._queue.remove(video); //video = null; }else { video = null; } }*/ //this._queue. //this._queue. while (this.numChildren) { this.removeChildAt(0); } this._queue = null; this._videos = null; this._currentVideo = null; this.removeEventListener(KeyboardEvent.KEY_DOWN, this.onKeyDown); this.parent.removeChild(this); } } }