diff --git a/extensions/shared/src/main/java/app/revanced/extension/youtube/patches/video/VideoQualityPatch.java b/extensions/shared/src/main/java/app/revanced/extension/youtube/patches/video/VideoQualityPatch.java index 22b51c334..c42125c0d 100644 --- a/extensions/shared/src/main/java/app/revanced/extension/youtube/patches/video/VideoQualityPatch.java +++ b/extensions/shared/src/main/java/app/revanced/extension/youtube/patches/video/VideoQualityPatch.java @@ -5,6 +5,7 @@ import androidx.annotation.NonNull; import app.revanced.extension.shared.settings.IntegerSetting; +import app.revanced.extension.shared.utils.Logger; import app.revanced.extension.shared.utils.Utils; import app.revanced.extension.youtube.settings.Settings; import app.revanced.extension.youtube.shared.PlayerType; @@ -58,11 +59,11 @@ private static void setVideoQuality(final long delayMillis) { if (defaultQuality == DEFAULT_YOUTUBE_VIDEO_QUALITY) return; - Utils.runOnMainThreadDelayed(() -> - VideoInformation.overrideVideoQuality( - VideoInformation.getAvailableVideoQuality(defaultQuality) - ), - delayMillis + Utils.runOnMainThreadDelayed(() -> { + final int qualityToUseFinal = VideoInformation.getAvailableVideoQuality(defaultQuality); + Logger.printDebug(() -> "Changing video quality to: " + qualityToUseFinal); + VideoInformation.overrideVideoQuality(qualityToUseFinal); + }, delayMillis ); } diff --git a/extensions/shared/src/main/java/app/revanced/extension/youtube/shared/VideoInformation.java b/extensions/shared/src/main/java/app/revanced/extension/youtube/shared/VideoInformation.java index 262c9d4d2..0c0b87d81 100644 --- a/extensions/shared/src/main/java/app/revanced/extension/youtube/shared/VideoInformation.java +++ b/extensions/shared/src/main/java/app/revanced/extension/youtube/shared/VideoInformation.java @@ -71,6 +71,8 @@ public final class VideoInformation { @Nullable private static List videoQualities; + private static boolean qualityNeedsUpdating; + /** * Injection point. */ @@ -415,16 +417,18 @@ public static void setVideoQuality(String newlyLoadedQuality) { * @return available video quality. */ public static int getAvailableVideoQuality(int preferredQuality) { - if (videoQualities != null) { - int qualityToUse = videoQualities.get(0); // first element is automatic mode - for (Integer quality : videoQualities) { - if (quality <= preferredQuality && qualityToUse < quality) { - qualityToUse = quality; - } + if (!qualityNeedsUpdating || videoQualities == null) { + return preferredQuality; + } + qualityNeedsUpdating = false; + + int qualityToUse = videoQualities.get(0); // first element is automatic mode + for (Integer quality : videoQualities) { + if (quality <= preferredQuality && qualityToUse < quality) { + qualityToUse = quality; } - preferredQuality = qualityToUse; } - return preferredQuality; + return qualityToUse; } /** @@ -444,6 +448,7 @@ public static void setVideoQualityList(Object[] qualities) { } } } + qualityNeedsUpdating = true; Logger.printDebug(() -> "videoQualities: " + videoQualities); } } catch (Exception ex) {