From b2f35fa1352f1b3771d8d3aa9231340560bebdab Mon Sep 17 00:00:00 2001 From: Florian Renaud Date: Fri, 4 Nov 2022 23:21:18 +0100 Subject: [PATCH] Improve PlaybackTicker --- .../helper/AudioMessagePlaybackTracker.kt | 2 +- .../listening/VoiceBroadcastPlayerImpl.kt | 51 ++++++++++--------- 2 files changed, 28 insertions(+), 25 deletions(-) diff --git a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/helper/AudioMessagePlaybackTracker.kt b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/helper/AudioMessagePlaybackTracker.kt index 6937cd3a46..7e40b92ac8 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/helper/AudioMessagePlaybackTracker.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/helper/AudioMessagePlaybackTracker.kt @@ -127,7 +127,7 @@ class AudioMessagePlaybackTracker @Inject constructor() { } } - private fun getPercentage(id: String): Float { + fun getPercentage(id: String): Float { return when (val state = states[id]) { is Listener.State.Playing -> state.percentage is Listener.State.Paused -> state.percentage diff --git a/vector/src/main/java/im/vector/app/features/voicebroadcast/listening/VoiceBroadcastPlayerImpl.kt b/vector/src/main/java/im/vector/app/features/voicebroadcast/listening/VoiceBroadcastPlayerImpl.kt index bfbc53fd78..50a3002c0d 100644 --- a/vector/src/main/java/im/vector/app/features/voicebroadcast/listening/VoiceBroadcastPlayerImpl.kt +++ b/vector/src/main/java/im/vector/app/features/voicebroadcast/listening/VoiceBroadcastPlayerImpl.kt @@ -340,34 +340,37 @@ class VoiceBroadcastPlayerImpl @Inject constructor( onPlaybackTick(id) } - private fun onPlaybackTick(id: String) { - if (currentMediaPlayer?.isPlaying.orFalse()) { - val itemStartPosition = playlist.currentItem?.startTime - val currentVoiceBroadcastPosition = itemStartPosition?.plus(currentMediaPlayer?.currentPosition ?: 0) - Timber.d("Voice Broadcast | VoiceBroadcastPlayerImpl - sequence: ${playlist.currentSequence}, itemStartPosition $itemStartPosition, currentMediaPlayer=$currentMediaPlayer, currentMediaPlayer?.currentPosition: ${currentMediaPlayer?.currentPosition}") - if (currentVoiceBroadcastPosition != null) { - val percentage = currentVoiceBroadcastPosition.toFloat() / playlist.duration - playbackTracker.updatePlayingAtPlaybackTime(id, currentVoiceBroadcastPosition, percentage) - } else { - stopPlaybackTicker(id) - } - } else { - stopPlaybackTicker(id) - } - } - fun stopPlaybackTicker(id: String) { playbackTicker?.stop() playbackTicker = null + onPlaybackTick(id) + } - val totalDuration = playlist.duration - if (totalDuration > 0) { - val playbackTime = playbackTracker.getPlaybackTime(id) - val remainingTime = totalDuration - playbackTime - if (remainingTime < 1000) { - playbackTracker.updatePausedAtPlaybackTime(id, 0, 0f) - } else { - playbackTracker.pausePlayback(id) + private fun onPlaybackTick(id: String) { + val currentItem = playlist.currentItem ?: return + val itemStartTime = currentItem.startTime + val duration = playlist.duration + when (playingState) { + State.PLAYING, + State.PAUSED -> { + Timber.d("Voice Broadcast | VoiceBroadcastPlayerImpl - sequence: ${playlist.currentSequence}, itemStartTime $itemStartTime, currentMediaPlayer=$currentMediaPlayer, currentMediaPlayer?.currentPosition: ${currentMediaPlayer?.currentPosition}") + val position = itemStartTime + (currentMediaPlayer?.currentPosition ?: 0) + val percentage = position.toFloat() / playlist.duration + if (playingState == State.PLAYING) { + playbackTracker.updatePlayingAtPlaybackTime(id, position, percentage) + } else { + playbackTracker.updatePausedAtPlaybackTime(id, position, percentage) + } + } + State.BUFFERING, + State.IDLE -> { + val playbackTime = playbackTracker.getPlaybackTime(id) + val percentage = playbackTracker.getPercentage(id) + if (playingState == State.IDLE && duration > 0 && (duration - playbackTime) < 1000) { + playbackTracker.updatePausedAtPlaybackTime(id, 0, 0f) + } else { + playbackTracker.updatePausedAtPlaybackTime(id, playbackTime, percentage) + } } } }