admin管理员组

文章数量:1122847

I wrote an App to record video and audio in a foreground service:

  1. MainActivity handles all permission's stuff;
  2. It launches a fragment to be the client (controller) of a foreground LivecycleService;
  3. When I "click" a button in the client it sends a message to the service and it starts recording video/audio;
  4. Clicking the button again, it sends a message to the service to stop recording of video/audio;
  5. Exiting the client inform the service, disconnect and exit;
  6. If not recording, the service stops itself and is destroyed.

This works as expected except that when recording (video/audio) and the client exits, it stops recording the audio! Restarting the client resumes the audio recording! How can this audio recording stop be prevented?

Some of what I think is pertinent code:

    recording=videoCapture.output
      .prepareRecording(this, mediaStoreOutputOptions)
      .apply {
          // Enable Audio in this recording.
          // if (PermissionChecker.checkSelfPermission(applicationContext,
          // if (PermissionChecker.checkSelfPermission(baseContext,
          if (PermissionChecker.checkSelfPermission(this@MyService,
                  Manifest.permission.RECORD_AUDIO)==PermissionChecker.PERMISSION_GRANTED)
          {   // Enables audio to be recorded for this recording.
              withAudioEnabled()
          }
      }
      // Start this new recording, and register a lambda VideoRecordEvent listener.
      // getMainExecutor: Return an Executor that will run enqueued tasks on the main thread
      //  associated with this context.
      .start(ContextCompat.getMainExecutor(this)) { recordEvent -> ...

I started with no permission check here for RECORD_AUDIO because I already did that in the MainActivity, but withAudioEnabled requires it (why?!).

Thanks for any help/comments.

I tried:

  1. Several contexts in PermissionChecker.checkSelfPermission as shown in the comments;
  2. Move the check of for audio permission out of prepareRecording;
  3. I tried to move all the MainActivity permissions stuff to inside the LifecycleService but I was unable to do that. All I know about that needs to be into an Activity.

本文标签: