clean code and show/hide recording buttons
This commit is contained in:
parent
19b7d4c019
commit
5dee3a20bb
|
@ -6,6 +6,7 @@ import android.content.pm.PackageManager
|
|||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import android.view.View
|
||||
import android.widget.Button
|
||||
import android.widget.Toast
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
|
@ -94,6 +95,7 @@ class MainActivity : AppCompatActivity() {
|
|||
private fun initStartCamera(){
|
||||
startFrontCameraButton.setOnClickListener {
|
||||
useFrontCamera = true
|
||||
manageRecordingButtonsVisibility(startRecording = true)
|
||||
if (hasCameraPermission()) {
|
||||
startRecordingService(useFrontCamera)
|
||||
} else {
|
||||
|
@ -103,6 +105,7 @@ class MainActivity : AppCompatActivity() {
|
|||
|
||||
startBackCameraButton.setOnClickListener {
|
||||
useFrontCamera = false
|
||||
manageRecordingButtonsVisibility(startRecording = true)
|
||||
if (hasCameraPermission()) {
|
||||
startRecordingService(useFrontCamera)
|
||||
} else {
|
||||
|
@ -111,6 +114,7 @@ class MainActivity : AppCompatActivity() {
|
|||
}
|
||||
|
||||
stopCameraButton.setOnClickListener {
|
||||
manageRecordingButtonsVisibility(startRecording = false)
|
||||
stopRecordingService()
|
||||
}
|
||||
}
|
||||
|
@ -127,11 +131,20 @@ class MainActivity : AppCompatActivity() {
|
|||
|
||||
private fun stopRecordingService() {
|
||||
Log.e("main", "stopRecordingService")
|
||||
val serviceIntent = Intent(this, RecordingService::class.java).apply {
|
||||
action = RecordingService.ACTION_STOP_RECORDING
|
||||
}
|
||||
// startService(serviceIntent) // Note: Using startService to send the stop action
|
||||
val serviceIntent = Intent(this, RecordingService::class.java)
|
||||
stopService(serviceIntent)
|
||||
}
|
||||
|
||||
private fun manageRecordingButtonsVisibility(startRecording: Boolean) {
|
||||
if (startRecording) {
|
||||
startFrontCameraButton.visibility = View.GONE
|
||||
startBackCameraButton.visibility = View.GONE
|
||||
stopCameraButton.visibility = View.VISIBLE
|
||||
} else {
|
||||
startFrontCameraButton.visibility = View.VISIBLE
|
||||
startBackCameraButton.visibility = View.VISIBLE
|
||||
stopCameraButton.visibility = View.GONE
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -115,7 +115,6 @@ class RecordingService : LifecycleService() {
|
|||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||
useFrontCamera = intent?.getBooleanExtra("useFrontCamera", false) ?: false
|
||||
when (intent?.action) {
|
||||
|
||||
ACTION_START_RECORDING -> {
|
||||
if (isCameraInitialized) {
|
||||
captureVideo()
|
||||
|
@ -127,25 +126,18 @@ class RecordingService : LifecycleService() {
|
|||
})
|
||||
}
|
||||
}
|
||||
ACTION_STOP_RECORDING -> {
|
||||
Log.e(tag, ACTION_STOP_RECORDING)
|
||||
stopRecording()
|
||||
// return START_NOT_STICKY
|
||||
}
|
||||
}
|
||||
// return START_STICKY
|
||||
return super.onStartCommand(intent, flags, startId)
|
||||
}
|
||||
|
||||
// override fun onDestroy() {
|
||||
// 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()
|
||||
// }
|
||||
override fun onDestroy() {
|
||||
Log.e(tag, "ON DESTROY IS CALLED SHOUlD STOP CAMERA")
|
||||
// Release resources
|
||||
stopRecording()
|
||||
cameraExecutor.shutdown()
|
||||
Log.e(tag, "Resources released")
|
||||
super.onDestroy()
|
||||
}
|
||||
|
||||
private fun startCamera() {
|
||||
Log.e(tag, "startCamera")
|
||||
|
@ -242,12 +234,11 @@ class RecordingService : LifecycleService() {
|
|||
curRecording.stop()
|
||||
recording = null
|
||||
Log.e(tag, "Recording stopped. Now stopping service.")
|
||||
stopSelf() // Add this line to stop the service.
|
||||
stopSelf() // This will stop the service.
|
||||
} else {
|
||||
Log.e(tag, "No active recording to stop")
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
interface CameraInitializedCallback {
|
||||
|
|
Loading…
Reference in New Issue