package com.panache.creativeplayer.view { import com.panache.creativeplayer.*; import com.panache.creativeplayer.view.TimerComponent; import flash.display.*; import flash.events.*; import flash.geom.Rectangle; public class VideoControls extends MovieClip { private var _controls:MovieClip; private var _player:Object; private var _panache:Object; private var bar:ControlBar; private var playPauseButton:PlayPause; private var fullScreenButton:FullScreen; private var progressBar:ProgressBar; private var volumeScrubber:VolumeScrubber; private var muteButton:MuteButton; private var muted:Boolean = false; private var initVolume:Number = 1; private var _volumeScrubbing:Boolean = false; private var _scrubbing:Boolean = false; private var timer:TimerComponent; private var _adTimer:Boolean = false; private var _stage:*; public function VideoControls(player:Object) { _player = player; trace("PANACHE PLAYER: " + _player); //trace("VIDEO CONTROLS COMPONENT INI"); _controls = new MovieClip(); _controls.buttonMode = true; addChild(_controls); bar = new ControlBar(); _controls.addChild(bar); playPauseButton = new PlayPause(); _controls.addChild(playPauseButton); //fullScreenButton = new FullScreen(); //_controls.addChild(fullScreenButton); muteButton = new MuteButton(); _controls.addChild(muteButton); volumeScrubber = new VolumeScrubber(); //_controls.addChild(volumeScrubber); progressBar = new ProgressBar(); _controls.addChild(progressBar); timer = new TimerComponent(); timer.setTimerStyle(CatalogPlayerConstants.TIMER_STYLE_CLOCK); _controls.addChild(timer); _controls.visible = false; this.addEventListener(Event.ENTER_FRAME, handleProgress); } public function setAdTimer(value:Boolean):void { _adTimer = value; } public function setAdInstance(panache:*):void { _panache = panache; } public function setControlStates():void { var videoPlayState = _player.getPlayState(); //var linearAdPlayState = _player.getLinearAdPlayState(); var linearAdPlayState = _player.linearAdPlayState; if(videoPlayState == CatalogPlayerConstants.PLAYSTATE_PLAYING || linearAdPlayState == CatalogPlayerConstants.LINEAR_AD_STATE_PLAYING) { playPauseButton.gotoAndStop(3); } else { playPauseButton.gotoAndStop(1); } } public function setTimerDuration(time:int):void { timer.setDuration(time); } public function updateTime(time:int):void { timer.updateTime(time); } public function positionControlBar(width:Number=640, height:Number=30, topOffset:Number=0, leftOffset:Number=0):void { _controls.x = leftOffset; _controls.y = topOffset; bar.width = width; playPauseButton.addEventListener(MouseEvent.MOUSE_OVER, handleMouseOver); playPauseButton.addEventListener(MouseEvent.MOUSE_OUT, handleMouseOut); playPauseButton.addEventListener(MouseEvent.CLICK, handleMouseClick); playPauseButton.y = 3; muteButton.x = playPauseButton.x + playPauseButton.width; muteButton.y = 3; muteButton.addEventListener(MouseEvent.MOUSE_OVER, handleMouseOver); muteButton.addEventListener(MouseEvent.MOUSE_OUT, handleMouseOut); muteButton.addEventListener(MouseEvent.CLICK, handleMouseClick); /*fullScreenButton.x = bar.width - fullScreenButton.width; fullScreenButton.addEventListener(MouseEvent.MOUSE_OVER, handleMouseOver); fullScreenButton.addEventListener(MouseEvent.MOUSE_OUT, handleMouseOut); fullScreenButton.addEventListener(MouseEvent.CLICK, handleMouseClick); */ volumeScrubber.x = bar.width - 60;//(fullScreenButton.width + volumeScrubber.track.width + CatalogPlayerConstants.CONTROL_BAR_BUTTON_PADDING); volumeScrubber.y = bar.height / 2;//progressBar.ProgressBackgroundBar.height/2 - volumeScrubber.height/2; timer.x = muteButton.x + muteButton.width + 5;//bar.width - (fullScreenButton.width + volumeScrubber.track.width + timer.getWidth() + (Number(CatalogPlayerConstants.CONTROL_BAR_BUTTON_PADDING) * 2)); timer.y = 3;//progressBar.ProgressBackgroundBar.height/2 - timer.height/2; timer.show(); progressBar.x = 0; progressBar.ProgressBackgroundBar.width = bar.width;//timer.x - (playPauseButton.width + (Number(CatalogPlayerConstants.CONTROL_BAR_BUTTON_PADDING) * 2)); progressBar.ScrubberBar.addEventListener(MouseEvent.MOUSE_DOWN, handleStartScrub); progressBar.ScrubberBar.addEventListener(MouseEvent.MOUSE_UP, handleStopScrub); //volumeScrubber.thumb.x = _player.getVolume() * volumeScrubber.track.width; volumeScrubber.thumb.x = _player.volume * volumeScrubber.track.width; volumeScrubber.thumb.addEventListener(MouseEvent.MOUSE_DOWN, handleVolumeStartScrub); volumeScrubber.thumb.addEventListener(MouseEvent.MOUSE_UP, handleStopVolumeScrub); volumeScrubber.thumb.y = volumeScrubber.track.height /2; _controls.visible = true; } public function cleanUp():void { playPauseButton.removeEventListener(MouseEvent.MOUSE_OVER, handleMouseOver); playPauseButton.removeEventListener(MouseEvent.MOUSE_OUT, handleMouseOut); playPauseButton.removeEventListener(MouseEvent.CLICK, handleMouseClick); progressBar.ScrubberBar.removeEventListener(MouseEvent.MOUSE_DOWN, handleStartScrub); progressBar.ScrubberBar.removeEventListener(MouseEvent.MOUSE_UP, handleStopScrub); volumeScrubber.thumb.removeEventListener(MouseEvent.MOUSE_DOWN, handleVolumeStartScrub); volumeScrubber.thumb.removeEventListener(MouseEvent.MOUSE_UP, handleStopVolumeScrub); fullScreenButton.removeEventListener(MouseEvent.MOUSE_OVER, handleMouseOver); fullScreenButton.removeEventListener(MouseEvent.MOUSE_OUT, handleMouseOut); fullScreenButton.removeEventListener(MouseEvent.CLICK, handleMouseClick); } private function handleMouseOver(e:MouseEvent=null):void { var videoPlayState = _player.getPlayState(); //var linearAdPlayState = _player.getLinearAdPlayState(); var linearAdPlayState = _player.linearAdPlayState; switch(String(e.target)) { case "[object PlayPause]": if(videoPlayState == CatalogPlayerConstants.PLAYSTATE_PLAYING || linearAdPlayState == CatalogPlayerConstants.LINEAR_AD_STATE_PLAYING) { playPauseButton.gotoAndStop(4); } else { playPauseButton.gotoAndStop(2); } break; case "[object FullScreen]": fullScreenButton.gotoAndStop(2); break; case "[object MuteButton]": if(muted) { muteButton.gotoAndStop(4); } else { muteButton.gotoAndStop(2); } break; } } private function handleMouseOut(e:MouseEvent=null):void { var videoPlayState = _player.getPlayState(); //var linearAdPlayState = _player.getLinearAdPlayState(); var linearAdPlayState = _player.linearAdPlayState; switch(String(e.target)) { case "[object PlayPause]": if(videoPlayState == CatalogPlayerConstants.PLAYSTATE_PLAYING || linearAdPlayState == CatalogPlayerConstants.LINEAR_AD_STATE_PLAYING) { playPauseButton.gotoAndStop(3); } else { playPauseButton.gotoAndStop(1); } break; case "[object FullScreen]": fullScreenButton.gotoAndStop(1); break; case "[object MuteButton]": if(muted) { muteButton.gotoAndStop(3); } else { muteButton.gotoAndStop(1); } break; } } private function handleMouseClick(e:MouseEvent=null):void { switch(String(e.target)) { case "[object PlayPause]": //var linearAdPlayState:String = _player.getLinearAdPlayState(); var linearAdPlayState:String = _player.linearAdPlayState; var videoPlayState:String = _player.getPlayState(); trace("Linear Ad Play State " + linearAdPlayState); //Content is paused if(videoPlayState == CatalogPlayerConstants.PLAYSTATE_PAUSED) { //check state of ad content...if playing pause trace("Video Player is paused...switching on linear ad play state"); switch(linearAdPlayState) { case CatalogPlayerConstants.LINEAR_AD_STATE_PLAYING: //linear ad playing pause linear ad _panache.notifyPanache("panache_request_pause_ad", " "); //_player.setLinearAdPlayState(CatalogPlayerConstants.LINEAR_AD_STATE_PAUSED); _player.linearAdPlayState = CatalogPlayerConstants.LINEAR_AD_STATE_PAUSED; playPauseButton.gotoAndStop(3); break; case CatalogPlayerConstants.LINEAR_AD_STATE_PAUSED: //linear ad paused, resume linear ad _panache.notifyPanache("panache_request_resume_ad", " "); //_player.setLinearAdPlayState(CatalogPlayerConstants.LINEAR_AD_STATE_PLAYING); _player.linearAdPlayState = CatalogPlayerConstants.LINEAR_AD_STATE_PLAYING; playPauseButton.gotoAndStop(1); break; case CatalogPlayerConstants.LINEAR_AD_STATE_NA: //No linear ad resume content //_player.resume(); _player.play(); //_player.setPlayState(CatalogPlayerConstants.PLAYSTATE_PLAYING); playPauseButton.gotoAndStop(3); break; } } else { _player.pause(); //_player.setPlayState(CatalogPlayerConstants.PLAYSTATE_PAUSED); playPauseButton.gotoAndStop(1); } break; case "[object FullScreen]": try { if (stage.displayState == StageDisplayState.NORMAL) { stage.displayState = StageDisplayState.FULL_SCREEN; } else { stage.displayState = StageDisplayState.NORMAL; } } catch(error:Error) { MediaServicesAdDebug.reportErrorTrace("Cannot access to the stage", error, DEBUG_SOURCE, "handleMouseClick"); } break; case "[object MuteButton]": if(muted) { //_player.setVolume(initVolume); _player.volume = initVolume; muted = false; } else { //_player.setVolume(0); _player.volume = 0; muted = true; } break; } } public function resize(width, height):void { var playerWidth = width; var playerHeight = height -30; trace("CATALOG PLAYER CONTROLS RESIZE: Width ->" + playerWidth + " Height -> " + playerHeight); _player.position(0,0); _player.scale(playerWidth, playerHeight); _panache.notifyPanache("PlayerResizePanache", {width: playerWidth, height: playerHeight}) positionControlBar(playerWidth, playerHeight, playerHeight, 0); } private function handleProgress(e:Event):void { var progressObject = new Object(); progressObject = _player.getVideoProgress(); if(!_adTimer) { timer.updateTime(progressObject.videoTime); timer.setDuration(progressObject.videoDuration); } progressBar.ContentLoadedBar.width = (progressObject.videoBytesLoaded/progressObject.videoBytesTotal) * progressBar.ProgressBackgroundBar.width; if(!_scrubbing) { progressBar.PlayheadProgressBar.width = (progressObject.videoTime/progressObject.videoDuration) * progressBar.ProgressBackgroundBar.width; progressBar.ScrubberBar.x = (progressObject.videoTime/progressObject.videoDuration) * progressBar.ProgressBackgroundBar.width; } else { progressBar.PlayheadProgressBar.width = progressBar.ScrubberBar.x; try { } catch(e:Error) { } } } private function handleStartScrub(e:MouseEvent):void { _scrubbing = true; trace("Scrub Rect Width: " + progressBar.ProgressBackgroundBar.width); var scrubRec:Rectangle = new Rectangle(0, 0, progressBar.ProgressBackgroundBar.width, 0); progressBar.ScrubberBar.startDrag(true, scrubRec); } private function handleStopScrub(e:MouseEvent):void { var progressObject = new Object(); progressObject = _player.getVideoProgress(); if(progressBar.ContentLoadedBar.width >= progressBar.ScrubberBar.x) { _player.seek(progressObject.videoDuration * (progressBar.ScrubberBar.x/progressBar.ProgressBackgroundBar.width)); } else { _player.seek(progressObject.videoDuration * (progressBar.ContentLoadedBar.width/progressBar.ProgressBackgroundBar.width)); } progressBar.ScrubberBar.stopDrag(); _scrubbing = false; } private function handleVolumeStartScrub(e:MouseEvent):void { _volumeScrubbing = true; var scrubRec:Rectangle = new Rectangle(0, volumeScrubber.track.height /2, volumeScrubber.track.width, 0); volumeScrubber.thumb.startDrag(true, scrubRec); _player.addEventListener(MouseEvent.MOUSE_OVER, handleStopVolumeScrub); try { stage.addEventListener(MouseEvent.MOUSE_OUT, handleStopVolumeScrub); } catch(error:Error) { MediaServicesAdDebug.reportErrorTrace("Cannot access to the stage", error, DEBUG_SOURCE, "handleVolumeStartScrub"); } volumeScrubber.track.addEventListener(MouseEvent.MOUSE_OUT, handleStopVolumeScrub); } private function handleStopVolumeScrub(e:MouseEvent):void { var volume:Number = (volumeScrubber.thumb.x /volumeScrubber.track.width); volumeScrubber.thumb.stopDrag(); trace(volume); //_player.setVolume(volume); _player.volume = volume; _panache.notifyPanache("PlayerVolume", volume); _volumeScrubbing = false; _player.removeEventListener(MouseEvent.MOUSE_OVER, handleStopVolumeScrub); try { stage.removeEventListener(MouseEvent.MOUSE_OUT, handleStopVolumeScrub); } catch(error:Error) { MediaServicesAdDebug.reportErrorTrace("Cannot access to the stage", error, DEBUG_SOURCE, "handleStopVolumeScrub"); } volumeScrubber.track.removeEventListener(MouseEvent.MOUSE_OUT, handleStopVolumeScrub); } } }