From f74c7fa997dc246d7f8dec873f01c2934cec7433 Mon Sep 17 00:00:00 2001 From: Florian Renaud Date: Tue, 31 Jan 2023 10:14:28 +0100 Subject: [PATCH] Handle the potential offset on the chunk position used by the media player --- .../listening/VoiceBroadcastPlayerImpl.kt | 18 +++++++++++------- .../listening/VoiceBroadcastPlaylist.kt | 4 ++-- 2 files changed, 13 insertions(+), 9 deletions(-) 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 61e4bf640c..bc07e174d3 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 @@ -223,28 +223,32 @@ class VoiceBroadcastPlayerImpl @Inject constructor( } } - private fun startPlayback(position: Int) { + private fun startPlayback(playbackPosition: Int) { stopPlayer() playingState = State.Buffering - val playlistItem = playlist.findByPosition(position) ?: run { - Timber.w("## Voice Broadcast | No content to play at position $position"); stop(); return + val playlistItem = playlist.findByPosition(playbackPosition) ?: run { + Timber.w("## Voice Broadcast | No content to play at position $playbackPosition"); stop(); return } val sequence = playlistItem.sequence ?: run { Timber.w("## Voice Broadcast | Playlist item has no sequence"); stop(); return } - val sequencePosition = position - playlistItem.startTime currentVoiceBroadcast?.let { - val percentage = tryOrNull { position.toFloat() / playlist.duration } ?: 0f - playbackTracker.updatePausedAtPlaybackTime(it.voiceBroadcastId, position, percentage) + val percentage = tryOrNull { playbackPosition.toFloat() / playlist.duration } ?: 0f + playbackTracker.updatePausedAtPlaybackTime(it.voiceBroadcastId, playbackPosition, percentage) } prepareCurrentPlayerJob = sessionScope.launch { try { val mp = prepareMediaPlayer(playlistItem.audioEvent.content) - playlist.currentSequence = sequence - 1 // will be incremented in onNextMediaPlayerStarted + // Take the difference between the duration given from the media player and the duration given from the chunk event + // If the offset is smaller than 500ms, we consider there is no offset to keep the normal behaviour + val offset = (mp.duration - playlistItem.duration).takeUnless { it < 500 }?.coerceAtLeast(0) ?: 0 + val sequencePosition = offset + (playbackPosition - playlistItem.startTime) + + playlist.currentSequence = sequence - 1 // will be incremented in onNextMediaPlayerStarted mp.start() if (sequencePosition > 0) { mp.seekTo(sequencePosition) diff --git a/vector/src/main/java/im/vector/app/features/voicebroadcast/listening/VoiceBroadcastPlaylist.kt b/vector/src/main/java/im/vector/app/features/voicebroadcast/listening/VoiceBroadcastPlaylist.kt index 36b737f23f..437b216d77 100644 --- a/vector/src/main/java/im/vector/app/features/voicebroadcast/listening/VoiceBroadcastPlaylist.kt +++ b/vector/src/main/java/im/vector/app/features/voicebroadcast/listening/VoiceBroadcastPlaylist.kt @@ -65,6 +65,6 @@ class VoiceBroadcastPlaylist( } data class PlaylistItem(val audioEvent: MessageAudioEvent, val startTime: Int) { - val sequence: Int? - get() = audioEvent.sequence + val sequence: Int? = audioEvent.sequence + val duration: Int = audioEvent.duration }