0

I am trying to build AS3 web audio/video capture. I have successfully tried to display the webcam stream with the help of this great tutorial. The next step is how do i capture the video when the user starts recording. Below is my code:

// 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());
                }
            }
        }

I am completely new to AS3, just started 2 days ago. What is the logic for audio/video capture in AS3? Any pointers/demo/tutorials/books appreciated

VishwaKumar
  • 3,433
  • 8
  • 44
  • 72
  • I'm not sure there is a way to get a recorded video, as there are different formats, compression, etc. You have all the frames though ;) – Andrey Popov Apr 27 '15 at 10:01
  • How do i use these frames then? What are the different ways to save a webcam stream as a file using AS3 then? – VishwaKumar Apr 27 '15 at 10:05
  • Store the bitmaps into array. Then use them to construct video file - there are different libraries for this one. Here's some very old example of this: http://www.joristimmerman.be/wordpress/2008/12/18/flvrecorder-record-to-flv-using-air/ – Andrey Popov Apr 27 '15 at 10:09
  • Thanks for your reply! I implemented it with red5 server – VishwaKumar Apr 29 '15 at 09:10
  • Hey do you know any other better way apart from using flash / red5 servers that the industry is using as a best practice and i am not aware of? Thanks for your time. – VishwaKumar Apr 29 '15 at 10:38
  • No, I think Red5 is the most commonly used one. – Andrey Popov Apr 29 '15 at 12:04

1 Answers1

0

I had a very difficult time getting to work on the flash webcam recording. And as couple of posts here ( this one ) suggested to use some kind of flash servers like the Adobe Flash Server / Red5 Server. I did the same. I installed Red5 Server ( Please make sure you use the right JRE for the server to run smoothly after installation - as i had problems installing the demos ) and used Flex with FlashDevelop IDE ( Use Flex 3 Project to create a new project ). Here's below working sample code:

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

Community
  • 1
  • 1
VishwaKumar
  • 3,433
  • 8
  • 44
  • 72