From e7d6ed66da85fd3cd72469f880f902e6006b4eda Mon Sep 17 00:00:00 2001 From: Stanislav Dmitrenko <7953703+avently@users.noreply.github.com> Date: Sat, 11 Nov 2023 00:09:19 +0800 Subject: [PATCH] android: fix crash when playing recorded voice message (#3325) * android: fix crash when playing recorded voice message * better --- .../chat/simplex/common/platform/RecAndPlay.android.kt | 8 +++++--- .../kotlin/chat/simplex/common/views/chat/ComposeView.kt | 6 +++++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/apps/multiplatform/common/src/androidMain/kotlin/chat/simplex/common/platform/RecAndPlay.android.kt b/apps/multiplatform/common/src/androidMain/kotlin/chat/simplex/common/platform/RecAndPlay.android.kt index b89719b2e..f99dea77c 100644 --- a/apps/multiplatform/common/src/androidMain/kotlin/chat/simplex/common/platform/RecAndPlay.android.kt +++ b/apps/multiplatform/common/src/androidMain/kotlin/chat/simplex/common/platform/RecAndPlay.android.kt @@ -284,9 +284,11 @@ actual object AudioPlayer: AudioPlayerInterface { kotlin.runCatching { helperPlayer.setDataSource(unencryptedFilePath) helperPlayer.prepare() - helperPlayer.start() - helperPlayer.stop() - res = helperPlayer.duration + if (helperPlayer.duration <= 0) { + Log.e(TAG, "Duration of audio is incorrect: ${helperPlayer.duration}") + } else { + res = helperPlayer.duration + } helperPlayer.reset() } return res diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/ComposeView.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/ComposeView.kt index 59e43557e..959ded42b 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/ComposeView.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/ComposeView.kt @@ -776,7 +776,11 @@ fun ComposeView( .collect { when(it) { is RecordingState.Started -> onAudioAdded(it.filePath, it.progressMs, false) - is RecordingState.Finished -> onAudioAdded(it.filePath, it.durationMs, true) + is RecordingState.Finished -> if (it.durationMs > 300) { + onAudioAdded(it.filePath, it.durationMs, true) + } else { + cancelVoice() + } is RecordingState.NotStarted -> {} } }