From 1fdc67b47d4abfb8218a7d0598d9da52b0f13f3f Mon Sep 17 00:00:00 2001 From: SpiritCroc Date: Sat, 9 Jul 2022 21:59:35 +0200 Subject: [PATCH] [merge] Catch voice recorder crash on SDK 21 Better broken voice recording, than chats broken completely Change-Id: I768c75c4853badb692ca4ab1bff3ead18fb91e05 --- .../java/im/vector/app/features/voice/VoiceRecorderL.kt | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/vector/src/main/java/im/vector/app/features/voice/VoiceRecorderL.kt b/vector/src/main/java/im/vector/app/features/voice/VoiceRecorderL.kt index f0eed41637..77bdbc46ec 100644 --- a/vector/src/main/java/im/vector/app/features/voice/VoiceRecorderL.kt +++ b/vector/src/main/java/im/vector/app/features/voice/VoiceRecorderL.kt @@ -32,6 +32,7 @@ import kotlinx.coroutines.launch import org.matrix.android.sdk.api.extensions.tryOrNull import org.matrix.android.sdk.api.session.content.ContentAttachmentData import org.matrix.android.sdk.api.util.md5 +import timber.log.Timber import java.io.File import java.util.UUID import kotlin.coroutines.CoroutineContext @@ -58,13 +59,17 @@ class VoiceRecorderL( private var audioRecorder: AudioRecord? = null private var noiseSuppressor: NoiseSuppressor? = null private var automaticGainControl: AutomaticGainControl? = null - private val codec = OggOpusEncoder() + private val codec = try { OggOpusEncoder() } catch (t: Throwable) { + Timber.e(t) + null + } // Size of the audio buffer for Short values private var bufferSizeInShorts = 0 private var maxAmplitude = 0 private fun initializeCodec(filePath: String) { + codec ?: return codec.init(filePath, SAMPLE_RATE) codec.setBitrate(BITRATE) @@ -90,6 +95,7 @@ class VoiceRecorderL( } override fun startRecord(roomId: String) { + codec ?: return val fileName = "${UUID.randomUUID()}.ogg" val outputDirectoryForRoom = File(outputDirectory, roomId.md5()).apply { mkdirs() @@ -113,6 +119,7 @@ class VoiceRecorderL( } override fun stopRecord() { + codec ?: return val recorder = this.audioRecorder ?: return recordingJob?.cancel()