From e4627fd156b1cea95377da3ece8b07ccfb03947c Mon Sep 17 00:00:00 2001 From: Bnyro Date: Sun, 12 Feb 2023 13:27:35 +0100 Subject: [PATCH] Fix SponsorBlock segment loop at end of video --- .../com/github/libretube/helpers/PlayerHelper.kt | 13 +++++++++++-- .../com/github/libretube/services/BackgroundMode.kt | 1 + .../github/libretube/ui/fragments/PlayerFragment.kt | 8 ++++---- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt b/app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt index 0022cca5f..cda4d4cef 100644 --- a/app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt +++ b/app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt @@ -26,6 +26,7 @@ import com.google.android.exoplayer2.PlaybackParameters import com.google.android.exoplayer2.audio.AudioAttributes import com.google.android.exoplayer2.ui.CaptionStyleCompat import com.google.android.exoplayer2.video.VideoSize +import kotlin.math.absoluteValue import kotlin.math.roundToInt object PlayerHelper { @@ -485,10 +486,18 @@ object PlayerHelper { * @param skipManually Whether the event gets handled by the function caller * @return If segment found and [skipManually] is true, the end position of the segment in ms, otherwise null */ - fun ExoPlayer.checkForSegments(context: Context, segments: List, skipManually: Boolean = false): Long? { - segments.forEach { segment -> + fun ExoPlayer.checkForSegments( + context: Context, + segments: List, + skipManually: Boolean = false + ): Long? { + for (segment in segments) { val segmentStart = (segment.segment[0] * 1000f).toLong() val segmentEnd = (segment.segment[1] * 1000f).toLong() + + // avoid seeking to the same segment multiple times, e.g. when the SB segment is at the end of the video + if ((duration - currentPosition).absoluteValue < 500) continue + if (currentPosition in segmentStart until segmentEnd) { if (!skipManually) { if (sponsorBlockNotifications) { diff --git a/app/src/main/java/com/github/libretube/services/BackgroundMode.kt b/app/src/main/java/com/github/libretube/services/BackgroundMode.kt index 690a74ba0..2a44f3e6e 100644 --- a/app/src/main/java/com/github/libretube/services/BackgroundMode.kt +++ b/app/src/main/java/com/github/libretube/services/BackgroundMode.kt @@ -112,6 +112,7 @@ class BackgroundMode : LifecycleService() { val notification: Notification = Notification.Builder(this, BACKGROUND_CHANNEL_ID) .setContentTitle(getString(R.string.app_name)) .setContentText(getString(R.string.playingOnBackground)) + .setSmallIcon(R.drawable.ic_launcher_lockscreen) .build() startForeground(PLAYER_NOTIFICATION_ID, notification) 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 73e8398b8..112f77948 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 @@ -114,6 +114,10 @@ import com.google.android.exoplayer2.ui.StyledPlayerView import com.google.android.exoplayer2.upstream.DefaultDataSource import com.google.android.exoplayer2.util.MimeTypes import com.google.android.material.dialog.MaterialAlertDialogBuilder +import java.io.IOException +import java.util.* +import java.util.concurrent.Executors +import kotlin.math.abs import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch @@ -121,10 +125,6 @@ import kotlinx.coroutines.withContext import kotlinx.datetime.LocalDate import kotlinx.serialization.encodeToString import retrofit2.HttpException -import java.io.IOException -import java.util.* -import java.util.concurrent.Executors -import kotlin.math.abs class PlayerFragment : BaseFragment(), OnlinePlayerOptions {