简体   繁体   中英

Can't display video coming in from NetStream properly

I am basically starting work with Flex and netstream for video calls. So I was able to read a bit about Netstreams and streaming and I wrote this code to get my camera and publish my stream in a video display below in the view but even though I pass through all the methods without any error, the display is not showing so I don't really know what's going on. Here is what I did.

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" applicationDPI="160" creationComplete="start();">
    <fx:Script>
        import flash.media.Camera;
        import flash.media.Video;
        import flash.net.NetConnection;
        import flash.net.ObjectEncoding;
        import flash.events.AsyncErrorEvent; 
        import flash.events.NetStatusEvent;
        import flash.net.NetStream;
        import mx.graphics.ImageSnapshot;
        import mx.graphics.codec.JPEGEncoder;

        public var camera:Camera;
        var video:Video;
        public var myVideo:Video;
        private var nc:NetConnection;
        private var rtmpf:String="rtmfp://p2p.rtmfp.net/61c33c80be7022350a0dea3d-960194f988ba/";
        private const DEVKEY:String = "61c33c80be7022350a0dea3d-960194f988ba";

        public var in_ns:NetStream;
        public var out_ns:NetStream;
        public function start():void{
            trace("Started the start function");
            nc=new NetConnection();
            nc.objectEncoding = ObjectEncoding.AMF0;
            nc.client=this;
            nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
            nc.connect(rtmpf);
        }
        public function netStatusHandler(event:NetStatusEvent):void{
            switch(event.info.code){
                case "NetConnection.Connect.Success":
                    trace("Received the status");
                    initStart();
                    default:
                    trace( event.info.code);
            }
        }

        public function initStart():void{
            trace("Started the initstart function");
            initNetStream();
            initMyVideo();
            publish();
            playIt();

        }

        public function initNetStream():void{
            trace("Started the initNetstream start function");
            out_ns=new NetStream(nc);
            out_ns.client=this;
            in_ns=new NetStream(nc);
            in_ns.client=this;
        }

        public function publish():void{
            trace("Started the publish function");
            camera=Camera.getCamera();
            out_ns.attachCamera(camera);
            out_ns.publish("Me", "live");

        }
        public function startCamera(muteCam:Boolean=false):void{
            if(!video)
            video = new Video(); 
            trace("Started the startCamera function");
            camera=Camera.getCamera();
            if(muteCam){
                video.attachCamera(camera);
                //out_ns.attachCamera(camera);
                //out_ns.publish("Me", "live");
                vidHolder.addChild(video);
            }else{
                video.attachCamera(null);
                if(contains(video))
                    vidHolder.removeChild(video);
                //camera=null;

            }


        }
        public function initMyVideo():void
        {
            trace("Started the initmyvideo function");
            myVideo = new Video(230,160);
            myVideo.x = 10;
            myVideo.width = 230; 
            myVideo.height = 160;
            myVideo.y = 30;
        //  myVid.addChild(myVideo);
        }

        public function playIt():void{
            trace("Started the play it function");
            myVideo.attachNetStream(in_ns);
            in_ns.play("Me");
            myVid.addChild(myVideo);
        }

        public function stopCamera():void{
            vidHolder.removeChild(video);
        }
    </fx:Script>
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->

    </fx:Declarations>

    <s:Button x="116" y="28" label="Start" click="startCamera(true)"/>
    <s:VideoDisplay id="vidHolder" x="31" y="87" width="200" height="100"
                    />
    <s:VideoDisplay id="myVid" x="31" y="250"/>
    <s:Button id="stop" x="208" y="28" label="Stop" click="startCamera(false)"/>

</s:Application>

These are some reasons that is stopping from displaying the stream.

Are you testing it on the same browser using your same webcam device driver ?

Possibility is that you were not able to see it maybe since the driver is being used. Get a virtual webcam driver from a website ManyCam and test it out. Your application will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM