From f1afd1d13ac8f9fd63401e29a5eeab046b02bfb1 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Tue, 27 Dec 2022 19:14:25 +0100 Subject: [PATCH] Fix issues with the video duration using SponsorBlock --- .../ui/activities/OfflinePlayerActivity.kt | 14 +++++++++- .../libretube/ui/fragments/PlayerFragment.kt | 28 +++++++++---------- .../layout/exo_styled_player_control_view.xml | 7 +++-- 3 files changed, 30 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/com/github/libretube/ui/activities/OfflinePlayerActivity.kt b/app/src/main/java/com/github/libretube/ui/activities/OfflinePlayerActivity.kt index c19cbe9b1..102ccead5 100644 --- a/app/src/main/java/com/github/libretube/ui/activities/OfflinePlayerActivity.kt +++ b/app/src/main/java/com/github/libretube/ui/activities/OfflinePlayerActivity.kt @@ -7,6 +7,7 @@ import android.media.session.PlaybackState import android.net.Uri import android.os.Build import android.os.Bundle +import android.text.format.DateUtils import android.view.View import android.view.WindowManager import androidx.activity.viewModels @@ -23,6 +24,7 @@ import com.github.libretube.util.DownloadHelper import com.github.libretube.util.PlayerHelper import com.google.android.exoplayer2.ExoPlayer import com.google.android.exoplayer2.MediaItem +import com.google.android.exoplayer2.Player import com.google.android.exoplayer2.source.MergingMediaSource import com.google.android.exoplayer2.source.ProgressiveMediaSource import com.google.android.exoplayer2.ui.StyledPlayerView @@ -58,7 +60,17 @@ class OfflinePlayerActivity : BaseActivity() { private fun initializePlayer() { player = ExoPlayer.Builder(this) .setHandleAudioBecomingNoisy(true) - .build() + .build().apply { + addListener(object : Player.Listener { + override fun onEvents(player: Player, events: Player.Events) { + super.onEvents(player, events) + // update the displayed duration on changes + playerBinding.duration.text = DateUtils.formatElapsedTime( + player.duration / 1000 + ) + } + }) + } playerView = binding.player diff --git a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt index fc1a1433b..f1aeffe05 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt @@ -859,6 +859,7 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions { } override fun onEvents(player: Player, events: Player.Events) { + updateDisplayedDuration() super.onEvents(player, events) if (events.containsAny( Player.EVENT_PLAYBACK_STATE_CHANGED, @@ -871,8 +872,6 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions { } override fun onPlaybackStateChanged(playbackState: Int) { - updateDisplayedDuration() - exoPlayerView.keepScreenOn = !( playbackState == Player.STATE_IDLE || playbackState == Player.STATE_ENDED @@ -992,22 +991,21 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions { */ @SuppressLint("SetTextI18n") private fun updateDisplayedDuration() { - if (exoPlayer.duration == Long.MAX_VALUE || isLive) return - - val durationWithSponsorBlock = if ( - !this::segmentData.isInitialized || this.segmentData.segments.isEmpty() - ) { - null - } else { - val difference = segmentData.segments.sumOf { - it.segment[1] - it.segment[0] - }.toInt() - DateUtils.formatElapsedTime(exoPlayer.duration.div(1000) - difference) - } + if (exoPlayer.duration < 0 || isLive) return playerBinding.duration.text = DateUtils.formatElapsedTime( exoPlayer.duration.div(1000) - ) + if (durationWithSponsorBlock != null) " ($durationWithSponsorBlock)" else "" + ) + if (!this::segmentData.isInitialized || this.segmentData.segments.isEmpty()) { + return + } + + val durationWithSb = DateUtils.formatElapsedTime( + exoPlayer.duration.div(1000) - segmentData.segments.sumOf { + it.segment[1] - it.segment[0] + }.toInt() + ) + playerBinding.duration.text = playerBinding.duration.text.toString() + " ($durationWithSb)" } private fun syncQueueButtons() { diff --git a/app/src/main/res/layout/exo_styled_player_control_view.xml b/app/src/main/res/layout/exo_styled_player_control_view.xml index 87ada7aec..35281c044 100644 --- a/app/src/main/res/layout/exo_styled_player_control_view.xml +++ b/app/src/main/res/layout/exo_styled_player_control_view.xml @@ -227,8 +227,8 @@ + android:visibility="gone" + tools:text="05:10" /> + android:text="00:00" + tools:ignore="HardcodedText" />