⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 flex2fms.mxml

📁 flex 和fms通信,实现实时多人视频聊天实例
💻 MXML
字号:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="init()">
	<mx:HBox id="videoHBox">
	</mx:HBox>

	<mx:TextArea width="622" height="188" id="traceArea" wordWrap="true"/>
	
	<mx:Form width="622">
		<mx:FormItem label="Call Test Echo Message:" width="560">
			<mx:TextInput text="called!" id="echoMessageInput"  width="350"/>
			<mx:Button label="Call Echo Message" id="callButton" enabled="false" click="echoMessage();"/>
		</mx:FormItem>
		<mx:FormItem label="Stream Send Test Echo Message:" width="560">
			<mx:TextInput text="sent!" id="sendMessageInput"  width="350"/>
			<mx:Button label="Send Message" id="sendButton" enabled="false" click="sendMessage();"/>
		</mx:FormItem>
		<mx:FormItem label="So property value:" width="560">
			<mx:TextInput text="foo" id="soInput"  width="350"/>
			<mx:Button label="Set" id="soSetButton" enabled="false" click="setSO();"/>
		</mx:FormItem>
		<mx:FormItem   horizontalAlign="right" width="560">
			<mx:Button label="Connect" id="connectButton" click="connect();"  width="100"/>
		</mx:FormItem>
	</mx:Form>
	
	
