fix: Delete old recordings and create new ones based on intervalDuration

This commit is contained in:
Myzel394 2023-08-05 23:18:25 +02:00
parent 2d228010ab
commit 5620ff9625
No known key found for this signature in database
GPG Key ID: 79CC92F37B3E1A2B

View File

@ -55,12 +55,24 @@ class RecorderService: Service() {
private set private set
val isRecording = mutableStateOf(false) val isRecording = mutableStateOf(false)
val filePaths = mutableListOf<String>()
val amplitudes = mutableStateListOf<Int>() val amplitudes = mutableStateListOf<Int>()
var recordingStart: LocalDateTime? = null var recordingStart: LocalDateTime? = null
private set private set
val filePaths: List<File>
get() = File(fileFolder!!).listFiles()?.filter {
val name = it.nameWithoutExtension
if (name.toIntOrNull() == null) {
return@filter false
}
val extension = it.extension
extension == settings.fileExtension
}?.toList() ?: emptyList()
override fun onBind(p0: Intent?): IBinder = binder override fun onBind(p0: Intent?): IBinder = binder
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
@ -92,17 +104,16 @@ class RecorderService: Service() {
amplitudes.clear() amplitudes.clear()
isRecording.value = false isRecording.value = false
filePaths.forEach { File(fileFolder!!).listFiles()?.forEach {
File(it).delete() it.delete()
} }
filePaths.clear()
} }
fun concatenateFiles(forceConcatenation: Boolean = false): File { fun concatenateFiles(forceConcatenation: Boolean = false): File {
val paths = filePaths.joinToString("|") val paths = filePaths.joinToString("|")
println("Concatenating files: $paths")
val fileName = recordingStart!! val fileName = recordingStart!!
.format(DateTimeFormatter.ISO_DATE_TIME) .format(ISO_DATE_TIME)
.toString() .toString()
.replace(":", "-") .replace(":", "-")
.replace(".", "_") .replace(".", "_")
@ -132,7 +143,7 @@ class RecorderService: Service() {
session.getReturnCode(), session.getReturnCode(),
session.getFailStackTrace() session.getFailStackTrace()
) )
); )
throw Exception("Failed to concatenate audios") throw Exception("Failed to concatenate audios")
} }
@ -159,7 +170,7 @@ class RecorderService: Service() {
deleteOldRecordings() deleteOldRecordings()
val newRecorder = createRecorder(); val newRecorder = createRecorder()
newRecorder.prepare() newRecorder.prepare()
@ -174,6 +185,8 @@ class RecorderService: Service() {
mediaRecorder = newRecorder mediaRecorder = newRecorder
counter++ counter++
handler.postDelayed(this::startNewRecording, settings.intervalDuration)
} }
private fun deleteOldRecordings() { private fun deleteOldRecordings() {
@ -190,8 +203,6 @@ class RecorderService: Service() {
} }
private fun createRecorder(): MediaRecorder { private fun createRecorder(): MediaRecorder {
filePaths.add(getFilePath())
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
MediaRecorder(this) MediaRecorder(this)
} else { } else {
@ -265,9 +276,6 @@ class RecorderService: Service() {
// show notification // show notification
startForeground(getNotificationId(), notification) startForeground(getNotificationId(), notification)
// call function 1 sec later
handler.postDelayed(this::showNotification, 1000L)
} }
// To avoid int overflow, we'll use the number of seconds since 2023-01-01 01:01:01 // To avoid int overflow, we'll use the number of seconds since 2023-01-01 01:01:01