Detect if camera is capturing in Flash

2019-08-13 19:01发布

问题:

I'm having trouble with Camera.getCamera() in Flash AS3. If there are multiple camera drivers, it does not necessarily pick the correct one. If the default is the correct, the program works fine; however, if another driver is selected as the default then LED on the webcam does not come on and no video is captured. I can cycle through the available cams and select an arbitrary one. I don't, however, know how to determine if the selected camera is correct. It is not returning null. I considered using camera.currentFPS, but it's not clear when this gets updated. I'm really struggling to determine dynamically which camera will do the trick!

回答1:

Check out this blog post, it could be of use for what you're trying to do.

I remeber I used this class a while ago, because there were problems detecting the right webcam in Macs in an app I was working on. Never experienced this problem for Windows or Linux, though.

I was on a rather tight deadline then, so I didn't really investigate much about the subject. I remember this code had some problems with some built-in cameras in laptops running Windows. I ended up just checking flash.system.Capabilities.os to detect whether the client was a Mac or not. For Macs, I used the code in this class, as is. For everything else, I just detected the camera the "regular way" (i.e., getting the default camera). It worked fine in all our tests, and since time was short, that settled it.

To be honest, though, I'm not sure if it covers some corner cases. You might want to check that, but hopefully, this will give you at least some pointers -- if not a solution.

Hope this helps.



回答2:

I recognize this is an old question that has been answered, but seeing as this link is the strongest referrer to my post i felt it necessary to make a more informed answer, as well as explain new updates to the CameraDetection library.

causes:

there are a few well known reasons that might cause Camera.getCamera() to return a valid Camera object, but the Camera won't actually work.

  1. if the camera is already in use. This is most common if you have skype running on your computer. you don't even have to be using the camera at that moment, but the last i checked, if skype is open, it is stealing your camera.

  2. mac laptops have/used to have multiple cameras listed even though they were not connected. this frequently showed up as DVI camera and something else (sorry for the poor memory). if for some reason one of these 'disconnected' cameras was chosen as the default, then you will experience your problem.

  3. the google chat/video adapter have caused problems in the past. simply having installed it caused many issues for people for a while (this may have started ~2011?) since you never know which OS or player version you'll get this may still be an issue

why this problem sucks:

determining if a Camera is actually working is a pain. the best option we have is to monitor the Camera's activity and fps to see if it is working.

what you should expect

to determine the behavior of a working camera, i use a timer to check on the fps and activity levels of the Camera. this is a common result: (this timer is set at 100ms)

fps:0   activity:-1
 ...  x20  ...
fps:0   activity:100
fps:0   activity:100
fps:8.1 activity:5
fps:8.5 activity:46
fps:8.2 activity:3
fps:8.4 activity:3

so, the camera appears unresponsive for 2.1 whole seconds!!! what!?!?!? I know. but then it finally kicks in. this is common behavior even though I am testing this on a 6core 3.3GHz machine with little running. my library keeps checking the camera waiting for these activity and fps levels to change.

what you get when it doesn't work

can you guess what happens when the camera doesn't work? you guessed it!

you get this non-stop, forever

fps:0 activity:-1
fps:0 activity:-1
fps:0 activity:-1

so my code will check a camera by default for 3 whole seconds, waiting for some activity, and then quit. this is the bad part, maybe all the hardware sucks and the cpu is already overloaded and this makes it take longer than 3 seconds to get a response? woe is us actionscript guys.

CameraDetection

my library does all this and more! it even handles getting permission from the user to use the media devices!

updated 2012.10.06, it now has:

  • MediaPermissions - a class that handles the worst part of Cameras. it will get permission, tell you if it was allowed or denied and even tell you if the user had the 'remembered' checkbox selected
  • CameraChecker - an abstraction to clean up the library.
  • CameraDetection - simpler to use and tells you even more! get events when the permission dialog opens and closes, when the user gives permission, and when a camera is found, no cameras to use, and even when there are cameras but none of them worked
  • logging! - almost done, i will be supporting logging of all the data that CameraDetection uses, so maybe we can get enough data to push Adobe into fixing something...

please visit CameraDetection homepage and don't forget to view the other posts about it!

to get the code you can go directly to github



回答3:

The thing you want is probably activityLevel http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/flash/media/Camera.html#activityLevel But if you're just having issues selecting the "correct" camera in OSX I suggest you use a drop down and let the user select. Use the Camera.names to fill the selection drop-down and use the selected index to do the actual selection. One major gotcha is to make sure you use a string to select the camera, it is the opposite of Microphone selection wherein a number is used.

//camera uses string
var camera:Camera = Camera.getCamera("0");

//microphone uses number
var mic:Microphone = Microphone.getMicrophone(0);

//method to handle the selection of a new camera
private function changeCamera(evt:ListEvent):void {
    camera = Camera.getCamera(evt.currentTarget.selectedIndex.toString());
}

I have seen differences with windows and osx myself but I don't recall the exact problem.