<mx:Script>
	<![CDATA[
		import mx.controls.Spacer;
		import flash.net.*;
		import flash.events.*;
		import flash.utils.*;
		import flash.media.Camera;
		import mx.controls.*;
		import mx.core.UIComponent;

		// Current release of FMS only understands AMF0 so tell Flex to 
		// use AMF0 for all NetConnection, NetStream, and SharedObject objects.
		NetConnection.defaultObjectEncoding = flash.net.ObjectEncoding.AMF0;
		//NetStream.defaultObjectEncoding     = flash.net.ObjectEncoding.AMF0;
		SharedObject.defaultObjectEncoding  = flash.net.ObjectEncoding.AMF0;
		
		// NetConnection to be used throughout this script:
		private var nc:NetConnection;
		// echoResponder is used when nc.call("echo", echoResponder ...) is called.
		private var echoResponder:Responder = new Responder(echoResult, echoStatus);
		// Video objects
		private var returnedVideo:Video;
		private var localVideo:Video;
		// Stream Objects:
		private var inStream:NetStream = null;
		private var outStream:NetStream = null;
		// Camera:
		private var camera:Camera;
		
		private var so:SharedObject;
		
		/** 
		 * init is called by the application after it's children are created. 
		 * See the application tag.
		 */
		private function init():void{
			writeln("Initializing application... in player: " + flash.system.Capabilities.version + "\n");
			
			// Create the NetConnection and listen for NetStatusEvent and 
			nc = new NetConnection();
			nc.addEventListener(NetStatusEvent.NET_STATUS, netStatus);
			nc.addEventListener(SecurityErrorEvent.SECURITY_ERROR, netSecurityError);
			
			// Create two Video objects and add them to the application's video HBox.
			// To put them in the HBox I had to put them in a UIComponent first...
			
			var videoHolder:UIComponent = new UIComponent();
			videoHolder.setActualSize(330, 120);
						
			localVideo = new Video(160, 120);
			videoHolder.addChild(localVideo);
			localVideo.x = 0;
			localVideo.y = 0;
			
			returnedVideo = new Video(160, 120);
			videoHolder.addChild(returnedVideo);
			returnedVideo.x = 170;
			returnedVideo.y = 0;
			
			videoHBox.addChild(videoHolder);
		}
		
		private function soOnSync(event:SyncEvent):void {
			// Show the ChangeList:
			var info:Object;
			var changeList:Array = event.changeList;
			writeln("soOnSync> event: " + event.toString());
			
			for (var i:Number=0; i < changeList.length; i++) {
				info =  changeList[i];
				for (var p:String in info){
					writeln("soOnSync> changeList[" + i + "]." + p + ": " + info[p]);
				}
			}
			// Show all data in the text area
			writeln("---- Shared Object Data -----");
			for(var p:String in so.data) writeln("soOnSync> so.data." + p + ": " + so.data[p]);
			
			writeln(""); // just add some space
			
			// Show the prop value in the TextInput even though it will be there already.
			soInput.text = so.data.prop;
		}

		/** 
		 * This method could be named anything - even onStatus. I've named it
		 * netStatus as in Dave Simmons example. In the docs they use the
		 * name netStatusHandler. Instead of receiving an information object
		 * it is passed an event that contains the information object.
		 */
		private function netStatus(event:NetStatusEvent):void {
			// Write out information about connection events:
            writeln("netStatus: " + event);
            var info:Object = event.info;
            for(var p:String in info) {
                writeln(p + " : " + info[p]);
            }
            writeln("");
            
            // Change the application state if we connect or disconnect.
            switch(info.code){
            	case "NetConnection.Connect.Success":
            		// Adjust Buttons
            		connectButton.label = "Disconnect";
            		connectButton.enabled = true;
            		callButton.enabled = true;  
            		sendButton.enabled = true; 
            		soSetButton.enabled = true;
            		
            		// Close and recreate streams
            		if (inStream) inStream.close();
            		if (outStream) outStream.close(); 
            		inStream = new NetStream(nc);       
            		outStream = new NetStream(nc);
            		
            		// Capture video and publish it
            		camera = Camera.getCamera();
            		// Avoid an error if there is no camera!
            		if (camera){
            			camera.setMode(160, 120, 6, true);
            			localVideo.attachCamera(camera);
            			outStream.attachCamera(camera);
            		}	
            		outStream.publish("testVideo");
          			writeln("Publishing stream...\n");
          			
          			// Play the video-only stream and set it to receive 
          			// showMessage remote method calls. 
            		inStream.play("testVideo");
            		//inStream.showMessage = showMessage;
            		inStream.client = this;
            		//inStream["showMessage"] = showMessage;
            		returnedVideo.attachNetStream(inStream);

          			writeln("Creating local copy of a persistent Remote SharedObject...\n");			
					so = SharedObject.getRemote("mySo", nc.uri, true);
					if(so){
						so.addEventListener(SyncEvent.SYNC, soOnSync);
						so.connect(nc);
					}
            		 	
            	break;
            	case "NetConnection.Connect.Closed":
            		connectButton.label = "Connect";
            		connectButton.enabled = true;
            		callButton.enabled = false;
            		sendButton.enabled = false;
            		if (inStream) inStream.close();
            		if (outStream) outStream.close();         	
            	break;
            }
        }

		/** 
		 * Reports a security violation error after trying to connect.
		 */
		private function netSecurityError(event:SecurityErrorEvent):void {
            writeln("netSecurityError: " + event);
        }
        
        /** 
         * connect is called whenever the connectButton is pressed
         * and decides what to do based on the current label of the button.
         * NOTE: the rtmp address is in this function. Change it if you need to.
         */
        private function connect():void{
        	switch(connectButton.label){
        		case "Connect":
        			connectButton.label = "Wait";
        			connectButton.enabled = false;
        			nc.connect("rtmp:/flex2FMS");
        		break;
        		case "Disconnect":
        			connectButton.label = "Connect";
        			connectButton.enabled = true;
        			nc.close();
        		break;
        	}
        	
        }
        
        /** echoResult is called when the echoResponder gets a result from the nc.call("echoMessage"..) */
        private function echoResult(msg:String):void{
        	writeln("echoResult: " + msg + "\n");
        }
        
        /** echoResult is called when the echoResponder gets a error after a nc.call("echoMessage"..) */
        private function echoStatus(event:Event):void{
        	writeln("echoStatus: " + event);
        }
        
        /** echoMessage is called when the callButton is pressed to test nc.call */
        private function echoMessage():void{
        	nc.call("echo", echoResponder, echoMessageInput.text);
        }

		/** sendMessage is called when the sendButton is pressed to test ns.send */
		private function sendMessage():void{
			outStream.send("showMessage", sendMessageInput.text);
		}


		/** sendMessage is called when the sendButton is pressed to test ns.send */
		private function setSO():void{
			so.setProperty("prop", soInput.text);
		}
		
		/** showMessage is the function called by the inStream. See the netStatus function */
		public function showMessage(msg:String):void{
			writeln("showMessage: " + msg + "\n");
		}
				
		/** 
		 * writeln writes text into the traceArea TextArea instead of using trace. 
		 * Note to get scrolling to the bottom of the TextArea to work validateNow()
		 * must be called before scrolling.
		 */		
		public function writeln(msg:String):void{
			traceArea.text += msg + "\n";
			traceArea.validateNow();
			traceArea.verticalScrollPosition = traceArea.maxVerticalScrollPosition;
		}
	
		
	]]>
</mx:Script>
</mx:Application>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -