From 486309ae9a3e59d9000b240ede96d267732ebbd1 Mon Sep 17 00:00:00 2001 From: cilantrofe Date: Fri, 31 Jan 2025 21:00:40 +0300 Subject: [PATCH] Refactoring in writeByteAudioDataToStorage --- .../squti/androidwaverecorder/WaveRecorder.kt | 77 +++++++++++-------- 1 file changed, 44 insertions(+), 33 deletions(-) diff --git a/android-wave-recorder/src/main/java/com/github/squti/androidwaverecorder/WaveRecorder.kt b/android-wave-recorder/src/main/java/com/github/squti/androidwaverecorder/WaveRecorder.kt index 49dd2ad..d77ee93 100644 --- a/android-wave-recorder/src/main/java/com/github/squti/androidwaverecorder/WaveRecorder.kt +++ b/android-wave-recorder/src/main/java/com/github/squti/androidwaverecorder/WaveRecorder.kt @@ -184,12 +184,7 @@ class WaveRecorder { private suspend fun writeByteAudioDataToStorage() { val bufferSize = calculateMinBufferSize(waveConfig) val data = ByteArray(bufferSize) - val outputStream: OutputStream = if (fileUri != null) { - context.contentResolver.openOutputStream(fileUri ?: return) ?: return - } else { - val file = File(filePath!!) - FileOutputStream(file) - } + val outputStream = getOutputStream() ?: return val dataOutputStream = DataOutputStream(outputStream) val fileWriter = FileWriter(dataOutputStream) @@ -198,42 +193,58 @@ class WaveRecorder { val lastSkippedData = LinkedList() - var fileDurationInMillis = BigDecimal.ZERO - while (audioRecorder.recordingState == AudioRecord.RECORDSTATE_RECORDING) { - val operationStatus = audioRecorder.read(data, 0, bufferSize) + processAudioData(data, bufferSize, fileWriter, lastSkippedData, bufferSizeToKeep) + } + updateState(RecorderState.STOP) + cleanup(dataOutputStream) + } - if (operationStatus != AudioRecord.ERROR_INVALID_OPERATION) { - val amplitude = getByteArrayAmplitude(data) + private fun getOutputStream(): OutputStream? { + return if (fileUri != null) { + context.contentResolver.openOutputStream(fileUri!!) + } else { + val file = File(filePath!!) + FileOutputStream(file) + } + } - if (isPaused.get()) - updateState(RecorderState.PAUSE) - else { + private suspend fun processAudioData( + data: ByteArray, + bufferSize: Int, + fileWriter: FileWriter, + lastSkippedData: LinkedList, + bufferSizeToKeep: Int, + ) { + val operationStatus = audioRecorder.read(data, 0, bufferSize) + var fileDurationInMillis = BigDecimal.ZERO - if (silenceDetection) { - handleByteSilenceState( - amplitude, - lastSkippedData, - data, - bufferSizeToKeep - ) - } + if (operationStatus != AudioRecord.ERROR_INVALID_OPERATION) { + val amplitude = getByteArrayAmplitude(data) - if (!isSkipping.get()) { - updateState(RecorderState.RECORDING) - lastSkippedData.forEach { - fileDurationInMillis += calculateDurationInMillis(it, waveConfig) - } - fileWriter.writeDataToStream(lastSkippedData, data) - fileDurationInMillis += calculateDurationInMillis(data, waveConfig) - } + if (isPaused.get()) { + updateState(RecorderState.PAUSE) + } else { + if (silenceDetection) { + handleByteSilenceState(amplitude, lastSkippedData, data, bufferSizeToKeep) } - updateListeners(amplitude, fileDurationInMillis.toLong()) + if (!isSkipping.get()) { + updateState(RecorderState.RECORDING) + lastSkippedData.forEach { + fileDurationInMillis += calculateDurationInMillis(it, waveConfig) + } + fileWriter.writeDataToStream(lastSkippedData, data) + fileDurationInMillis += calculateDurationInMillis(data, waveConfig) + } } + + updateListeners(amplitude, fileDurationInMillis.toLong()) } - updateState(RecorderState.STOP) - cleanup(dataOutputStream) + } + + private fun cleanup(dataOutputStream: DataOutputStream) { + dataOutputStream.close() } @RequiresApi(Build.VERSION_CODES.M)