Skip to content
This repository was archived by the owner on Oct 26, 2024. It is now read-only.

Commit 6aa0ca9

Browse files
committed
feat(video-information): hook video time
1 parent 98eaf9c commit 6aa0ca9

2 files changed

Lines changed: 45 additions & 28 deletions

File tree

app/src/main/java/app/revanced/integrations/patches/PlayerControllerPatch.java renamed to app/src/main/java/app/revanced/integrations/patches/VideoInformation.java

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,17 @@
99
import app.revanced.integrations.utils.LogHelper;
1010

1111
/**
12-
* Hooking class for the player controller.
12+
* Hooking class for the current playing video.
1313
*/
14-
public final class PlayerControllerPatch {
14+
public final class VideoInformation {
1515
private static final String SEEK_METHOD_NAME = "seekTo";
1616

1717
private static WeakReference<Object> playerController;
1818
private static Method seekMethod;
19+
1920
private static long videoLength = 1;
21+
private static long videoTime = -1;
22+
2023

2124
/**
2225
* Hook into PlayerController.onCreate() method.
@@ -26,26 +29,36 @@ public final class PlayerControllerPatch {
2629
public static void playerController_onCreateHook(final Object thisRef) {
2730
playerController = new WeakReference<>(thisRef);
2831
videoLength = 1;
32+
videoTime = -1;
2933

3034
try {
3135
seekMethod = thisRef.getClass().getMethod(SEEK_METHOD_NAME, Long.TYPE);
3236
seekMethod.setAccessible(true);
3337
} catch (NoSuchMethodException ex) {
34-
LogHelper.debug(PlayerControllerPatch.class, "Failed to initialize: " + ex.getMessage());
38+
LogHelper.debug(VideoInformation.class, "Failed to initialize: " + ex.getMessage());
3539
}
3640
}
3741

3842
/**
39-
* Set the current video length.
43+
* Set the video length.
4044
*
4145
* @param length The length of the video in milliseconds.
4246
*/
43-
public static void setCurrentVideoLength(final long length) {
44-
LogHelper.debug(PlayerControllerPatch.class, "Setting current video length to " + length);
45-
47+
public static void setVideoLength(final long length) {
48+
LogHelper.debug(VideoInformation.class, "Setting current video length to " + length);
4649
videoLength = length;
4750
}
4851

52+
/**
53+
* Set the video time.
54+
*
55+
* @param time The time of the video in milliseconds.
56+
*/
57+
public static void setVideoTime(final long time) {
58+
LogHelper.debug(VideoInformation.class, "Current video time " + time);
59+
videoTime = time;
60+
}
61+
4962
/**
5063
* Seek on the current video.
5164
*
@@ -54,25 +67,34 @@ public static void setCurrentVideoLength(final long length) {
5467
public static void seekTo(final long millisecond) {
5568
new Handler(Looper.getMainLooper()).post(() -> {
5669
if (seekMethod == null) {
57-
LogHelper.debug(PlayerControllerPatch.class, "seekMethod was null");
70+
LogHelper.debug(VideoInformation.class, "seekMethod was null");
5871
return;
5972
}
6073

6174
try {
62-
LogHelper.debug(PlayerControllerPatch.class, "Seeking to " + millisecond);
75+
LogHelper.debug(VideoInformation.class, "Seeking to " + millisecond);
6376
seekMethod.invoke(playerController.get(), millisecond);
6477
} catch (Exception ex) {
65-
LogHelper.debug(PlayerControllerPatch.class, "Failed to seek: " + ex.getMessage());
78+
LogHelper.debug(VideoInformation.class, "Failed to seek: " + ex.getMessage());
6679
}
6780
});
6881
}
6982

7083
/**
7184
* Get the length of the current video playing.
7285
*
73-
* @return The length of the video in milliseconds.
86+
* @return The length of the video in milliseconds. 1 if not set yet.
7487
*/
7588
public static long getCurrentVideoLength() {
76-
return videoLength;
89+
return videoLength;
90+
}
91+
92+
/**
93+
* Get the time of the current video playing.
94+
*
95+
* @return The time of the video in milliseconds. -1 if not set yet.
96+
*/
97+
public static long getVideoTime() {
98+
return videoTime;
7799
}
78100
}

app/src/main/java/app/revanced/integrations/sponsorblock/PlayerController.java

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import java.util.Timer;
2020
import java.util.TimerTask;
2121

22-
import app.revanced.integrations.patches.PlayerControllerPatch;
22+
import app.revanced.integrations.patches.VideoInformation;
2323
import app.revanced.integrations.settings.SettingsEnum;
2424
import app.revanced.integrations.sponsorblock.objects.SponsorSegment;
2525
import app.revanced.integrations.sponsorblock.requests.SBRequester;
@@ -102,20 +102,18 @@ public static void executeDownloadSegments(String videoId) {
102102
}
103103

104104
sponsorSegmentsOfCurrentVideo = segments;
105-
// new Handler(Looper.getMainLooper()).post(findAndSkipSegmentRunnable);
105+
// new Handler(Looper.getMainLooper()).post(findAndSkipSegmentRunnable);
106106
}
107107

108-
/**
109-
* Called when it's time to update the UI with new second, about once per second, only when playing, also in background
110-
*/
111-
public static void setCurrentVideoTime(long millis) {
108+
109+
public static void setVideoTime(long millis) {
112110
LogHelper.debug(PlayerController.class, "setCurrentVideoTime: current video time: " + millis);
113111
if (!SettingsEnum.SB_ENABLED.getBoolean()) return;
114112
lastKnownVideoTime = millis;
115113
if (millis <= 0) return;
116114
//findAndSkipSegment(false);
117115

118-
if (millis == PlayerControllerPatch.getCurrentVideoLength()) {
116+
if (millis == VideoInformation.getCurrentVideoLength()) {
119117
SponsorBlockUtils.hideShieldButton();
120118
SponsorBlockUtils.hideVoteButton();
121119
return;
@@ -187,22 +185,19 @@ private static void sendViewRequestAsync(final long millis, final SponsorSegment
187185
}).start();
188186
}
189187

190-
/**
191-
* Called very high frequency (once every about 100ms), also in background. It sometimes triggers when a video is paused (couple times in the row with the same value)
192-
*/
193-
public static void setCurrentVideoTimeHighPrecision(final long millis) {
194-
if ((millis < lastKnownVideoTime && lastKnownVideoTime >= PlayerControllerPatch.getCurrentVideoLength()) || millis == 0) {
188+
public static void setHighPrecisionVideoTime(final long millis) {
189+
if ((millis < lastKnownVideoTime && lastKnownVideoTime >= VideoInformation.getCurrentVideoLength()) || millis == 0) {
195190
SponsorBlockUtils.showShieldButton(); // skipping from end to the video will show the buttons again
196191
SponsorBlockUtils.showVoteButton();
197192
}
198193
if (lastKnownVideoTime > 0) {
199194
lastKnownVideoTime = millis;
200195
} else
201-
setCurrentVideoTime(millis);
196+
setVideoTime(millis);
202197
}
203198

204199
public static long getCurrentVideoLength() {
205-
return PlayerControllerPatch.getCurrentVideoLength();
200+
return VideoInformation.getCurrentVideoLength();
206201
}
207202

208203
public static long getLastKnownVideoTime() {
@@ -296,7 +291,7 @@ public static void drawSponsorTimeBars(final Canvas canvas, final float posY) {
296291
final float absoluteLeft = sponsorBarLeft;
297292
final float absoluteRight = sponsorBarRight;
298293

299-
final float tmp1 = 1f / (float) PlayerControllerPatch.getCurrentVideoLength() * (absoluteRight - absoluteLeft);
294+
final float tmp1 = 1f / (float) VideoInformation.getCurrentVideoLength() * (absoluteRight - absoluteLeft);
300295
for (SponsorSegment segment : sponsorSegmentsOfCurrentVideo) {
301296
float left = segment.start * tmp1 + absoluteLeft;
302297
float right = segment.end * tmp1 + absoluteLeft;
@@ -330,7 +325,7 @@ public static boolean skipToMillisecond(long millisecond) {
330325
try {
331326
LogHelper.debug(PlayerController.class, "Skipping to millis=" + finalMillisecond);
332327
lastKnownVideoTime = finalMillisecond;
333-
PlayerControllerPatch.seekTo(finalMillisecond);
328+
VideoInformation.seekTo(finalMillisecond);
334329
} catch (Exception e) {
335330
LogHelper.printException(PlayerController.class, "Cannot skip to millisecond", e);
336331
}

0 commit comments

Comments
 (0)