title: Torch for camera "0" is not available due to an existing camera user (code 7)
body: <p>I've been trying to implement a <strong>turn on/off flashlight</strong> in my code scanning app, but I've found no good solution to the problem I am facing.</p>
  <p>the code that takes care of scanning codes: (<code>MainActivity.kt</code>)</p>
  <pre class="lang-kotlin prettyprint-override"><code>val clipboard = getSystemService(context, ClipboardManager::class.java)
  val compoundBarcodeView = remember {
      CompoundBarcodeView(context).apply {
          val capture = CaptureManager(activity, this)
          capture.initializeFromIntent(
              Intent(Intent.ACTION_MEDIA_SCANNER_STARTED).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK),
              null
          )
          this.setStatusText(&quot;&quot;)
          capture.decode()
          this.resume()
          this.decodeSingle { result -&gt;
              result.text?.let { barCodeOrQr -&gt;
                  contentOfScannedCode = barCodeOrQr
                  showPopup = true // basically this takes care of displaying a popup message, you do not have to add this
              }
          }
      }
  }
  </code></pre>
  <p>later this is used by:</p>
  <pre class="lang-kotlin prettyprint-override"><code>if(!showPopup) {
      AndroidView(
          modifier = Modifier.fillMaxSize(),
          factory = { compoundBarcodeView },
      )
      Row(
          Modifier
              .padding(15.dp)
              .border(
                  width = 1.dp,
                  color = colorResource(id = R.color.button_background_light_blue),
                  shape = RoundedCornerShape(10.dp)
              )
      ){
          IconButton(onClick = {}) {
              Icon(painterResource(id = R.drawable.ic_photo), contentDescription = &quot;scan code from image&quot;, tint = colorResource(
                  id = R.color.button_background_light_blue
              ))
          }
          IconButton(onClick = {
              currentFlashlightMode = if(currentFlashlightMode == 0) {
                  Camera(context).startFlashlight()
                  1
              } else {
                  Camera(context).stopFlashlight()
                  0
              }
          }) {
              Icon(painterResource(id = R.drawable.ic_flashlight), contentDescription = &quot;turn on flashlight&quot;, tint = colorResource(
                  id = R.color.button_background_light_blue
              ))
          }
      }
  }
  </code></pre>
  <p>and as you can guess I want to toggle flashlight state when user taps the <strong>flashlight</strong> icon, and the code that should take care of that is:</p>
  <pre class="lang-kotlin prettyprint-override"><code>class Camera(private val context: Context) {
      fun startFlashlight(){
          val canUseFlashlight = context.packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)
          var cameraId = &quot;&quot;
          if(!canUseFlashlight){
              Toast.makeText(context, &quot;your device doesn't support flash light&quot;, Toast.LENGTH_SHORT).show()
          } else {
              val camManager = context.getSystemService(Context.CAMERA_SERVICE) as CameraManager
              try {
                  cameraId = camManager.cameraIdList[0]
              } catch (e: CameraAccessException) {
                  logcat{e.printStackTrace().toString()}
              }
  
              try {
                  logcat { cameraId }
                  camManager.setTorchMode(cameraId, true)
              } catch (e: CameraAccessException) {
                  logcat{e.printStackTrace().toString()}
              }
          }
      }
  
      fun stopFlashlight(){
          val canUseFlashlight = context.packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)
          var cameraId = &quot;&quot;
          if(!canUseFlashlight){
              Toast.makeText(context, &quot;your device doesn't support flash light&quot;, Toast.LENGTH_SHORT).show()
          } else {
              val camManager = context.getSystemService(Context.CAMERA_SERVICE) as CameraManager
              try {
                  cameraId = camManager.cameraIdList[0]
              } catch (e: CameraAccessException) {
                  logcat{e.printStackTrace().toString()}
              }
  
              try {
                  logcat { cameraId }
                  camManager.setTorchMode(cameraId, false)
              } catch (e: CameraAccessException) {
                  logcat{e.printStackTrace().toString()}
              }
          }
      }
  }
  </code></pre>
  <p>and It probably would work if I stopped a camera, turned on/off flashlight and resumed the camera progress but that doesn't seem very user friendly, so I've tried to use both flashlight and camera simultaneously and that resulted in</p>
  <blockquote>
  <p>Torch for camera &quot;0&quot; is not available due to an existing camera user (code 7)</p>
  </blockquote>
  <p>Any ideas how to fix that?</p>