I wrote an App to record video and audio in a foreground service:
- MainActivity handles all permission's stuff;
- It launches a fragment to be the client (controller) of a foreground LivecycleService;
- When I "click" a button in the client it sends a message to the service and it starts recording video/audio;
- Clicking the button again, it sends a message to the service to stop recording of video/audio;
- Exiting the client inform the service, disconnect and exit;
- 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:
- Several contexts in PermissionChecker.checkSelfPermission as shown in the comments;
- Move the check of for audio permission out of prepareRecording;
- 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.