Video in PaperVision 3D - PART 3

Play & Pause +
Load new video on the fly


This example continues on the previous examples by adding a play and pause button. This example also adds two buttons that load a new video in to the VideoStreamMaterial texture.

Download Soruce Files

Content on this page requires a newer version of Adobe Flash Player.

Get Adobe Flash player

package  {
	
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.media.Video;
    import flash.net.NetConnection;
    import flash.net.NetStream;
    import flash.text.TextField;
    
    import org.papervision3d.materials.VideoStreamMaterial;
    import org.papervision3d.objects.primitives.Plane;
  
    public class VideoPlusControl extends PaperBase {
    	
        protected var sceneWidth:Number;      
        protected var sceneHeight:Number;
        
        private var videoPlane:Plane;
        private var netConnection:NetConnection;
        private var videoStreamMaterial:VideoStreamMaterial;
		private var video:Video;
		private var netStream:NetStream;
		      
        public function VideoPlusControl(){   	
            sceneWidth = stage.stageWidth
            sceneHeight = stage.stageHeight;
            init(sceneWidth,sceneHeight);
        }
        
        /* Created buttons used to control video. 
       	   This 2D layer is on top of the 3D layer - see paper base class */
        override protected function init2d() : void {
        	
     		var video1:Sprite = makeButton('Play Video One');
     		video1.addEventListener(MouseEvent.CLICK, playVideo1);
     		addChild(video1);
     		
     		var video2:Sprite = makeButton('Play Video Two');
     		video2.addEventListener(MouseEvent.CLICK, playVideo2);
     		video2.y = 30;
     		addChild(video2);
     		
     		var play:Sprite = makeButton('PLAY');
     		play.addEventListener(MouseEvent.CLICK, playVideo);
     		play.y = 60;
     		addChild(play);
     		
     		var pause:Sprite = new Sprite();
     		pause.addChild(makeButton('PAUSE'));
     		pause.addEventListener(MouseEvent.CLICK, pauseVideo);
     		pause.y = 90;
     		addChild(pause); 		
     		
     		// Create button graphics
     		function makeButton(title:String) : Sprite {
			
				var label:TextField = new TextField();
				label.textColor = 0xFFFFFF;
				label.text = title;
				label.selectable = false;
				
				var bg:Sprite = new Sprite();
				bg.graphics.beginFill(0x000000);
				bg.graphics.drawRect(0,0,label.width,20);
				bg.alpha = .8;	
				bg.addChild(label);	
				
				bg.addEventListener(MouseEvent.MOUSE_OVER, onState );
				bg.addEventListener(MouseEvent.MOUSE_OUT, offState );
				
				function onState(e:Event) : void {		
					label.textColor = 0x000000;
					bg.graphics.clear();
					bg.graphics.beginFill(0xFFFFFF);
					bg.graphics.drawRect(0,0,label.width,20);
				}
				
				function offState(e:Event) : void {		
					label.textColor = 0xFFFFFF;
					bg.graphics.clear();
					bg.graphics.beginFill(0x000000);
					bg.graphics.drawRect(0,0,label.width,20);
				}
				return bg;
			}
        }
        	
        override protected function init3d(): void {
        	
        	videoMaterial();  // Create video material texture
        	
        	// Match the aspect ratio of the video w480 & h360
        	videoPlane =  new Plane( videoStreamMaterial, 480, 360, 10, 10 ); 
	   		videoPlane.scale =  1;
	 	  	default_scene.addChild(videoPlane);  	 
        }
        
        public function videoMaterial() : void {
			
			var customClient:Object = new Object();
			customClient.onMetaData = metaDataHandler;
			  
			netConnection = new NetConnection();
			netConnection.connect(null); 
			 
			netStream = new NetStream(netConnection);
			netStream.client = customClient;
			netStream.play( "assets/video.f4v" ); 
			
			// Match the pixel size of the video w480 & h360
			video = new Video(480,360); 
			video.smoothing = true;  
			video.attachNetStream(netStream);
			 
			videoStreamMaterial = new VideoStreamMaterial(video, netStream, true, true);
			videoStreamMaterial.doubleSided = true;			
		} 
		
		// Required to catch meta data from 
        private function metaDataHandler(infoObject:Object) : void {
    		trace("metaData");
		}
		
		private function pauseVideo(e:Event) : void {
			// Pauses video from a playing state
			netStream.pause();		
		}
		
		private function playVideo(e:Event) : void {
			// Resumes video from a pause() state
			netStream.resume();	
		}
		
		private function playVideo1(e:Event) : void {
			// Load up new video and starts from the beginning 
			netStream.play( 'assets/video_plus_alpha.flv' );		
		}
		
		private function playVideo2(e:Event) : void {
			// Load up new video and starts from the beginning 
			netStream.play( 'assets/video.f4v' );		
		}
        
        override protected function processFrame() : void {	
            // Relates the video planes X & Y location to the MouseX & Mouse Y
            videoPlane.z = -((mouseY / sceneHeight) * 800) 
            videoPlane.rotationY = -((mouseX / sceneWidth) * 360) 
       }
	}
}
2009-04-23