[merge] Catch voice recorder crash on SDK 21

Better broken voice recording, than chats broken completely

Change-Id: I768c75c4853badb692ca4ab1bff3ead18fb91e05
This commit is contained in:
SpiritCroc 2022-07-09 21:59:35 +02:00
parent a6953afc1e
commit 1fdc67b47d
1 changed files with 8 additions and 1 deletions

View File

@ -32,6 +32,7 @@ import kotlinx.coroutines.launch
import org.matrix.android.sdk.api.extensions.tryOrNull import org.matrix.android.sdk.api.extensions.tryOrNull
import org.matrix.android.sdk.api.session.content.ContentAttachmentData import org.matrix.android.sdk.api.session.content.ContentAttachmentData
import org.matrix.android.sdk.api.util.md5 import org.matrix.android.sdk.api.util.md5
import timber.log.Timber
import java.io.File import java.io.File
import java.util.UUID import java.util.UUID
import kotlin.coroutines.CoroutineContext import kotlin.coroutines.CoroutineContext
@ -58,13 +59,17 @@ class VoiceRecorderL(
private var audioRecorder: AudioRecord? = null private var audioRecorder: AudioRecord? = null
private var noiseSuppressor: NoiseSuppressor? = null private var noiseSuppressor: NoiseSuppressor? = null
private var automaticGainControl: AutomaticGainControl? = 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 // Size of the audio buffer for Short values
private var bufferSizeInShorts = 0 private var bufferSizeInShorts = 0
private var maxAmplitude = 0 private var maxAmplitude = 0
private fun initializeCodec(filePath: String) { private fun initializeCodec(filePath: String) {
codec ?: return
codec.init(filePath, SAMPLE_RATE) codec.init(filePath, SAMPLE_RATE)
codec.setBitrate(BITRATE) codec.setBitrate(BITRATE)
@ -90,6 +95,7 @@ class VoiceRecorderL(
} }
override fun startRecord(roomId: String) { override fun startRecord(roomId: String) {
codec ?: return
val fileName = "${UUID.randomUUID()}.ogg" val fileName = "${UUID.randomUUID()}.ogg"
val outputDirectoryForRoom = File(outputDirectory, roomId.md5()).apply { val outputDirectoryForRoom = File(outputDirectory, roomId.md5()).apply {
mkdirs() mkdirs()
@ -113,6 +119,7 @@ class VoiceRecorderL(
} }
override fun stopRecord() { override fun stopRecord() {
codec ?: return
val recorder = this.audioRecorder ?: return val recorder = this.audioRecorder ?: return
recordingJob?.cancel() recordingJob?.cancel()