Enabling Camera Flash While Recording Video

前端 未结 4 461
遇见更好的自我
遇见更好的自我 2021-02-05 02:46

I need a way to control the camera flash on an Android device while it is recording video. I\'m making a strobe light app, and taking videos with a flashing strobe light would r

4条回答
  •  不思量自难忘°
    2021-02-05 03:22

    Try this.. hopefully it will work.. :)

     private static Torch torch;
    
              public Torch() {
                super();
                torch = this;
              }
    
              public static Torch getTorch() {
                return torch;
              }
    
              private void getCamera() {
                if (mCamera == null) {
                  try {
                    mCamera = Camera.open();
                  } catch (RuntimeException e) {
                    Log.e(TAG, "Camera.open() failed: " + e.getMessage());
                  }
                }
              }
            public void toggleLight(View view) {
                toggleLight();
              }
    
              private void toggleLight() {
                if (lightOn) {
                  turnLightOff();
                } else {
                  turnLightOn();
                }
              }
    
              private void turnLightOn() {
                if (!eulaAgreed) {
                  return;
                }
                if (mCamera == null) {
                  Toast.makeText(this, "Camera not found", Toast.LENGTH_LONG);
                       button.setBackgroundColor(COLOR_WHITE);
                  return;
                }
                lightOn = true;
                Parameters parameters = mCamera.getParameters();
                if (parameters == null) {
                        button.setBackgroundColor(COLOR_WHITE);
                  return;
             }
                List flashModes = parameters.getSupportedFlashModes();
                   if (flashModes == null) {
                       button.setBackgroundColor(COLOR_WHITE);
                  return;
                }
                String flashMode = parameters.getFlashMode();
                Log.i(TAG, "Flash mode: " + flashMode);
                Log.i(TAG, "Flash modes: " + flashModes);
                if (!Parameters.FLASH_MODE_TORCH.equals(flashMode)) {
                       if (flashModes.contains(Parameters.FLASH_MODE_TORCH)) {
                    parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
                    mCamera.setParameters(parameters);
                    button.setBackgroundColor(COLOR_LIGHT);
                    startWakeLock();
                  } else {
                    Toast.makeText(this, "Flash mode (torch) not supported",
                        Toast.LENGTH_LONG);
                           button.setBackgroundColor(COLOR_WHITE);
                    Log.e(TAG, "FLASH_MODE_TORCH not supported");
                  }
                }
              }
             private void turnLightOff() {
                if (lightOn) {
                        button.setBackgroundColor(COLOR_DARK);
                  lightOn = false;
                  if (mCamera == null) {
                    return;
                  }
                  Parameters parameters = mCamera.getParameters();
                  if (parameters == null) {
                    return;
                  }
                  List flashModes = parameters.getSupportedFlashModes();
                  String flashMode = parameters.getFlashMode();
                       if (flashModes == null) {
                    return;
                  }
                  Log.i(TAG, "Flash mode: " + flashMode);
                  Log.i(TAG, "Flash modes: " + flashModes);
                  if (!Parameters.FLASH_MODE_OFF.equals(flashMode)) {
                           if (flashModes.contains(Parameters.FLASH_MODE_OFF)) {
                      parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
                      mCamera.setParameters(parameters);
                      stopWakeLock();
                    } else {
                      Log.e(TAG, "FLASH_MODE_OFF not supported");
                    }
                  }
                }
              }
         private void startPreview() {
            if (!previewOn && mCamera != null) {
              mCamera.startPreview();
              previewOn = true;
            }
          }
    
          private void stopPreview() {
            if (previewOn && mCamera != null) {
              mCamera.stopPreview();
              previewOn = false;
            }
          }
    
          private void startWakeLock() {
            if (wakeLock == null) {
              Log.d(TAG, "wakeLock is null, getting a new WakeLock");
              PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
              Log.d(TAG, "PowerManager acquired");
              wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKE_LOCK_TAG);
              Log.d(TAG, "WakeLock set");
            }
            wakeLock.acquire();
            Log.d(TAG, "WakeLock acquired");
          }
    
          private void stopWakeLock() {
            if (wakeLock != null) {
              wakeLock.release();
              Log.d(TAG, "WakeLock released");
            }
          }
         @Override
          public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            if (Eula.show(this)) {
              eulaAgreed = true;
            }
            setContentView(R.layout.main);
            button = findViewById(R.id.button);
            surfaceView = (SurfaceView) this.findViewById(R.id.surfaceview);
            surfaceHolder = surfaceView.getHolder();
            surfaceHolder.addCallback(this);
            surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
            disablePhoneSleep();
            Log.i(TAG, "onCreate");
          }
    

提交回复
热议问题