fix: Only start recording if recordingstate is idle

This commit is contained in:
Myzel394 2023-12-16 23:09:10 +01:00
parent b1c77fe16a
commit 4f9f65d0b1
No known key found for this signature in database
GPG Key ID: 79CC92F37B3E1A2B
5 changed files with 31 additions and 17 deletions

View File

@ -29,7 +29,7 @@ abstract class RecorderService : LifecycleService() {
private lateinit var recordingTimeTimer: ScheduledExecutorService
private var notificationDetails: RecorderNotificationHelper.NotificationDetails? = null
var state = RecorderState.STOPPED
var state = RecorderState.IDLE
private set
var onStateChange: ((RecorderState) -> Unit)? = null

View File

@ -51,9 +51,11 @@ fun Navigation(
DisposableEffect(Unit) {
audioRecorder.bindToService(context)
videoRecorder.bindToService(context)
onDispose {
audioRecorder.unbindFromService(context)
videoRecorder.unbindFromService(context)
}
}

View File

@ -8,6 +8,7 @@ import androidx.compose.runtime.setValue
import androidx.documentfile.provider.DocumentFile
import app.myzel394.alibi.db.AppSettings
import app.myzel394.alibi.db.RecordingInformation
import app.myzel394.alibi.enums.RecorderState
import app.myzel394.alibi.helpers.AudioBatchesFolder
import app.myzel394.alibi.services.AudioRecorderService
import app.myzel394.alibi.ui.utils.MicrophoneInfo
@ -47,8 +48,13 @@ class AudioRecorderModel :
onAmplitudeChange()
}
service.clearAllRecordings()
service.startRecording()
// `onServiceConnected` may be called when reconnecting to the service,
// so we only want to actually start the recording if the service is idle and thus
// not already recording
if (service.state == RecorderState.IDLE) {
service.clearAllRecordings()
service.startRecording()
}
recorderState = service.state
recordingTime = service.recordingTime

View File

@ -105,6 +105,17 @@ abstract class BaseRecorderModel<I, T : IntervalRecorderService<I>, B : BatchesF
protected open fun handleIntent(intent: Intent) = intent
private fun stopOldServices(context: Context) {
runCatching {
context.unbindService(connection)
}
val intent = Intent(context, intentClass)
runCatching {
context.stopService(intent)
}
}
// If override, call `super` AFTER setting the settings
open fun startRecording(
context: Context,
@ -113,10 +124,7 @@ abstract class BaseRecorderModel<I, T : IntervalRecorderService<I>, B : BatchesF
this.settings = settings
// Clean up
runCatching {
recorderService?.clearAllRecordings()
context.unbindService(connection)
}
stopOldServices(context)
notificationDetails = settings.notificationSettings.let {
if (it == null)
@ -147,10 +155,6 @@ abstract class BaseRecorderModel<I, T : IntervalRecorderService<I>, B : BatchesF
suspend fun stopRecording(context: Context) {
recorderService!!.stopRecording()
runCatching {
context.unbindService(connection)
}
}
fun pauseRecording() {
@ -164,11 +168,8 @@ abstract class BaseRecorderModel<I, T : IntervalRecorderService<I>, B : BatchesF
fun destroyService(context: Context) {
recorderService!!.destroy()
reset()
val intent = Intent(context, intentClass)
runCatching {
context.stopService(intent)
}
stopOldServices(context)
}
// Bind functions used to manually bind to the service if the app

View File

@ -39,8 +39,13 @@ class VideoRecorderModel :
}
override fun onServiceConnected(service: VideoRecorderService) {
service.clearAllRecordings()
service.startRecording()
// `onServiceConnected` may be called when reconnecting to the service,
// so we only want to actually start the recording if the service is idle and thus
// not already recording
if (service.state == RecorderState.IDLE) {
service.clearAllRecordings()
service.startRecording()
}
recorderState = service.state
recordingTime = service.recordingTime