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)) }