From f59e979aab63200caa863b392465a5d9956047a5 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Wed, 31 Dec 2025 09:43:25 +0100 Subject: [PATCH] Create VoicePlayerConfig. --- .../voiceplayer/impl/VoiceMessagePresenter.kt | 14 +++++--------- .../voiceplayer/impl/VoicePlayerConfig.kt | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 9 deletions(-) create mode 100644 libraries/voiceplayer/impl/src/main/kotlin/io/element/android/libraries/voiceplayer/impl/VoicePlayerConfig.kt diff --git a/libraries/voiceplayer/impl/src/main/kotlin/io/element/android/libraries/voiceplayer/impl/VoiceMessagePresenter.kt b/libraries/voiceplayer/impl/src/main/kotlin/io/element/android/libraries/voiceplayer/impl/VoiceMessagePresenter.kt index 089e7fa533..95c9554731 100644 --- a/libraries/voiceplayer/impl/src/main/kotlin/io/element/android/libraries/voiceplayer/impl/VoiceMessagePresenter.kt +++ b/libraries/voiceplayer/impl/src/main/kotlin/io/element/android/libraries/voiceplayer/impl/VoiceMessagePresenter.kt @@ -12,6 +12,7 @@ import androidx.compose.runtime.Composable import androidx.compose.runtime.collectAsState import androidx.compose.runtime.derivedStateOf import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableIntStateOf import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import io.element.android.libraries.architecture.AsyncData @@ -38,9 +39,7 @@ class VoiceMessagePresenter( private val duration: Duration, ) : Presenter { private val play = mutableStateOf>(AsyncData.Uninitialized) - private val playbackSpeed = mutableStateOf(1.0f) - - private val availablePlaybackSpeeds = listOf(0.5f, 1.0f, 1.5f, 2.0f) + private val playbackSpeedIndex = mutableIntStateOf(0) @Composable override fun present(): VoiceMessageState { @@ -116,11 +115,8 @@ class VoiceMessagePresenter( player.seekTo((event.percentage * duration).toLong()) } is VoiceMessageEvents.ChangePlaybackSpeed -> { - val currentIndex = availablePlaybackSpeeds.indexOf(playbackSpeed.value) - val nextIndex = (currentIndex + 1) % availablePlaybackSpeeds.size - val newSpeed = availablePlaybackSpeeds[nextIndex] - playbackSpeed.value = newSpeed - player.setPlaybackSpeed(newSpeed) + playbackSpeedIndex.intValue = (playbackSpeedIndex.intValue + 1) % VoicePlayerConfig.availablePlaybackSpeeds.size + player.setPlaybackSpeed(VoicePlayerConfig.availablePlaybackSpeeds[playbackSpeedIndex.intValue]) } } } @@ -130,7 +126,7 @@ class VoiceMessagePresenter( progress = progress, time = time, showCursor = showCursor, - playbackSpeed = playbackSpeed.value, + playbackSpeed = VoicePlayerConfig.availablePlaybackSpeeds[playbackSpeedIndex.intValue], eventSink = ::handleEvent, ) } diff --git a/libraries/voiceplayer/impl/src/main/kotlin/io/element/android/libraries/voiceplayer/impl/VoicePlayerConfig.kt b/libraries/voiceplayer/impl/src/main/kotlin/io/element/android/libraries/voiceplayer/impl/VoicePlayerConfig.kt new file mode 100644 index 0000000000..3ade34d99f --- /dev/null +++ b/libraries/voiceplayer/impl/src/main/kotlin/io/element/android/libraries/voiceplayer/impl/VoicePlayerConfig.kt @@ -0,0 +1,14 @@ +/* + * Copyright (c) 2025 Element Creations Ltd. + * + * SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial. + * Please see LICENSE files in the repository root for full details. + */ + +package io.element.android.libraries.voiceplayer.impl + +object VoicePlayerConfig { + // Available playback speeds for voice messages, the first one is the default speed, and + // the UI will allow to change to the next speed in the list, in loop. + val availablePlaybackSpeeds = listOf(1.0f, 1.5f, 2.0f, 0.5f) +}