基于自定义规则的番剧采集APP,支持流媒体在线观看,支持弹幕,支持实时超分辨率。
feat: adaptive PiP aspect (#2026)
x1renefde0d822f2c
diff --git a/android/app/src/main/kotlin/com/example/kazumi/MainActivity.kt b/android/app/src/main/kotlin/com/example/kazumi/MainActivity.ktindex 323d5365..a92e189a 100644--- a/android/app/src/main/kotlin/com/example/kazumi/MainActivity.kt+++ b/android/app/src/main/kotlin/com/example/kazumi/MainActivity.kt@@ -31,6 +31,8 @@ class MainActivity: AudioServiceActivity() {private var pipActionReceiverRegistered = falseprivate var autoEnterPipOnHomeGesture = falseprivate var pipInPlayerPage = false+ private var pipAspectWidth = 16+ private var pipAspectHeight = 9private val actionPipPlayPause = "com.predidit.kazumi.pip.PLAY_PAUSE"private val actionPipForward = "com.predidit.kazumi.pip.FORWARD"@@ -96,13 +98,15 @@ class MainActivity: AudioServiceActivity() {if (call.method == "isPictureInPictureSupported") {result.success(isPictureInPictureSupported())} else if (call.method == "enterPictureInPictureMode") {- val width = call.argument<Int>("width") ?: 16- val height = call.argument<Int>("height") ?: 9- val entered = enterPictureInPicture(width, height)+ pipAspectWidth = call.argument<Int>("width") ?: pipAspectWidth+ pipAspectHeight = call.argument<Int>("height") ?: pipAspectHeight+ val entered = enterPictureInPicture()result.success(entered)} else if (call.method == "updatePictureInPictureActions") {val playing = call.argument<Boolean>("playing") ?: falseval danmakuEnabled = call.argument<Boolean>("danmakuEnabled") ?: false+ pipAspectWidth = call.argument<Int>("width") ?: pipAspectWidth+ pipAspectHeight = call.argument<Int>("height") ?: pipAspectHeightupdatePictureInPictureActions(playing, danmakuEnabled)result.success(true)} else if (call.method == "setAndroidAutoEnterPIPEnabled") {@@ -145,14 +149,14 @@ class MainActivity: AudioServiceActivity() {return packageManager.hasSystemFeature(PackageManager.FEATURE_PICTURE_IN_PICTURE)}- private fun enterPictureInPicture(width: Int, height: Int): Boolean {+ private fun enterPictureInPicture(): Boolean {if (!isPictureInPictureSupported()) {return false}if (isInPictureInPictureMode) {return true}- return enterPictureInPictureMode(buildPictureInPictureParams(width, height))+ return enterPictureInPictureMode(buildPictureInPictureParams())}private fun updatePictureInPictureActions(@@ -167,12 +171,10 @@ class MainActivity: AudioServiceActivity() {refreshPictureInPictureParamsIfNeeded()}- private fun buildPictureInPictureParams(width: Int = 16, height: Int = 9): PictureInPictureParams {- val safeWidth = if (width > 0) width else 16- val safeHeight = if (height > 0) height else 9+ private fun buildPictureInPictureParams(): PictureInPictureParams {val actions = buildPipActions()val builder = PictureInPictureParams.Builder()- .setAspectRatio(Rational(safeWidth, safeHeight))+ .setAspectRatio(Rational(pipAspectWidth, pipAspectHeight))if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {builder.setAutoEnterEnabled(autoEnterPipOnHomeGesture && pipInPlayerPage)builder.setSeamlessResizeEnabled(false)diff --git a/lib/pages/player/player_item.dart b/lib/pages/player/player_item.dartindex f31fb77e..9a888c76 100644--- a/lib/pages/player/player_item.dart+++ b/lib/pages/player/player_item.dart@@ -127,6 +127,7 @@ class _PlayerItemState extends State<PlayerItem>int episodeNum = 0;bool? _lastPipPlaying;bool? _lastPipDanmakuEnabled;+ late mobx.ReactionDisposer _playerSizeListener;late mobx.ReactionDisposer _fullscreenListener;@@ -199,9 +200,28 @@ class _PlayerItemState extends State<PlayerItem>await PipUtils.updateAndroidPIPActions(playing: playing,danmakuEnabled: danmakuEnabled,+ width: playerController.playerWidth,+ height: playerController.playerHeight,);}+ Future<void> _syncPIPAspectWhenVideoSizeReady() async {+ if (playerController.playerWidth <= 0 ||+ playerController.playerHeight <= 0) {+ return;+ }+ if (Platform.isAndroid) {+ await _updateAndroidPIPActions(force: true);+ return;+ }+ if (Utils.isDesktop() && videoPageController.isPip) {+ await PipUtils.enterDesktopPIPWindow(+ width: playerController.playerWidth,+ height: playerController.playerHeight,+ );+ }+ }+void _loadShortcuts() {keyboardShortcuts = {};defaultShortcuts.forEach((key, defaultValue) {@@ -1455,6 +1475,12 @@ class _PlayerItemState extends State<PlayerItem>_handleFullscreenChange(context);},);+ _playerSizeListener = mobx.reaction<String>(+ (_) => '${playerController.playerWidth}:${playerController.playerHeight}',+ (_) {+ unawaited(_syncPIPAspectWhenVideoSizeReady());+ },+ );if (Platform.isAndroid) {PipUtils.initPipHandler(onAction: (action) async {@@ -1535,6 +1561,7 @@ class _PlayerItemState extends State<PlayerItem>// We need to reuse the player after episode is changed and player item is disposed// We dispose player after video page disposed_fullscreenListener();+ _playerSizeListener();WidgetsBinding.instance.removeObserver(this);windowManager.removeListener(this);playerTimer?.cancel();@@ -1604,7 +1631,7 @@ class _PlayerItemState extends State<PlayerItem>}},child: SizedBox(- height: videoPageController.isFullscreen+ height: videoPageController.isFullscreen || videoPageController.isPip? (MediaQuery.of(context).size.height): (MediaQuery.of(context).size.width * 9.0 / (16.0)),width: MediaQuery.of(context).size.width,@@ -1682,7 +1709,7 @@ class _PlayerItemState extends State<PlayerItem>top: 0,left: 0,right: 0,- height: videoPageController.isFullscreen+ height: videoPageController.isFullscreen || videoPageController.isPip? MediaQuery.sizeOf(context).height: (MediaQuery.sizeOf(context).width * 9 / 16),child: DanmakuScreen(diff --git a/lib/pages/player/player_item_panel.dart b/lib/pages/player/player_item_panel.dartindex 64bf70b1..5035f4e6 100644--- a/lib/pages/player/player_item_panel.dart+++ b/lib/pages/player/player_item_panel.dart@@ -1120,7 +1120,10 @@ class _PlayerItemPanelState extends State<PlayerItemPanel> {if (videoPageController.isPip) {await PipUtils.exitDesktopPIPWindow();} else {- await PipUtils.enterDesktopPIPWindow();+ await PipUtils.enterDesktopPIPWindow(+ width: playerController.playerWidth,+ height: playerController.playerHeight,+ );}videoPageController.isPip = !videoPageController.isPip;return;@@ -1134,8 +1137,13 @@ class _PlayerItemPanelState extends State<PlayerItemPanel> {await PipUtils.updateAndroidPIPActions(playing: playerController.playing,danmakuEnabled: playerController.danmakuOn,+ width: playerController.playerWidth,+ height: playerController.playerHeight,+ );+ final bool entered = await PipUtils.enterAndroidPIPWindow(+ width: playerController.playerWidth,+ height: playerController.playerHeight,);- final bool entered = await PipUtils.enterAndroidPIPWindow();if (!entered) {KazumiDialog.showToast(message: '进入画中画失败');}diff --git a/lib/pages/player/smallest_player_item_panel.dart b/lib/pages/player/smallest_player_item_panel.dartindex 684b9a3c..e36337b0 100644--- a/lib/pages/player/smallest_player_item_panel.dart+++ b/lib/pages/player/smallest_player_item_panel.dart@@ -570,7 +570,11 @@ class _SmallestPlayerItemPanelState extends State<SmallestPlayerItemPanel> {if (videoPageController.isPip) {await PipUtils.exitDesktopPIPWindow();} else {- await PipUtils.enterDesktopPIPWindow();+ // 进入画中画时使用播放源比例,避免窗口比例与视频比例不一致产生黑边+ await PipUtils.enterDesktopPIPWindow(+ width: playerController.playerWidth,+ height: playerController.playerHeight,+ );}videoPageController.isPip = !videoPageController.isPip;return;@@ -583,8 +587,13 @@ class _SmallestPlayerItemPanelState extends State<SmallestPlayerItemPanel> {await PipUtils.updateAndroidPIPActions(playing: playerController.playing,danmakuEnabled: playerController.danmakuOn,+ width: playerController.playerWidth,+ height: playerController.playerHeight,+ );+ final bool entered = await PipUtils.enterAndroidPIPWindow(+ width: playerController.playerWidth,+ height: playerController.playerHeight,);- final bool entered = await PipUtils.enterAndroidPIPWindow();if (!entered) {KazumiDialog.showToast(message: '进入画中画失败');}diff --git a/lib/utils/pip_utils.dart b/lib/utils/pip_utils.dartindex 30f5af0e..c1f7b919 100644--- a/lib/utils/pip_utils.dart+++ b/lib/utils/pip_utils.dart@@ -8,6 +8,15 @@ import 'package:window_manager/window_manager.dart';class PipUtils {static bool androidPIPInited = false;+ // 比例约分+ static Size getPIPAspectSize({required int width, required int height}) {+ if (width <= 0 || height <= 0) {+ return const Size(16, 9);+ }+ final int divisor = width.gcd(height);+ return Size(width / divisor, height / divisor);+ }+static Future<bool> isAndroidPIPSupported() async {if (!Platform.isAndroid) {return false;@@ -28,12 +37,13 @@ class PipUtils {if (!Platform.isAndroid) {return false;}+ final Size aspectSize = getPIPAspectSize(width: width, height: height);const pipChannel = MethodChannel('com.predidit.kazumi/pip');try {final bool? entered =await pipChannel.invokeMethod('enterPictureInPictureMode', {- 'width': width,- 'height': height,+ 'width': aspectSize.width.toInt(),+ 'height': aspectSize.height.toInt(),});return entered ?? false;} on PlatformException catch (e) {@@ -45,15 +55,20 @@ class PipUtils {static Future<void> updateAndroidPIPActions({required bool playing,required bool danmakuEnabled,+ int width = 16,+ int height = 9,}) async {if (!Platform.isAndroid) {return;}+ final Size aspectSize = getPIPAspectSize(width: width, height: height);const pipChannel = MethodChannel('com.predidit.kazumi/pip');try {await pipChannel.invokeMethod('updatePictureInPictureActions', {'playing': playing,'danmakuEnabled': danmakuEnabled,+ 'width': aspectSize.width.toInt(),+ 'height': aspectSize.height.toInt(),});} on PlatformException catch (e) {KazumiLogger().e("Failed to update Android PIP actions: '${e.message}'.");@@ -89,16 +104,22 @@ class PipUtils {}}- // 进入桌面设备小窗模式- static Future<void> enterDesktopPIPWindow() async {+ // 进入桌面设备小窗模式,并用播放源比例固定窗口宽高比+ static Future<void> enterDesktopPIPWindow(+ {int width = 16, int height = 9}) async {+ final Size aspectSize = getPIPAspectSize(width: width, height: height);+ final double aspectRatio = aspectSize.width / aspectSize.height;+ const double pipWidth = 480;await windowManager.setAlwaysOnTop(true);- await windowManager.setSize(const Size(480, 270));+ await windowManager.setAspectRatio(aspectRatio);+ await windowManager.setSize(Size(pipWidth, pipWidth / aspectRatio));}// 退出桌面设备小窗模式static Future<void> exitDesktopPIPWindow() async {bool isLowResolution = await Utils.isLowResolution();await windowManager.setAlwaysOnTop(false);+ await windowManager.setAspectRatio(0);await windowManager.setSize(isLowResolution ? const Size(800, 600) : const Size(1280, 860));await windowManager.center();