orchestrate initialization with callback
This commit is contained in:
parent
e0b84683d0
commit
19b7d4c019
|
@ -130,6 +130,7 @@ class MainActivity : AppCompatActivity() {
|
||||||
val serviceIntent = Intent(this, RecordingService::class.java).apply {
|
val serviceIntent = Intent(this, RecordingService::class.java).apply {
|
||||||
action = RecordingService.ACTION_STOP_RECORDING
|
action = RecordingService.ACTION_STOP_RECORDING
|
||||||
}
|
}
|
||||||
|
// startService(serviceIntent) // Note: Using startService to send the stop action
|
||||||
stopService(serviceIntent)
|
stopService(serviceIntent)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -37,6 +37,19 @@ class RecordingService : LifecycleService() {
|
||||||
private var videoCapture: VideoCapture<Recorder>? = null
|
private var videoCapture: VideoCapture<Recorder>? = null
|
||||||
private var recording: Recording? = null
|
private var recording: Recording? = null
|
||||||
private lateinit var cameraExecutor: ExecutorService
|
private lateinit var cameraExecutor: ExecutorService
|
||||||
|
private var isCameraInitialized = false
|
||||||
|
|
||||||
|
private var cameraInitializedCallback: CameraInitializedCallback? = null
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
const val ACTION_START_RECORDING = "com.urkob.wittrail_android.action.START_RECORDING"
|
||||||
|
const val ACTION_STOP_RECORDING = "com.urkob.wittrail_android.action.STOP_RECORDING"
|
||||||
|
private const val CHANNEL_ID = "ForegroundServiceChannel"
|
||||||
|
private const val NOTIFICATION_ID = 1
|
||||||
|
}
|
||||||
|
private fun setCameraInitializedCallback(callback: CameraInitializedCallback) {
|
||||||
|
this.cameraInitializedCallback = callback
|
||||||
|
}
|
||||||
|
|
||||||
override fun onCreate() {
|
override fun onCreate() {
|
||||||
super.onCreate()
|
super.onCreate()
|
||||||
|
@ -54,9 +67,15 @@ class RecordingService : LifecycleService() {
|
||||||
.build()
|
.build()
|
||||||
|
|
||||||
videoCapture = VideoCapture.withOutput(recorder)
|
videoCapture = VideoCapture.withOutput(recorder)
|
||||||
|
Log.e(tag, "videoCapture initialized")
|
||||||
|
|
||||||
// Now we can start the camera
|
// Now we can start the camera
|
||||||
startCamera()
|
startCamera()
|
||||||
|
isCameraInitialized = true
|
||||||
|
|
||||||
|
isCameraInitialized = true
|
||||||
|
cameraInitializedCallback?.onCameraInitialized()
|
||||||
|
cameraInitializedCallback = null
|
||||||
}, ContextCompat.getMainExecutor(this))
|
}, ContextCompat.getMainExecutor(this))
|
||||||
|
|
||||||
Log.e(tag, "END onCreate")
|
Log.e(tag, "END onCreate")
|
||||||
|
@ -74,6 +93,7 @@ class RecordingService : LifecycleService() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private fun createNotification(): Notification {
|
private fun createNotification(): Notification {
|
||||||
val notificationIntent = Intent(this, MainActivity::class.java)
|
val notificationIntent = Intent(this, MainActivity::class.java)
|
||||||
val pendingIntent = PendingIntent.getActivity(
|
val pendingIntent = PendingIntent.getActivity(
|
||||||
|
@ -92,17 +112,23 @@ class RecordingService : LifecycleService() {
|
||||||
.build()
|
.build()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||||
useFrontCamera = intent?.getBooleanExtra("useFrontCamera", false) ?: false
|
useFrontCamera = intent?.getBooleanExtra("useFrontCamera", false) ?: false
|
||||||
when (intent?.action) {
|
when (intent?.action) {
|
||||||
|
|
||||||
ACTION_START_RECORDING -> {
|
ACTION_START_RECORDING -> {
|
||||||
Log.e(tag, ACTION_START_RECORDING)
|
if (isCameraInitialized) {
|
||||||
captureVideo()
|
captureVideo()
|
||||||
|
} else {
|
||||||
|
setCameraInitializedCallback(object : CameraInitializedCallback {
|
||||||
|
override fun onCameraInitialized() {
|
||||||
|
captureVideo()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
ACTION_STOP_RECORDING -> {
|
ACTION_STOP_RECORDING -> {
|
||||||
Log.e(tag, ACTION_STOP_RECORDING)
|
Log.e(tag, ACTION_STOP_RECORDING)
|
||||||
|
|
||||||
stopRecording()
|
stopRecording()
|
||||||
// return START_NOT_STICKY
|
// return START_NOT_STICKY
|
||||||
}
|
}
|
||||||
|
@ -113,6 +139,11 @@ class RecordingService : LifecycleService() {
|
||||||
|
|
||||||
// override fun onDestroy() {
|
// override fun onDestroy() {
|
||||||
// Log.e(tag, "ON DESTROY IS CALLED SHOUlD STOP CAMERA")
|
// Log.e(tag, "ON DESTROY IS CALLED SHOUlD STOP CAMERA")
|
||||||
|
// // Release resources
|
||||||
|
// stopRecording()
|
||||||
|
// cameraExecutor.shutdown()
|
||||||
|
// // Consider releasing cameraProvider here if needed
|
||||||
|
// Log.e(tag, "Resources released")
|
||||||
// super.onDestroy()
|
// super.onDestroy()
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
@ -135,8 +166,19 @@ class RecordingService : LifecycleService() {
|
||||||
|
|
||||||
// Implements VideoCapture use case, including start and stop capturing.
|
// Implements VideoCapture use case, including start and stop capturing.
|
||||||
private fun captureVideo() {
|
private fun captureVideo() {
|
||||||
|
if (!isCameraInitialized) {
|
||||||
|
Log.e(tag, "Camera not initialized yet.")
|
||||||
|
// Optionally, queue this request or set a flag to try again later
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
Log.e(tag, "START CAPTURE VIDEO")
|
Log.e(tag, "START CAPTURE VIDEO")
|
||||||
val videoCapture = this.videoCapture ?: return
|
|
||||||
|
Log.e(tag, "Attempting to start video capture")
|
||||||
|
val videoCapture = this.videoCapture ?: run {
|
||||||
|
Log.e(tag, "VideoCapture not initialized")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
val curRecording = recording
|
val curRecording = recording
|
||||||
if (curRecording != null) {
|
if (curRecording != null) {
|
||||||
|
@ -192,22 +234,22 @@ class RecordingService : LifecycleService() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
companion object {
|
|
||||||
const val ACTION_START_RECORDING = "com.urkob.wittrail_android.action.START_RECORDING"
|
|
||||||
const val ACTION_STOP_RECORDING = "com.urkob.wittrail_android.action.STOP_RECORDING"
|
|
||||||
private const val CHANNEL_ID = "ForegroundServiceChannel"
|
|
||||||
private const val NOTIFICATION_ID = 1
|
|
||||||
}
|
|
||||||
private fun stopRecording() {
|
|
||||||
Log.e(tag, "stopRecording")
|
|
||||||
|
|
||||||
|
private fun stopRecording() {
|
||||||
|
Log.e(tag, "Attempting to stop recording")
|
||||||
val curRecording = recording
|
val curRecording = recording
|
||||||
if (curRecording != null) {
|
if (curRecording != null) {
|
||||||
Log.e(tag, "stopRecording curRecording != null Stop the current recording session")
|
|
||||||
curRecording.stop()
|
curRecording.stop()
|
||||||
recording = null
|
recording = null
|
||||||
return
|
Log.e(tag, "Recording stopped. Now stopping service.")
|
||||||
}
|
stopSelf() // Add this line to stop the service.
|
||||||
|
} else {
|
||||||
|
Log.e(tag, "No active recording to stop")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
interface CameraInitializedCallback {
|
||||||
|
fun onCameraInitialized()
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue