diff --git a/lib/pages/setting/play_setting.dart b/lib/pages/setting/play_setting.dart index 6606f3c4..82459be9 100644 --- a/lib/pages/setting/play_setting.dart +++ b/lib/pages/setting/play_setting.dart @@ -85,6 +85,12 @@ class _PlaySettingState extends State { setKey: SettingBoxKey.enableBackgroundPlay, defaultVal: false, ), + const SetSwitchItem( + title: '自动PiP播放', + subTitle: 'app切换至后台时画中画播放', + setKey: SettingBoxKey.autoPiP, + defaultVal: false, + ), const SetSwitchItem( title: '自动全屏', subTitle: '视频开始播放时进入全屏', diff --git a/lib/pages/video/detail/view.dart b/lib/pages/video/detail/view.dart index 0debd259..75d91c23 100644 --- a/lib/pages/video/detail/view.dart +++ b/lib/pages/video/detail/view.dart @@ -32,7 +32,7 @@ class VideoDetailPage extends StatefulWidget { } class _VideoDetailPageState extends State - with TickerProviderStateMixin, RouteAware { + with TickerProviderStateMixin, RouteAware, WidgetsBindingObserver { late VideoDetailController videoDetailController; PlPlayerController? plPlayerController; final ScrollController _extendNestCtr = ScrollController(); @@ -52,6 +52,8 @@ class _VideoDetailPageState extends State // 自动退出全屏 late bool autoExitFullcreen; late bool autoPlayEnable; + late bool autoPiP; + final floating = Floating(); @override void initState() { @@ -65,8 +67,11 @@ class _VideoDetailPageState extends State setting.get(SettingBoxKey.enableAutoExit, defaultValue: false); autoPlayEnable = setting.get(SettingBoxKey.autoPlayEnable, defaultValue: true); + autoPiP = setting.get(SettingBoxKey.autoPiP, defaultValue: false); + videoSourceInit(); appbarStreamListen(); + WidgetsBinding.instance.addObserver(this); } // 获取视频资源,初始化播放器 @@ -149,6 +154,8 @@ class _VideoDetailPageState extends State if (videoDetailController.floating != null) { videoDetailController.floating!.dispose(); } + WidgetsBinding.instance.removeObserver(this); + floating.dispose(); super.dispose(); } @@ -195,6 +202,17 @@ class _VideoDetailPageState extends State .subscribe(this, ModalRoute.of(context) as PageRoute); } + @override + void didChangeAppLifecycleState(AppLifecycleState lifecycleState) { + if (lifecycleState == AppLifecycleState.inactive && autoPiP) { + floating.enable( + aspectRatio: Rational( + videoDetailController.data.dash!.video!.first.width!, + videoDetailController.data.dash!.video!.first.height!, + )); + } + } + @override Widget build(BuildContext context) { final videoHeight = MediaQuery.of(context).size.width * 9 / 16; @@ -493,6 +511,7 @@ class _VideoDetailPageState extends State return PiPSwitcher( childWhenDisabled: childWhenDisabled, childWhenEnabled: childWhenEnabled, + floating: floating, ); } else { return childWhenDisabled; diff --git a/lib/utils/storage.dart b/lib/utils/storage.dart index ea30253b..d49bd4ad 100644 --- a/lib/utils/storage.dart +++ b/lib/utils/storage.dart @@ -106,6 +106,7 @@ class SettingBoxKey { static const String enableAutoExit = 'enableAutoExit'; static const String p1080 = 'p1080'; static const String enableCDN = 'enableCDN'; + static const String autoPiP = 'autoPiP'; // youtube 双击快进快退 static const String enableQuickDouble = 'enableQuickDouble';