In flash as3 webcam how to get newly connected webcam name at run-time? - actionscript-3

I have a flash as3 based webcam video recorder that publishes the webcam video stream and to do this i am using the following codes :
var camera:Camera = Camera.getCamera(0);
var arr:Array = Camera.names;
if(camera != null)
{
videoWidth = topBorderContainer.width;
videoHeight = topBorderContainer.height;
camera.setMode(videoWidth, videoHeight, 30, false);
camera.setQuality(0, 100);
if (camera)
{
videoDisplay.videoObject.attachCamera(camera);
}
}
But problem is that if i am connecting a new document camera at run time and running my webcam tool then Camera.names returning the previously connected document camera name instead of returning the new document camera name.
And to get new document webcam name i have to restart my application again.
But i have no knowledge how to get newly connected document camera name at run time so if anybody know how to get the newly connected document camera name at run time please help me to solve.

It is very likely that you will not be able to pull the trick:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/media/Camera.html#getCamera()
"Scanning the hardware for cameras takes time. When the runtime finds at least one camera, the hardware is not scanned again for the lifetime of the player instance. However, if the runtime doesn't find any cameras, it will scan each time getCamera is called. This is helpful if the camera is present but is disabled; if your SWF file provides a Try Again button that calls getCamera, Flash Player can find the camera without the user having to restart the SWF file."
It is possible that Flash Player treats Workers (http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/system/Worker.html) as separate Flash Player instances and thus a new Worker would be able to access the renewed list of Cameras. You might want to try it.

Related

How to record video using the as3 camera class

I'm trying to add video recording into a mobile app I'm building. But am struggling with the recording part. So far I have got the camera class displaying in a video object with the code below.
private function init():void
{
var camera1:Camera = Camera.getCamera();
trace(Camera.names);
if (camera1)
{
var ui1:UIComponent = new UIComponent();
var video:Video = new Video (320, 240);
camera1.setMode(320, 240, 24);
camera1.setQuality(0, 100);
video.attachCamera(camera1);
ui1.addChild(video);
cameraGroup1.addElement(ui1);
}
}
From what I understand you can use the cameraUI class instead to achieve the recording part and add it to the library but it would mean using the default camera application when I would rather use my own. I imagine recording using the camera class would require a tick to grab each frame and then something like the videoFrame event but could really use some code or a point in the right direction.
Thanks in advance.
Best way to record using the Flash or AIR application is Media Server. You can connect your application with Media Server (Flash Media Server / Red5 / any other Media Server which is providing Media Recording and Publishing.) and publish your stream for recording.
Second way, I need to search out the code but logically I can say answer. We need to convert Camera stream buffer in to the binary data and we need to save binary data as video file o File system.
For that you can write down some code in ActionScript and need to write down some code in the Native Application as Native Extension.

create (webcam capture like youtube) in my website

I have a website working the same as youtube. At this moment I am trying to create a video image captured by WEBCAM.
The video image should be saved on my computer (by FLV format) first and then if the user is satisfied, he or she can upload it on the server
I am trying to use Actionscript3 in Adobe flash CS5 and Flash media server4
1- How can I do that?
2- Is the flash media server needed?
Please pay attention that we would like to allow the user to save video on his/her computer and then be able to uploaded to the server.
Many thanks.
Assuming the computer can take the overhead of doing the encoding on the fly (or has enough memory to buffer the data then can run the data through an encoding process) then the library mentioned in the SO answer here should work:
Encode video from any format to .flv format in AS3
I believe the Flash media server would only really be necessary in this case for broadcast.
Pseudocode example
private var cam:Camera;
public function Whatever()
{
//In constructor
addEventListener(Event.ENTER_FRAME, grabFrame);
cam = Camera.getCamera();
if (cam != null)
{
var vid:Video = new Video(cam.width, cam.height);
vid.attachCamera(cam);
addChild(vid);
}
}
private function grabFrame(event:Event):void
{
var bd:BitmapData = new BitmapData(cam.width, cam.height)
bd.draw(vid);
//now the BitmapData has a frame of the video, at this point you also
//would want to capture the audio then use the FLV class in the library
}
You can also check out using Red5 as an alternative open source video stream recorder.
http://distriqt.com/post/493
Cheers

Flash AS3 and webcam: problems with AIR

