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.Build
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
|
import android.view.View
|
||||||
import android.widget.Button
|
import android.widget.Button
|
||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
import androidx.activity.result.contract.ActivityResultContracts
|
import androidx.activity.result.contract.ActivityResultContracts
|
||||||
|
@ -94,6 +95,7 @@ class MainActivity : AppCompatActivity() {
|
||||||
private fun initStartCamera(){
|
private fun initStartCamera(){
|
||||||
startFrontCameraButton.setOnClickListener {
|
startFrontCameraButton.setOnClickListener {
|
||||||
useFrontCamera = true
|
useFrontCamera = true
|
||||||
|
manageRecordingButtonsVisibility(startRecording = true)
|
||||||
if (hasCameraPermission()) {
|
if (hasCameraPermission()) {
|
||||||
startRecordingService(useFrontCamera)
|
startRecordingService(useFrontCamera)
|
||||||
} else {
|
} else {
|
||||||
|
@ -103,6 +105,7 @@ class MainActivity : AppCompatActivity() {
|
||||||
|
|
||||||
startBackCameraButton.setOnClickListener {
|
startBackCameraButton.setOnClickListener {
|
||||||
useFrontCamera = false
|
useFrontCamera = false
|
||||||
|
manageRecordingButtonsVisibility(startRecording = true)
|
||||||
if (hasCameraPermission()) {
|
if (hasCameraPermission()) {
|
||||||
startRecordingService(useFrontCamera)
|
startRecordingService(useFrontCamera)
|
||||||
} else {
|
} else {
|
||||||
|
@ -111,6 +114,7 @@ class MainActivity : AppCompatActivity() {
|
||||||
}
|
}
|
||||||
|
|
||||||
stopCameraButton.setOnClickListener {
|
stopCameraButton.setOnClickListener {
|
||||||
|
manageRecordingButtonsVisibility(startRecording = false)
|
||||||
stopRecordingService()
|
stopRecordingService()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -127,11 +131,20 @@ class MainActivity : AppCompatActivity() {
|
||||||
|
|
||||||
private fun stopRecordingService() {
|
private fun stopRecordingService() {
|
||||||
Log.e("main", "stopRecordingService")
|
Log.e("main", "stopRecordingService")
|
||||||
val serviceIntent = Intent(this, RecordingService::class.java).apply {
|
val serviceIntent = Intent(this, RecordingService::class.java)
|
||||||
action = RecordingService.ACTION_STOP_RECORDING
|
|
||||||
}
|
|
||||||
// startService(serviceIntent) // Note: Using startService to send the stop action
|
|
||||||
stopService(serviceIntent)
|
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 {
|
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 -> {
|
||||||
if (isCameraInitialized) {
|
if (isCameraInitialized) {
|
||||||
captureVideo()
|
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)
|
return super.onStartCommand(intent, flags, startId)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
// Release resources
|
||||||
// stopRecording()
|
stopRecording()
|
||||||
// cameraExecutor.shutdown()
|
cameraExecutor.shutdown()
|
||||||
// // Consider releasing cameraProvider here if needed
|
Log.e(tag, "Resources released")
|
||||||
// Log.e(tag, "Resources released")
|
super.onDestroy()
|
||||||
// super.onDestroy()
|
}
|
||||||
// }
|
|
||||||
|
|
||||||
private fun startCamera() {
|
private fun startCamera() {
|
||||||
Log.e(tag, "startCamera")
|
Log.e(tag, "startCamera")
|
||||||
|
@ -242,12 +234,11 @@ class RecordingService : LifecycleService() {
|
||||||
curRecording.stop()
|
curRecording.stop()
|
||||||
recording = null
|
recording = null
|
||||||
Log.e(tag, "Recording stopped. Now stopping service.")
|
Log.e(tag, "Recording stopped. Now stopping service.")
|
||||||
stopSelf() // Add this line to stop the service.
|
stopSelf() // This will stop the service.
|
||||||
} else {
|
} else {
|
||||||
Log.e(tag, "No active recording to stop")
|
Log.e(tag, "No active recording to stop")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
interface CameraInitializedCallback {
|
interface CameraInitializedCallback {
|
||||||
|
|
Loading…
Reference in New Issue