The DeviceDetector class isn't very good -- here is an improvement

2 replies [Last post]
Joined: 12/15/2009
Points: 27

The method used in the DeviceDetector is EXTREMELY slow it takes almost 10 seconds to get a mic and camera.

I think rather than monitoring the video display you can actually just look at the currentFPS property of the camera object. This property is 0 when there is no valid device and positive when there is a valid device.

Here are the changes I made:

private var currCamera:Camera = Camera.getCamera("0");
var oneMinuteTimer:Timer = new Timer(100);
public function detectCamera ():void
{
       
        oneMinuteTimer.addEventListener(TimerEvent.TIMER,tryDetectingCamera);
        oneMinuteTimer.start();
}

private var triesOnCurrent:int = 0;
private var testVideo:Video = new Video(50,50);
private var currIndex:Number = 0;
private function tryDetectingCamera(e:Event):void{
       
        if(triesOnCurrent>5){
                currIndex++;
                if(currIndex>=Camera.names.length){
                        dispatchCameraError ();
                }
                triesOnCurrent = 0;
                currCamera = Camera.getCamera(currIndex.toString());
                testVideo.attachCamera(currCamera);
        }
       
        triesOnCurrent++;
        if(currCamera){
                trace("FPS:" +  currCamera.currentFPS);
        if( currCamera.currentFPS>0){
                trace("Found valid Camera FPS:" +  currCamera.currentFPS);
                camera = currCamera;
                dispatchCameraSuccess ();
                oneMinuteTimer.stop();
        }}else{trace("NO CAMERA : " + currIndex);}
}

Perhaps you can polish this and work it in to a future release?

Joined: 01/05/2009
Points: 1697

Thanks for the suggestion!

Comparing to the bitmap test, what is the time difference?
We found many techniques are causing run-time errors with Flash on mac, did you test this on mac or PC?

Joined: 12/15/2009
Points: 27

Didn't have time to test it too thoroughly. The problem with the DeviceDetector is more so with detecting the mic than the camera -- that is what was taking a really long time.

As far as time goes -- it might only be faster because I sped up the delay between checks of different cameras. So maybe it breaks down on certain platforms.

I do know that this is the same way facebook does their camera detection (i looked at their decompiled swfs)