Flash网络摄像头捕获

时间:2015-04-27 06:26:15

标签: actionscript-3 flash video

我正在尝试构建AS3网络音频/视频捕获。我已经成功地尝试在这个伟大的tutorial的帮助下显示网络摄像头流。下一步是如何在用户开始录制时捕获视频。以下是我的代码:

// video stuff
        private var camW:int = 300;
        private var camH:int = 300;
        private var video:Video;

        // block stuff
        private var rows:int = 3;
        private var cols:int = 3;
        private var blockW:int = camW/cols;
        private var blockH:int = camH/rows;
        private var pointArray:Array = new Array();

        public function Main():void {

            // Checks if camera is installed
            if (Camera.names.length > 0) { 
                trace("User has at least one camera installed."); 
                var camera:Camera = Camera.getCamera();
                camera.setMode(camW, camH, 30);
                camera.addEventListener(StatusEvent.STATUS, statusHandler); 
                video = new Video(camW, camH);
                video.attachCamera(camera);

                initBlocks();
                addEventListener(Event.ENTER_FRAME, updateBlocks);
            }else { 
                trace("User has no cameras installed."); 
                var text:TextField = new TextField();
                text.text = "Device Not Found! Please check if the camera is installed.";
                text.x = 20;
                text.y = 20;
                text.width = 500;
                addChild(text);
            }

            function statusHandler(event:StatusEvent):void {
                // This event gets dispatched when the user clicks the "Allow" or "Deny" 
                // button in the Flash Player Settings dialog box. 
                trace(event.code); // "Camera.Muted" or "Camera.Unmuted"

                switch (event.code) {
                    case "Camera.Muted": 
                        trace("User clicked Deny.");
                        var text:TextField = new TextField();
                        text.text = "Device Denied Permission! Please provide permission to record video!";
                        text.x = 20;
                        text.y = 20;
                        text.width = 500;
                        addChild(text);
                        break; 
                    case "Camera.Unmuted": 
                        trace("User clicked Accept."); 
                        break; 
                } 
            }

        }

        private function initBlocks():void {
            for (var r:int = 0; r < rows; r++) {
                for (var c:int = 0; c < cols; c++) {
                    var newBlock:Sprite = new Sprite();
                    newBlock.name = "block" + r + c;
                    var p:Point = new Point(c * blockW, r * blockH);
                    newBlock.x = c * (blockW) + 20;
                    newBlock.y = r * (blockH) + 20;
                    pointArray[newBlock.name] = p;

                    var bmpd:BitmapData = new BitmapData(blockW, blockH);
                    var bmp:Bitmap = new Bitmap(bmpd);
                    bmp.name = "myBmp";

                    newBlock.addChild(bmp);
                    addChild(newBlock);             
                }
            }           
        }

        private function updateBlocks(e:Event):void {
            var srcBmpd:BitmapData = new BitmapData(camW, camH);
            srcBmpd.draw(video);
            for (var r:int = 0; r < rows; r++) {
                for (var c:int = 0; c < cols; c++) {
                    var b_mc:Sprite = this.getChildByName("block" + r + c) as Sprite;
                    var bmp:Bitmap = b_mc.getChildByName("myBmp") as Bitmap;
                    var p:Point = pointArray[b_mc.name];

                    bmp.bitmapData.copyPixels(srcBmpd, new Rectangle(p.x, p.y, blockW, blockH), new Point());
                }
            }
        }

我是AS3的新手,刚刚开始2天前。 AS3中音频/视频捕获的逻辑是什么?任何指针/演示/教程/书籍赞赏

1 个答案:

答案 0 :(得分:0)

我很难开始使用Flash网络摄像头录制。这里的几个帖子(this one)建议使用某种类型的Flash服务器,如Adobe Flash Server / Red5 Server。我做了同样的事。我安装了Red5 Server(请确保您使用正确的JRE让服务器在安装后顺利运行 - 因为我在安装演示时遇到问题)并使用Flex FlashDevelop IDE(使用Flex 3项目创建一个新项目 )。以下是工作示例代码:

Main.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
    <mx:Script source="Main.as"/>

    <mx:VideoDisplay width="300" height="300" autoPlay="true" creationComplete="initCamera()" id="video"></mx:VideoDisplay>

    <mx:Button label="Record" click="startRecording()" />
    <mx:Button label="Stop" click="stopRecording()" />  
</mx:Application>

Main.as

/**
 * ...
 * @author VishwaKumar
 */

import flash.events.NetStatusEvent; 
import flash.media.Microphone;
import flash.net.NetConnection;
import flash.net.NetStream;
import mx.controls.Alert;
import flash.media.Camera;
import flash.media.Video;
import flash.display.Sprite;

public var camera:Camera;
public var nc:NetConnection;
public var ns:NetStream;
public var mic:Microphone;

public function initCamera():void {
    var camW:int = 300;
    var camH:int = 300;

    if (Camera.names.length > 0) {
        camera = Camera.getCamera();
        camera.setMode(camW, camH, 30);
        video.attachCamera(camera);

        mic = Microphone.getMicrophone();


    }else { 
        Alert.show("Device Not Found! Please check if the camera is installed.","Hardware Error!");
    }


}

public function startRecording():void {
    nc = new NetConnection();
    nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
    nc.connect("rtmp://127.0.0.1/oflaDemo");

}

public function netStatusHandler(event:NetStatusEvent):void {
    trace(event.info.code);
    switch(event.info.code) {
        case "NetConnection.Connect.Success":
            ns = new NetStream(nc);
            ns.publish("test","record");
            ns.attachAudio(mic);
            ns.attachCamera(camera);
            break;
        default :
            Alert.show("NetConnection.Connect.error");
            break;
    }
}

public function stopRecording():void {
    ns.close();
}

Flex Project Structure