Search code examples
apache-flexvideoadobenetstreamnetconnection

newbie: flex netstream how to get my code stream and receive netstreams correctly?


I have problems getting my flex code to work, below is my code I try to netstream a webcam and receive it and use 2 functions for that. Any flex guru can help me fix these functions?

            function onNetConnectionPublish():void { 
                StatusMessage("onNetConnectionPublish called");
                ncNetStream = new NetStream(nc, NetStream.DIRECT_CONNECTIONS);  
                ncNetStream.addEventListener(NetStatusEvent.NET_STATUS, sendNetStreamHandler);  
                ncNetStream.publish("media");  
                ncNetStream.attachAudio(Microphone.getMicrophone());  
                ncNetStream.attachCamera(Camera.getCamera()); 
            } 

and:

            function connectToRemote(remoteId:String) { 
                StatusMessage("connectToRemote(" + remoteId + ")"); 

                ncNetStream = new NetStream(nc, remoteId);  
                ncNetStream.addEventListener(NetStatusEvent.NET_STATUS, receiveNetStreamHandler);  
                ncNetStream.play("media");  
            } 

display video:


Solution

  • The Publisher Application:

    private function Publisher():void{
    
            var camera1:Camera = Camera.getCamera();
            var video:Video = new Video(285, 254);
            if (camera1)
            {
                video.attachCamera(camera1);
                VideoDisplay1.addChild(video);
                camera1.addEventListener(ActivityEvent.ACTIVITY, camera_activity);
                camera1.addEventListener(StatusEvent.STATUS, camera_status);
            }
    
            var nc:NetConnection = new NetConnection();
            nc.connect("rtmp://your/stream/url");
            nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
    
            function netStatusHandler(event:NetStatusEvent):void {
                switch (event.info.code) {
                    case "NetConnection.Connect.Success":
                        var ns:NetStream = new NetStream(nc,NetStream.CONNECT_TO_FMS);
                        ns.attachCamera(camera1);
                        ns.publish("videofeed", "live");
                        break;
                    case "NetStream.Play.StreamNotFound":
                        trace("Unable to locate video: ");
                        break;
                        }
                 }
        }
    

    The Reciever Application :

            import mx.utils.ObjectUtil;
    
            private var nc:NetConnection;
            private var ns:NetStream;
            private var video:Video;
            private var meta:Object;
    
            private function init():void {
                var nsClient:Object = {};
                nsClient.onMetaData = ns_onMetaData;
                nsClient.onCuePoint = ns_onCuePoint;
    
                nc = new NetConnection();
                nc.connect(null);
    
                ns = new NetStream(nc);
                ns.play("http://www.helpexamples.com/flash/video/cuepoints.flv");
                ns.client = nsClient;
    
                video = new Video();
                video.attachNetStream(ns);
                uic.addChild(video);
            }
    
            private function ns_onMetaData(item:Object):void {
                trace("meta");
                meta = item;
                // Resize Video object to same size as meta data.
                video.width = item.width;
                video.height = item.height;
                // Resize UIComponent to same size as Video object.
                uic.width = video.width;
                uic.height = video.height;
                panel.title = "framerate: " + item.framerate;
                panel.visible = true;
                trace(ObjectUtil.toString(item));
            }
    
            private function ns_onCuePoint(item:Object):void {
                trace("cue");
            }
    

    Reciever mxml code :

    <mx:Panel id="panel" visible="false">
        <mx:UIComponent id="uic" />
        <mx:ControlBar>
            <mx:Button label="Play/Pause" click="ns.togglePause();" />
            <mx:Button label="Rewind" click="ns.seek(0); ns.pause();" />
        </mx:ControlBar>
    </mx:Panel>