From f6bdd1b3458dee2ea0c368ef445647f66166256e Mon Sep 17 00:00:00 2001
From: Myzel394 <50424412+Myzel394@users.noreply.github.com>
Date: Sat, 25 Nov 2023 17:13:28 +0100
Subject: [PATCH] debug: Fix microphone not accessible in background
---
app/src/main/AndroidManifest.xml | 2 +-
.../myzel394/alibi/services/VideoService.kt | 69 ++++++++++---------
2 files changed, 39 insertions(+), 32 deletions(-)
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index c0254e2..dbaa892 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -53,7 +53,7 @@
android:foregroundServiceType="microphone" />
+ android:foregroundServiceType="camera|microphone" />
= Build.VERSION_CODES.Q) {
@@ -68,11 +77,10 @@ class VideoService : LifecycleService() {
this,
NotificationHelper.RECORDER_CHANNEL_NOTIFICATION_ID,
notification,
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
- ServiceInfo.FOREGROUND_SERVICE_TYPE_CAMERA
- } else {
- 0
- },
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R)
+ ServiceInfo.FOREGROUND_SERVICE_TYPE_CAMERA + ServiceInfo.FOREGROUND_SERVICE_TYPE_MICROPHONE
+ else
+ 0,
)
val cameraProviderFuture = ProcessCameraProvider.getInstance(this)
@@ -89,37 +97,36 @@ class VideoService : LifecycleService() {
// Unbind use cases before rebinding
cameraProvider?.unbindAll()
// Bind use cases to camera
- cameraProvider?.bindToLifecycle(this@VideoService, cameraSelector, videoCapture)
+ cameraProvider?.bindToLifecycle(
+ this@VideoService,
+ cameraSelector,
+ videoCapture
+ )
val options = createMediaStoreOutputOptions()
- val recording = videoCapture.output.prepareRecording(this@VideoService, options)
- .withAudioEnabled()
+ cycleTimer = Executors.newSingleThreadScheduledExecutor().also {
+ it.scheduleAtFixedRate(
+ {
+ val mainHandler = ContextCompat.getMainExecutor(this@VideoService)
- val result = recording.start(ContextCompat.getMainExecutor(this@VideoService), {})
+ mainHandler.execute {
+ runCatching {
+ recording?.stop()
+ }
- scope.launch {
- delay(15000)
-
- result.stop()
- // Unbind use cases before rebinding
- cameraProvider?.unbindAll()
- // Bind use cases to camera
- cameraProvider?.bindToLifecycle(this@VideoService, cameraSelector, videoCapture)
-
- delay(5000)
-
- val recording = videoCapture.output.prepareRecording(this@VideoService, options)
- .withAudioEnabled()
- val result = recording.start(ContextCompat.getMainExecutor(this@VideoService), {})
-
- delay(15000)
-
- result.stop()
-
- stopSelf()
- stopForeground(STOP_FOREGROUND_REMOVE)
+ val r =
+ videoCapture.output.prepareRecording(this@VideoService, options)
+ .withAudioEnabled()
+ recording =
+ r.start(ContextCompat.getMainExecutor(this@VideoService), {})
+ }
+ },
+ 0,
+ 10_000,
+ TimeUnit.MILLISECONDS
+ )
}
}, ContextCompat.getMainExecutor(this))
}