I'm having some problems getting the Flash AS3 Camera to work correctly. If you could help, much appreciated. I looked at olThe details:
I'm able, when publishing to a SWF, to get the webcam up and running and all works fine, popping up the 'may I access your camera dialog' which returns muted or not.
• First question: is there any way to make it so I can bypass the user permission, that is always grant it? We are running a kiosk app. Will the following method work for an AIR app? https://stackoverflow.com/questions/3266939/flash-grant-access-to-webcam-programmatically-behind-the-scenes
• Second question: as I said, I can get the webcam/Camera hookup to work fine when publishing for SWF in IDE, and in browser. But if I switch the project to publish for AIR and run the air app, or test in the IDE, I don't get the security permissions dialog coming up at all. Nothing. Perhaps the security box is off screen? Is there some way to control the placement? Is there something different about using the webcam from within AIR?
I'm happy to NOT publish to AIR, but to use SWF — simply need to be able to read/write to XML files on local disk and think that AIR only way to do that?
Thanks for any help!
The code:
private function initTracking() : void
{
var camW : int = 840;
var camH : int = 640;
// Create the camera
_cam = Camera.getCamera();
if (_cam == null)
{
trace("Unable to locate available cameras.");
return;
}
else
{
trace("Found camera: " + _cam.name);
_cam.addEventListener(StatusEvent.STATUS, camStatusHandler);
_cam.setMode(camW, camH, stage.frameRate);
// Create a video
_vid = new Video(camW, camH);
_vid.attachCamera(_cam);
trace("camera ", _cam, " attached to video ", _vid);
// Create the Motion Tracker
_motionTracker = new MotionTracker(_vid);
// We flip the input as we want a mirror image
_motionTracker.flipInput = true;
}
}
private function camStatusHandler(event:StatusEvent):void
{
trace("camStatusHandler::");
if (_cam.muted)
{
trace("Unable to connect to active camera.");
}
else
{
trace("able to connect to active camera.");
addEventListener(Event.ENTER_FRAME, onEnterFrameHandler, false,0,true);
}
// Remove the status event listener.
_cam.removeEventListener(StatusEvent.STATUS, camStatusHandler);
}
If you publish as AIR, there is no security dialog (the security box for swfs is there to stop 'hackers' gaining control of a users webcam without their knowledge).
If your code works in a swf, it should also work in an AIR app without needing any changes - assuming AIR is running on the desktop and not a mobile device?
If you are not seeing the webcam output when you publish as an AIR app, post the relevant code.
Edit:
The StatusEvent.STATUS event does not occur with AIR apps - it fires when user closes security dialog - hence camStatusHandler never gets called.
So remove camStatusHandler function completely and also this line:
_cam.addEventListener(StatusEvent.STATUS, camStatusHandler);
And add important code from camStatusHandler to the end of initTracking:
addEventListener(Event.ENTER_FRAME, onEnterFrameHandler, false,0,true);

NetStream.time starts from when camera is attached, not when NetStream.publish is called

I am recording from a webcam to AMS in an AS3 project and to get the volume level from the microphone I have to attach the microphone to a NetStream. Later when the user initiates recording the NetStream.time value counts from when the camera was attached and not from when NetStream.publish was called. If they stop the recording and record again, now NetStream.time starts from 0. So far the only way to get round this seems to be call publish and then close on the NetStream as soon as the microphone is attached. The docs for AS2 NetStream mention this fact and suggest to call NetStream.publish(false) which doesn't work in AS3, neither does just calling publish with no args.
ns = new NetStream(nc);
ns.attachCamera(cam);
ns.attachAudio(mic);
Then later
ns.publish(filename,"record");
trace(ns.time);
is the elapsed time between attaching the camera and calling publish for the first time.
The only solution I have so far is
ns = new NetStream(nc);
ns.attachCamera(cam);
ns.attachAudio(mic);
ns.publish(filename,"record");
ns.close();
the when the user starts the reording
ns.publish(filename,"record");
trace(ns.time);
ns.time is now zero. Am I missing something, is there a better solution?
You can use mic.setLoopBack(true), wich will route microphone activity to your speakers. You will now be able to see activityLevel. But then you will probaly want to set a soundTransform on the mic with volume 0, so the mic will be effectivetly muted.
Basically.
mic.setLoopBack(true);
var transform:SoundTransform = new SoundTransfrom();
transform.volume = 0;
mic.soundTransform = transform;
After you stop displaying activity level, make shure you remove the transform.

Adobe AIR, how to open a scene in a new window

I'm developing a networked AIR application in Flash Professional. I need to open two instances of the application and after searching I found that launching the app multiple times just causes an invoke event to be sent to the currently running app.
Up until now I've been using NetConnection & NetGroup (Supported by Flash Player 10.1+), now that I'm using ServerSocket & Socket, it requires the AIR 2+ runtime.
I found a solution to open a window on invoke.
my solution would be to launch a new window on invoke
function openWindow():void
{
newWin = new NativeWindow(init); //Initialize the Native Window
newWin.activate();
newWin.height = 200;
newWin.width = 300;
newWin.title = "My First New Win!";
}
and have it
gotoAndPlay(1, "Scene 1");
Is there a way to execute that on the new window? Or is there a way to open two instances of an AIR app?
Edit
You can open two instances of the same air app by changing it's ID. However, this is a very involved process every time I want to debug!
Turns out AIR on Android doesn't support ServerSockets. This means I must use non-AIR flash methods to achieve communication.
I can then achieve network testing through multiple Flash Player instances.
I don't believe ADL has the ability to run more than instance at a time.