android delaying flashlight

寵の児 提交于 2019-12-11 11:54:38

问题


I made a simple application with 2 buttons to test flashlights.

button1.setOnClickListener...etc
    if (cameraObj ==null){ return; }
    Camera.Parameters cameraParams =cameraObj.getParameters();
    if(cameraParams.getFlashMode() == null) { return;}
    cameraParams.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
    cameraObj.setParameters(cameraParams);
    cameraObj.startPreview(); 

button2.setOnClick etc...
    if(cameraObj==null){ return; }
    Camera.Parameters cameraParams = cameraObj.getParameters();
    cameraParams.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
    cameraObj.setParameters(cameraParams);
    cameraObj.stopPreview(); 

The application was tested on 2 devices: HTC ONE and HTC DESIRE 500

The application works good on both. The problem is: there's a delay on turning on/off the flashlight.

When I press on and off very fast, the flashlight on DESIRE 500 its turning off and on as I press the buttons, but on HTC ONE there is a delay (it looks like you are not allowed to switch that fast the flashlights). What could be the problem?


回答1:


You can solve this by adding a fixed delay using Hander which will trigger the flash after a delay,

// variable to fix the timeout duration in milliseconds
// 1000 milliseconds = 1 second
double TIME_OUT = 2*1000;

       new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {

                // time has been reached, turn the flash on
               if (cameraObj ==null){ return; }
               Camera.Parameters cameraParams =cameraObj.getParameters();
               if(cameraParams.getFlashMode() == null) { return;}
               cameraParams.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
               cameraObj.setParameters(cameraParams);
               cameraObj.startPreview(); 
                }
            }
        }, TIME_OUT);


来源:https://stackoverflow.com/questions/32091490/android-delaying-flashlight

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!