0

I'm building an android video application on flex mobile that can switch between front and back camera.I wrote a function that switch between the two cameras but it does that only once and when i try to press the button again to switch the camera again it doesn't work and it keeps the front camera.Here is the code that i wrote:

protected function changeCamera(event:MouseEvent):void{

            for(var i:int=0;i<Camera.names.length;i++){
                var compareCam:Camera;
                compareCam=Camera.getCamera(Camera.names[i]);
                if(compareCam.name!=camera.name){
                    camera=Camera.getCamera(compareCam.name);
                    video.attachCamera(camera);

                }

            }

Can any one help me please ?

adel oueslati
  • 117
  • 2
  • 6

1 Answers1

1

There is one bug with the logic in this code, you keep iterating after set the camera. So It will always set the last camera in the array. It is missing one break instruction, check the updated code below:

protected function changeCamera(event:MouseEvent):void{

    for(var i:int=0;i<Camera.names.length;i++){
       var compareCam:Camera;
       compareCam=Camera.getCamera(Camera.names[i]);
       if(compareCam.name!=camera.name){
          camera=Camera.getCamera(compareCam.name);
          video.attachCamera(camera);
          //stop the loop after set the camera.
          break;

        }

 }
fmodos
  • 4,472
  • 1
  • 16
  • 17
  • 1
    You didn't specify it explicitly; but I think the issue is that he changing his compare variable, camera, inside the loop. I think the break statement will indeed fix it. – JeffryHouser May 21 '13 at 18:13
  • @Reboog711 that is true, but only by adding the break instruction it will solve the problem, because the camera variable will keep the last value. – fmodos May 21 '13 at 18:15
  • @Reboog711 also this is a broken logic if there is a scenario for a device with 3 or more cameras. – fmodos May 21 '13 at 18:19
  • Using break isn't the only solution. You could have saved the current camera name in a local variable; thus changing the camera variable would not affect the condition. But, yes, I agree that this would probably lead to undesirable results on devices with more than 1 camera. – JeffryHouser May 21 '13 at 18:32
  • Thank you for you help , i added the break instruction and it works now . – adel oueslati May 22 '13 at 12:23