Android Camera API (Flashlight) is too slow

I am making a Flashlight application and I am using fragments. When I press the button, the light of the flashlight is delayed for more than 4 seconds, and I do not know what will happen. Also, when I press the switch button again, the flashlight does not turn off. Any idea?

I would also like to make a strobe light with another button.

I am searching on the Internet, but I have not found another option to create this function, only this.

this is my code

import android.hardware.Camera; import android.hardware.Camera.Parameters; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Switch; public class HerramientasFragment extends Fragment { private Camera cam; private Switch linterna; public HerramientasFragment() { // Required empty public constructor } @Override public void onCreate(Bundle savedInstanceState) { cam = Camera.open(); super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View masterView = inflater.inflate(R.layout.fragment_herramientas, container, false); linterna = (Switch) masterView.findViewById(R.id.switch_linterna); linterna.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Switch liternaSwitch = (Switch) v; Parameters p; if (liternaSwitch.isChecked()) { p = cam.getParameters(); p.setFlashMode(Parameters.FLASH_MODE_TORCH); cam.setParameters(p); cam.startPreview(); } else { p = cam.getParameters(); p.setFlashMode(Parameters.FLASH_MODE_OFF); cam.setParameters(p); cam.stopPreview(); } } }); return masterView; } } 
+5
source share
1 answer

It is possible that a 4 second delay is related to the hardware / operating system and is beyond your control. This does not mean that it is impossible to fix it, but I cannot find anything related to it (some Android experts may have a better idea here).

A light that does not turn off is probably because you need to add cam.release(); as indicated in this answer .

As for strobe light, I found this tutorial . It seems to be almost what you are looking for.

+2
source

All Articles