From 3b7e8ff7822d5e3ce004d704669c7f2c4b1e567f Mon Sep 17 00:00:00 2001 From: edde746 <86283021+edde746@users.noreply.github.com> Date: Thu, 22 Jan 2026 00:23:54 +0100 Subject: [PATCH] fix: clamp PiP aspect ratio to Android's valid range --- .../flutter_application_1/MainActivity.kt | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/android/app/src/main/kotlin/com/example/flutter_application_1/MainActivity.kt b/android/app/src/main/kotlin/com/example/flutter_application_1/MainActivity.kt index 3f36a9c0..cbd58641 100644 --- a/android/app/src/main/kotlin/com/example/flutter_application_1/MainActivity.kt +++ b/android/app/src/main/kotlin/com/example/flutter_application_1/MainActivity.kt @@ -57,8 +57,31 @@ class MainActivity : FlutterActivity() { try { val width = call.argument("width") ?: 16 val height = call.argument("height") ?: 9 + + // Android PiP requires aspect ratio between 0.418410 (5:12) and 2.39 (12:5) + val ratio = width.toFloat() / height.toFloat() + val clampedWidth: Int + val clampedHeight: Int + + when { + ratio < 0.42f -> { + // Too tall - clamp to minimum ratio (5:12) + clampedWidth = 5 + clampedHeight = 12 + } + ratio > 2.39f -> { + // Too wide - clamp to maximum ratio (12:5) + clampedWidth = 12 + clampedHeight = 5 + } + else -> { + clampedWidth = width + clampedHeight = height + } + } + val params = PictureInPictureParams.Builder() - .setAspectRatio(Rational(width, height)) + .setAspectRatio(Rational(clampedWidth, clampedHeight)) .build() val success = enterPictureInPictureMode(params) if (success) {