/ Published in: ActionScript 3
Requires two MovieClips: 1. a MovieClip instance named, "playPause" that has two frames with frame labels ("play", "pause") with the corresponding graphics for a play / pause button. 2. A MovieClip instance named, "videoBar" that has two instances inside - a long background named "bar" and a shorter slider bar named, "slider".
Expand |
Embed | Plain Text
package { import flash.display.MovieClip; import com.greensock.loading.*; import com.greensock.loading.display.*; import com.greensock.*; import com.greensock.events.LoaderEvent; import flash.events.MouseEvent; import flash.events.Event; import flash.geom.Rectangle; import flash.geom.Point; public class VideoPlayer extends MovieClip { public var videoBar:MovieClip = new MovieClip ;// the videoBar public var playPause:MovieClip = new MovieClip ; // check whether you are displaying the time or changing the time private var seeking:Boolean = false; private var video:VideoLoader; private var endOfVideo:Number; public function VideoPlayer ():void { init (); } public function init ():void { //create a VideoLoader video = new VideoLoader("song.flv",{name:"myVideo",container:this,width:400,height:300,scaleMode:"proportionalInside",bgColor:0x000000,autoPlay:false,volume:1,requireWithRoot:this.root}); //start loading video.load (); // set up the play/pause toggle playPause.buttonMode = true; playPause.addEventListener (MouseEvent.CLICK, togglePause); // set up the videoBar videoBar.slider.buttonMode = true; videoBar.slider.addEventListener (MouseEvent.MOUSE_DOWN, scrubToTime); // listen to the stopDrag method on the stage, not the videoBar! stage.addEventListener (MouseEvent.MOUSE_UP, stopScrubToTime); // check the video time and update the videoBar accordingly addEventListener (Event.ENTER_FRAME, updateVideoTime, false, 0, true); videoBar.addEventListener (MouseEvent.CLICK, onSeekToClick, false, 0, true); endOfVideo = videoBar.width - videoBar.slider.width; } private function updateVideoTime (e:Event):void { // moves the videoBar if (! seeking) { if (videoBar.slider.x <= endOfVideo) { videoBar.slider.x = (video.videoTime /video.duration)*videoBar.width; } else { video.videoTime = 0; videoBar.slider.x = (video.videoTime /video.duration)*videoBar.width; video.pauseVideo (); playPause.gotoAndStop ("play"); } } else { seekToPoint (); } } private function seekToPoint ():void { var c:Number = (videoBar.slider.x); var value:Number = Math.abs(c / videoBar.width); video.gotoVideoTime (value*video.duration, true); } private function scrubToTime (e:Event):void { // constrain to rectangle videoBar.slider.startDrag (false, new Rectangle(0,0,videoBar.width - videoBar.slider.width, 0)); seeking = true; } private function stopScrubToTime (e:Event):void { videoBar.slider.stopDrag (); seeking = false; } private function togglePause (event:MouseEvent):void { if (video.videoPaused) { video.playVideo (); playPause.gotoAndStop ("pause"); } else { video.pauseVideo (); playPause.gotoAndStop ("play"); } } private function onSeekToClick (e:MouseEvent):void { removeEventListener (Event.ENTER_FRAME, updateVideoTime); var point:Point = new Point(this.mouseX,this.mouseY); var stagePoint = videoBar.globalToLocal(point); if (stagePoint.x < videoBar.x + videoBar.width) { videoBar.slider.x = stagePoint.x; seekToPoint (); addEventListener (Event.ENTER_FRAME, updateVideoTime, false, 0, true); } } } }
You need to login to post a comment.
