Flashlight control in Marshmallow

前端 未结 4 1578
情话喂你
情话喂你 2021-02-07 18:45

I have a problem regarding the camera in the most recent Marshmallow build, more specifically the flashlight. On any pre-Marshmallow version all I need to do to turn the flash o

4条回答
  •  爱一瞬间的悲伤
    2021-02-07 19:06

    Building off of Saurabh7474's answer, you can toggle Marshmallow's torchMode by registering a torchCallback:

     final CameraManager mCameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
     CameraManager.TorchCallback torchCallback = new CameraManager.TorchCallback() {
         @Override
         public void onTorchModeUnavailable(String cameraId) {
             super.onTorchModeUnavailable(cameraId);
         }
    
         @Override
         public void onTorchModeChanged(String cameraId, boolean enabled) {
             super.onTorchModeChanged(cameraId, enabled);
             boolean currentTorchState = enabled;
             try {
                 mCameraManager.setTorchMode(cameraId, !currentTorchState);
             } catch (CameraAccessException e){}
    
    
    
         }
     };
    
     mCameraManager.registerTorchCallback(torchCallback, null);//fires onTorchModeChanged upon register
     mCameraManager.unregisterTorchCallback(torchCallback);
    

提交回复
热议问题