使用Adobe AIR捕获和流式传输视频

时间:2014-03-28 07:24:11

标签: actionscript-3 air

我试图通过网络摄像头捕获视频并使用AIR将其转换为RTMP流。但是我无法找到这样的任何示例。有什么方法可以这样做吗?请给我实现这一点的一些例子。我是ActionScript的新手,我正在使用ActionScript,我在ActionScript中看过Camera api,但它无法正常工作,可能是我错误地实现了。

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                       xmlns:s="library://ns.adobe.com/flex/spark" 
                       xmlns:mx="library://ns.adobe.com/flex/mx"
                       title="Hello Air Appp" >
    <s:Label text="Hello AIR" />

        <fx:Style> 
        @namespace s "library://ns.adobe.com/flex/spark"; 
        s|WindowedApplication 
        { 
            skinClass:ClassReference("spark.skins.spark.SparkChromeWindowedApplicationSkin"); 
            background-color:#999999; 
            background-alpha:"0.7"; 
        } 
    </fx:Style>

    <fx:Script>
        <![CDATA[
            import mx.events.FlexEvent;

            private var connection:NetConnection;
            private var outStream:NetStream;

            //device properties
            private var camera:Camera;
            private var microphone:Microphone;

            //video properties
            private var outVideo:Video;

            protected function application1_creationCompleteHandler(event:FlexEvent):void {
                trace("In the frst fn");
                connection = new NetConnection();
                connection.connect("rtmp://0.0.0.0/TestApp"); 
                connection.addEventListener(NetStatusEvent.NET_STATUS, onConnected);
            }

            private function onConnected(event:NetStatusEvent):void {
                if(event.info.code == "NetConnection.Connect.Success") {
                    setupVideo();
                }
            }
            private function setupVideo():void {
                //setup outgoing devices
                if (Camera.names.length > 0)  {
                    var camera:Camera = Camera.getCamera(); // Get default camera. 
                }
                else 
                    trace("No Cameras Found");
                if(Microphone.names.length>0)
                    microphone = Microphone.getMicrophone();
                microphone.addEventListener(StatusEvent.STATUS,onMicStatus,false, 0, true); 
                function onMicStatus(event:StatusEvent):void {
                    if (event.code == "Microphone.Muted") 
                        trace("Microphone access was denied."); 
                }
                //setup outgoing stream
                outStream = new NetStream(connection);
                outStream.attachCamera(camera);
                outStream.attachAudio(microphone);
                outStream.publish("DemoVideo1","live");

                //setup outgoing video and attach outgoing devices
                outVideo = new Video(250,250);
                outVideo.attachCamera(camera);
                addChild(outVideo);
            }

        ]]>
    </fx:Script>
    <fx:Declarations>

    </fx:Declarations>
</s:WindowedApplication>

1 个答案:

答案 0 :(得分:1)

最后我发现了一种从网络摄像头捕获视频并将其传输的方法。正如我所说的,我想要它AIR,所以我创建了一个ActionScript类来实现它。

package {
    import flash.display.Sprite;
    import flash.events.NetStatusEvent;
    import flash.media.Camera;
    import flash.media.Microphone;
    import flash.media.Video;
    import flash.net.NetConnection;
    import flash.net.NetStream;
    import flash.net.Responder;
    import flash.text.TextField;

    /**0...
     * @author BECKER Christophe
     */
    public class Main extends Sprite {
        //
        public var vid : Video;
        public var cam : Camera;
        public var bt : Sprite;
        public var txt : TextField ;
        public var microphone : Microphone;
        private var connection:NetConnection;

        private var outStream:NetStream;
        public var nc : NetConnection;
        public var responder : Responder;
        private var inStream:NetStream;
        private var inVideo:Video;
        public function Main() {
            connection = new NetConnection();
            connection.client = { onBWDone: function():void{ trace("onBWDone") } };
            connection.connect("rtmp://Ipaddress/TestApp"); 
            connection.addEventListener(NetStatusEvent.NET_STATUS, onConnected);

        }
            private function onConnected(event:NetStatusEvent):void {
        if(event.info.code == "NetConnection.Connect.Success") {

            if(Camera.names.length>0){
                cam = Camera.getCamera();
                cam.setMode(640, 480, 15);
            }
            if(Microphone.names.length>0)
                microphone = Microphone.getMicrophone();

            outStream = new NetStream(connection);
            outStream.attachCamera(cam);
            outStream.attachAudio(microphone);
            outStream.publish("DemoVideo1","live");


            vid = new Video(300, 300);
            vid.attachCamera(cam);
            addChild(vid);

//          inStream = new NetStream(connection);
//          inStream.play("DemoVideo1");

//          inVideo = new Video(250,250);
//          inVideo.attachNetStream(inStream);

//          addChild(inVideo);



                            }
        }


        private function metaDataHandler(meta : Object) : void {
            if (vid) {
                vid.width = meta.width;
                vid.height = meta.height;
            }
        }

    }
}