fix(youtube/remember-video-quality): fix default video quality/speed being applied when resuming app (#392)

This commit is contained in:
LisoUseInAIKyrios 2023-05-09 19:50:56 +04:00 committed by GitHub
parent 4fe923527e
commit c97d1b7ee5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 46 deletions

View File

@ -4,6 +4,7 @@ import androidx.annotation.NonNull;
import java.lang.ref.WeakReference;
import java.lang.reflect.Method;
import java.util.Objects;
import app.revanced.integrations.patches.playback.speed.RememberPlaybackSpeedPatch;
import app.revanced.integrations.utils.LogHelper;
@ -16,7 +17,7 @@ public final class VideoInformation {
private static final float DEFAULT_YOUTUBE_PLAYBACK_SPEED = 1.0f;
private static final String SEEK_METHOD_NAME = "seekTo";
private static WeakReference<Object> playerController;
private static WeakReference<Object> playerControllerRef;
private static Method seekMethod;
@NonNull
@ -30,17 +31,17 @@ public final class VideoInformation {
/**
* Injection point.
* Sets a reference to the YouTube playback controller.
*
* @param thisRef Reference to the player controller object.
* @param playerController player controller object.
*/
public static void playerController_onCreateHook(final Object thisRef) {
playerController = new WeakReference<>(thisRef);
videoLength = 0;
videoTime = -1;
public static void initialize(@NonNull Object playerController) {
try {
seekMethod = thisRef.getClass().getMethod(SEEK_METHOD_NAME, Long.TYPE);
playerControllerRef = new WeakReference<>(Objects.requireNonNull(playerController));
videoTime = -1;
videoLength = 0;
playbackSpeed = DEFAULT_YOUTUBE_PLAYBACK_SPEED;
seekMethod = playerController.getClass().getMethod(SEEK_METHOD_NAME, Long.TYPE);
seekMethod.setAccessible(true);
} catch (Exception ex) {
LogHelper.printException(() -> "Failed to initialize", ex);
@ -56,7 +57,6 @@ public final class VideoInformation {
if (!videoId.equals(newlyLoadedVideoId)) {
LogHelper.printDebug(() -> "New video id: " + newlyLoadedVideoId);
videoId = newlyLoadedVideoId;
playbackSpeed = DEFAULT_YOUTUBE_PLAYBACK_SPEED;
}
}
@ -124,7 +124,7 @@ public final class VideoInformation {
try {
LogHelper.printDebug(() -> "Seeking to " + millisecond);
return (Boolean) seekMethod.invoke(playerController.get(), millisecond);
return (Boolean) seekMethod.invoke(playerControllerRef.get(), millisecond);
} catch (Exception ex) {
LogHelper.printException(() -> "Failed to seek", ex);
return false;

View File

@ -2,7 +2,6 @@ package app.revanced.integrations.patches.playback.quality;
import static app.revanced.integrations.utils.ReVancedUtils.NetworkType;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.lang.reflect.Field;
@ -20,8 +19,6 @@ public class RememberVideoQualityPatch {
private static final SettingsEnum mobileQualitySetting = SettingsEnum.VIDEO_QUALITY_DEFAULT_MOBILE;
private static boolean qualityNeedsUpdating;
@Nullable
private static String currentVideoId;
/**
* If the user selected a new quality from the flyout menu,
@ -91,7 +88,7 @@ public class RememberVideoQualityPatch {
}
}
}
LogHelper.printDebug(() -> "VideoId: " + currentVideoId + " videoQualities: " + videoQualities);
LogHelper.printDebug(() -> "videoQualities: " + videoQualities);
}
if (userChangedDefaultQuality) {
@ -144,25 +141,9 @@ public class RememberVideoQualityPatch {
/**
* Injection point.
*/
public static void newVideoStarted(@NonNull String videoId) {
// The same videoId can be passed in multiple times for a single video playback.
// Such as closing and opening the app, and sometimes when turning off/on the device screen.
//
// Known limitation, if:
// 1. a default video quality exists, and remember quality is turned off
// 2. user opens a video
// 3. user changes the video quality
// 4. user turns off then on the device screen (or does anything else that triggers the video id hook)
// result: the video quality of the current video will revert back to the saved default
//
// qualityNeedsUpdating could be set only when the videoId changes
// but then if the user closes and re-opens the same video the default video quality will not be applied.
LogHelper.printDebug(() -> "newVideoStarted: " + videoId);
public static void newVideoStarted(Object ignoredPlayerController) {
LogHelper.printDebug(() -> "newVideoStarted");
qualityNeedsUpdating = true;
if (!videoId.equals(currentVideoId)) {
currentVideoId = videoId;
videoQualities = null;
}
videoQualities = null;
}
}

View File

@ -2,11 +2,9 @@ package app.revanced.integrations.patches.playback.speed;
import android.preference.ListPreference;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import app.revanced.integrations.patches.VideoInformation;
import app.revanced.integrations.settings.SettingsEnum;
import app.revanced.integrations.utils.LogHelper;
import app.revanced.integrations.utils.ReVancedUtils;
public final class RememberPlaybackSpeedPatch {
@ -16,18 +14,12 @@ public final class RememberPlaybackSpeedPatch {
*/
private static String[] preferenceListEntries, preferenceListEntryValues;
@Nullable
private static String currentVideoId;
/**
* Injection point.
* Called when a new video loads.
*/
public static void newVideoLoaded(@NonNull String videoId) {
if (videoId.equals(currentVideoId)) {
return;
}
currentVideoId = videoId;
public static void newVideoStarted(Object ignoredPlayerController) {
LogHelper.printDebug(() -> "newVideoStarted");
VideoInformation.overridePlaybackSpeed(SettingsEnum.PLAYBACK_SPEED_DEFAULT.getFloat());
}

View File

@ -177,7 +177,7 @@ public class SegmentPlaybackController {
* Injection point.
* Initializes SponsorBlock when the video player starts playing a new video.
*/
public static void initialize(Object _o) {
public static void initialize(Object ignoredPlayerController) {
try {
ReVancedUtils.verifyOnMainThread();
SponsorBlockSettings.initialize();