I'm accessing the camera using the SurfaceView and CameraSource and showing the image on the screen. The application also need to use the flash light. The camera view freezes when I turn on the flash light. I don't want this to happen. I don't understand why. When the flash light is on, the camera must be active at the same time. For example, taking pictures with the flash light on the phone. I am using the following codes. I'd appreciate it if you could help or indicate what caused the problem.
*My code is working. But when I turn on the flashlight, the camera image remains attached.
MyActivity
public class MyActivity extends AppCompatActivity {
    SurfaceView cameraPreview;
    CameraSource cameraSource;
    final int RequestCameraPermissionID = 1001;
    ImageButton Flash_On_Button;
    private Camera cam;
    Camera.Parameters p;
@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case RequestCameraPermissionID: {
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                        return;
                    }
                    try {
                        cameraSource.start(cameraPreview.getHolder());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            break;
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
        Flash_On_Button  = findViewById(R.id.Flash_On_Button);
        Flash_On_Button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FlashON();          
            }
        });
        cameraPreview  = findViewById(R.id.cameraPreview);
        cameraSource = new CameraSource
                .Builder(this, barcodeDetector)
                .setAutoFocusEnabled(true)
                .build();
        CameraStart() ;
}
    private void CameraStart() {
                cameraPreview.getHolder().addCallback(new SurfaceHolder.Callback() {
                    @Override
                    public void surfaceCreated(SurfaceHolder surfaceHolder) {
                        if (ActivityCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                            ActivityCompat.requestPermissions(ScanActivity.this,
                                    new String[]{Manifest.permission.CAMERA},RequestCameraPermissionID);
                            return;
                        }
                        try {
                            cameraSource.start(cameraPreview.getHolder());
                            Toast.makeText(ScanActivity.this, "surface Created", Toast.LENGTH_SHORT).show();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    @Override
                    public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
                        Toast.makeText(ScanActivity.this, "surface Changed", Toast.LENGTH_SHORT).show();
                    }
                    @Override
                    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
                        cameraSource.stop();
                        Toast.makeText(ScanActivity.this, "surface Destroyed", Toast.LENGTH_SHORT).show();
                    }
                });
    }
    public void FlashON(){
            try {
                if (getPackageManager().hasSystemFeature(
                        PackageManager.FEATURE_CAMERA_FLASH)) {
                    cam = Camera.open();
                    p = cam.getParameters();
                    p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                    cam.setParameters(p);
                    cam.startPreview();
                }
            } catch (Exception e) {
                e.printStackTrace();
                Toast.makeText(getBaseContext(), "Exception flashLightOn()", Toast.LENGTH_SHORT).show();
            }
        }
}
