From 206b9baacaa7e49b0665e3b137aea1667098bb9b Mon Sep 17 00:00:00 2001 From: inotia00 <108592928+inotia00@users.noreply.github.com> Date: Sun, 3 Dec 2023 10:13:33 +0900 Subject: [PATCH] fix(YouTube/SponsorBlock): some videos refused to end where there's a skippable segment at the end --- .../integrations/patches/video/VideoInformation.java | 9 +-------- .../sponsorblock/objects/SponsorSegment.java | 8 +++++++- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/app/revanced/integrations/patches/video/VideoInformation.java b/app/src/main/java/app/revanced/integrations/patches/video/VideoInformation.java index 42f8dfc892..eda67662d0 100644 --- a/app/src/main/java/app/revanced/integrations/patches/video/VideoInformation.java +++ b/app/src/main/java/app/revanced/integrations/patches/video/VideoInformation.java @@ -62,17 +62,10 @@ public static void initialize(@NonNull Object playerController) { * @return if the seek was successful */ public static boolean seekTo(long millisecond) { - final long videoLength = getVideoLength(); - - // Don't seek more than the video length to prevent issues such as - // Play pause button or autoplay not working. - // TODO: These are arbitrarily chosen values and should be subject to be adjusted. - final long seekToMilliseconds = millisecond > videoLength ? Integer.MAX_VALUE : millisecond; - ReVancedUtils.verifyOnMainThread(); try { //noinspection DataFlowIssue - return (Boolean) seekMethod.invoke(playerControllerRef.get(), seekToMilliseconds); + return (Boolean) seekMethod.invoke(playerControllerRef.get(), millisecond); } catch (Exception ex) { LogHelper.printException(() -> "Failed to seek", ex); } diff --git a/app/src/main/java/app/revanced/integrations/sponsorblock/objects/SponsorSegment.java b/app/src/main/java/app/revanced/integrations/sponsorblock/objects/SponsorSegment.java index 7502fbca70..a4d507f652 100644 --- a/app/src/main/java/app/revanced/integrations/sponsorblock/objects/SponsorSegment.java +++ b/app/src/main/java/app/revanced/integrations/sponsorblock/objects/SponsorSegment.java @@ -27,11 +27,17 @@ public class SponsorSegment implements Comparable { */ public boolean recordedAsSkipped = false; + /** + * The end of the segment may be longer than the length of the video. + * In this case, the autoplay may break or fall into an infinite loop. + * Checking the length every time in {@link VideoInformation#seekTo} is too inefficient, + * So check it only once when the segment is fetched. + */ public SponsorSegment(@NonNull SegmentCategory category, @Nullable String UUID, long start, long end, boolean isLocked) { this.category = category; this.UUID = UUID; this.start = start; - this.end = end; + this.end = Math.min(end, VideoInformation.getVideoLength() - 250); this.isLocked = isLocked; }