Publish and receive webcam with flex on Flash Media Server
Filed in Code Snippets, Flex on Jun.04, 2009
This is a working sample I made for education purpose.
- This script enables your webcam
- Show in a box the local webcam copy
- Publish to FMS (Flash Media Server)
- Receive from FMS
- Display the received (streamed) video in another Box.
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()"> <mx:Script> import mx.controls.Label; import mx.controls.Alert; import mx.core.UIComponent; import mx.controls.Button; import flash.net.NetConnection; private var nc:NetConnection; private function init():void { // Connect to FMS nc = new NetConnection(); nc.client = this; nc.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus); nc.connect("rtmp://yourfms/testlive"); } // Fix for AS3 public function onBWDone():void{ } private function onNetStatus(event:NetStatusEvent):void{ //Alert.show(event.info.code ); if (event.info.code == "NetConnection.Connect.Success") { var vContainer:UIComponent = new UIComponent(); // Local webcam copy var label1:Label = new Label(); label1.x = 0; label1.y= 0; label1.text = "Your webcam if you have any:"; addChild(label1); var cam:Camera = new Camera(); cam = Camera.getCamera(); var localvid:Video = new Video(); localvid.x = 10; localvid.y = 20; localvid.attachCamera(cam); vContainer.addChild(localvid); addChild(vContainer); // Publish var ns:NetStream = new NetStream(nc); ns.attachCamera(cam); ns.publish("testlive"); } // Re-play from FMS label1 = new Label(); label1.x = 400; label1.y= 0; label1.text = "Streamed video from FMS:"; addChild(label1); var nsPlayer:NetStream = new NetStream(nc); nsPlayer.play("testlive"); var vid:Video = new Video(); vid.x = 400; vid.y = 20; vid.attachNetStream(nsPlayer); //vid.smoothing = true; vContainer.addChild(vid); addChild(vContainer); } </mx:Script> </mx:Application>
Related posts:























Leave a Reply
You must be logged in to post a comment.