Return to Snippet

Revision: 24950
at March 16, 2010 14:12 by Bonky


Initial Code
package com.acme.view
{
    import flash.events.Event;
	
    import org.puremvc.as3.interfaces.*;
    import org.puremvc.as3.patterns.mediator.Mediator;

    import com.acme.ApplicationFacade;
    
	import ca.turbulent.media.Pyro;
	import ca.turbulent.media.events.*
	
	import flash.events.TimerEvent;
	import flash.utils.Timer;
    /**
     * A Mediator for interacting with the Pyro Video Player.
     */
    public class PyroMediator extends Mediator implements IMediator
    {
       
        public static const NAME:String = 'PyroMediator';
		private var loadProgressTimer:Timer;
		private var playbackProgressTimer:Timer;
		private var loadProgress:Number;
		private var playbackProgress:Number;
	   /**
         * Constructor. 
         */
        public function PyroMediator( viewComponent:Object ) 
        {
            // pass the viewComponent to the superclass where 
            // it will be stored in the inherited viewComponent property
            
            super(NAME, viewComponent);
    		loadProgressTimer=new Timer(500, 999);
			loadProgressTimer.addEventListener(TimerEvent.TIMER, checkLoadProgress);
			loadProgressTimer.addEventListener(TimerEvent.TIMER_COMPLETE, function(){loadProgressTimer.reset();loadProgressTimer.start();});
			
    		playbackProgressTimer=new Timer(500, 999);
			playbackProgressTimer.addEventListener(TimerEvent.TIMER, checkPlaybackProgress);
			playbackProgressTimer.addEventListener(TimerEvent.TIMER_COMPLETE, function(){playbackProgressTimer.reset();playbackProgressTimer.start();});
			
			
			myPyro.addEventListener( CuePointEvent.CUE_POINT_RECEIVED, onCuePointEvent );
			myPyro.addEventListener( ErrorEvent.CONNECTION_ERROR, onErrorEvent );
			myPyro.addEventListener( ErrorEvent.ERROR, onErrorEvent );
			myPyro.addEventListener( ErrorEvent.FILE_NOT_FOUND_ERROR, onErrorEvent );
			myPyro.addEventListener( ErrorEvent.SECURITY_ERROR, onErrorEvent );
			myPyro.addEventListener( ImageDataEvent.IMAGE_DATA_RECEIVED, onImageDateEvent );
			myPyro.addEventListener( PyroEvent.BANDWIDTH_CHECKED, onPyroEvent );
			myPyro.addEventListener( PyroEvent.BUFFER_EMPTY, onPyroEvent );
			myPyro.addEventListener( PyroEvent.BUFFER_FLUSH, onPyroEvent );
			myPyro.addEventListener( PyroEvent.BUFFER_FULL, onPyroEvent );
			myPyro.addEventListener( PyroEvent.BUFFER_TIME_ADJUSTED, onPyroEvent );
			myPyro.addEventListener( PyroEvent.CLOSE_CAPTIONS_UPDATE, onPyroEvent );
			myPyro.addEventListener( PyroEvent.COMPLETED, onPyroEvent );
			myPyro.addEventListener( PyroEvent.DISCONNECTED, onPyroEvent );
			myPyro.addEventListener( PyroEvent.INSUFFICIENT_BANDWIDTH, onPyroEvent );
			myPyro.addEventListener( PyroEvent.METADATA_RECEIVED, onPyroEvent );
			myPyro.addEventListener( PyroEvent.MUTED, onPyroEvent );
			myPyro.addEventListener( PyroEvent.NEW_STREAM_INIT, onPyroEvent );
			myPyro.addEventListener( PyroEvent.PAUSED, onPyroEvent );
			myPyro.addEventListener( PyroEvent.SEEKED, onPyroEvent );
			myPyro.addEventListener( PyroEvent.SIZE_UPDATE, onPyroEvent );
			myPyro.addEventListener( PyroEvent.STARTED, onPyroEvent );
			myPyro.addEventListener( PyroEvent.STOPPED, onPyroEvent );
			myPyro.addEventListener( PyroEvent.UNMUTED, onPyroEvent );
			myPyro.addEventListener( PyroEvent.UNPAUSED, onPyroEvent );
			myPyro.addEventListener( PyroEvent.URL_PARSED, onPyroEvent );
			myPyro.addEventListener( PyroEvent.VOLUME_UPDATE, onPyroEvent );
			myPyro.addEventListener( PyroEvent.XMP_DATA_RECEIVED, onPyroEvent );
			myPyro.addEventListener( StatusUpdateEvent.STATUS_UPDATE, onStatusUpdateEvent );
			myPyro.addEventListener( TextDataEvent.TEXT_DATA_RECEIVED, onTextDataEvent );
			
        }



		private function checkLoadProgress(e:Event):void{
			//loadProgress = myPyro.bytesLoaded / myPyro.bytesTotal;
			loadProgress = myPyro.loadRatio;;
			//playbackProgress = myPyro.progressRatio;
			//trace("loadProgress:"+loadProgress);
			sendNotification( ApplicationFacade.VIDEO_LOAD_PROGRESS,loadProgress );//goes to ControlsComponentMediator
			
			if(loadProgress==1){
				loadProgressTimer.reset();
			}
		}


		private function checkPlaybackProgress(e:Event):void{
			playbackProgress = myPyro.progressRatio;
			//trace("checkPlaybackProgress:"+playbackProgress);
			sendNotification( ApplicationFacade.VIDEO_PLAYBACK_PROGRESS,playbackProgress );//goes to ControlsComponentMediator
		}

        
		 private function onCuePointEvent(e:CuePointEvent):void{
			//trace("CuePointEvent:"+(CuePointEvent(e).cuePoint)); 
		 }
		 private function onErrorEvent(e:ErrorEvent):void{
			//trace("ErrorEvent:"+(ErrorEvent(e).errorMessage)); 
		 }
		 private function onImageDateEvent(e:ImageDataEvent):void{
			//trace("ImageDataEvent:"+(ImageDataEvent(e).image)); 
		 }
		 private function onPyroEvent(e:PyroEvent):void{
			//trace("PyroEvent:"+(PyroEvent(e).eventType)); 
			switch (PyroEvent(e).eventType){
				case "streamSeekedEvent":
				break;
				case "newStreamInitEvent":
					sendNotification( ApplicationFacade.VIDEO_STREAM_INIT );//indicates buffering has started, goes to controls component to show looper
				break;
			}
		 }
		 private function onStatusUpdateEvent(e:StatusUpdateEvent):void{
			trace("StatusUpdateEvent:"+(StatusUpdateEvent(e).status)); 
			switch (StatusUpdateEvent(e).status){
				case Pyro.STATUS_PLAYING:
					sendNotification( ApplicationFacade.VIDEO_IS_PLAYING );//goes to ControlsComponentMediator
					playbackProgressTimer.start();
					break;
				case Pyro.STATUS_READY:
					sendNotification( ApplicationFacade.VIDEO_PLAYER_IS_READY );
					break;
				case Pyro.STATUS_PENDING:
					loadProgressTimer.start();
					break;
				case Pyro.STATUS_CONNECTING:
					break;
				case Pyro.STATUS_PAUSED:
					playbackProgressTimer.stop();
					sendNotification( ApplicationFacade.VIDEO_IS_PAUSED );//goes to ControlsComponentMediator
					break;
			}
			
		 }
		 private function onTextDataEvent(e:TextDataEvent):void{
			//trace("TextDataEvent:"+(TextDataEvent(e).text)); 
		 }
		 /**
         * List all notifications this Mediator is interested in.
         * <P>
         * Automatically called by the framework when the mediator
         * is registered with the view.</P>
         * 
         * @return Array the list of Nofitication names
         */
        override public function listNotificationInterests():Array 
        {
            return [ 
    	      		ApplicationFacade.PLAY_PAUSE,
    	      		ApplicationFacade.VIDEO_PAUSE,
    	      		ApplicationFacade.VIDEO_PLAY,
					ApplicationFacade.SEEK,
					ApplicationFacade.START_VIDEO_PLAYBACK,
					ApplicationFacade.VIDEO_SET_VOLUME
                   ];
        }

        /**
         * Handle all notifications this Mediator is interested in.
         * <P>
         * Called by the framework when a notification is sent that
         * this mediator expressed an interest in when registered
         * (see <code>listNotificationInterests</code>.</P>
         * 
         * @param INotification a notification 
         */
        override public function handleNotification( note:INotification ):void 
        {
            switch ( note.getName() ) {
                
                case ApplicationFacade.PLAY_PAUSE://comes from ControlsComponentMediator
					//trace("pyro playPause");
					myPyro.togglePause();
                    break;
                
				case ApplicationFacade.VIDEO_PAUSE://comes from ControlsComponentMediator
					trace("pyro pause");
					myPyro.pause();
                    break;
				
				case ApplicationFacade.VIDEO_PLAY://comes from ControlsComponentMediator
					trace("pyro play");
					myPyro.play();
                    break;

				case ApplicationFacade.SEEK: //comes from ControlsComponentMediator
					var t:uint = note.getBody() as uint;
					//trace("pyro seek:"+t);
					myPyro.seek(t);
                    break;

				case ApplicationFacade.START_VIDEO_PLAYBACK: //comes from ApplicationMediator
					var path:String = note.getBody() as String;
					myPyro.play(path);
                    break;
					
				case ApplicationFacade.VIDEO_SET_VOLUME:
					myPyro.volume=note.getBody() as Number;
					break;
            }
        }

		
        protected function get myPyro():Pyro
        {
            return viewComponent as Pyro;
        }

    }
}

Initial URL


Initial Description
This is a PureMVC view mediator designed to interact with the Pyro video player from Turbulent Media. Must be used within the PureMVC Framework.

Initial Title
Pyro Mediator for PureMVC

Initial Tags


Initial Language
ActionScript 3