From dacc1b2b581eb4c663a204cf09f32e4f712c81fa Mon Sep 17 00:00:00 2001 From: orz12 Date: Wed, 6 Mar 2024 15:23:54 +0800 Subject: [PATCH] =?UTF-8?q?mod:=20=E6=94=B9=E5=96=84=E8=A7=86=E9=A2=91?= =?UTF-8?q?=E5=B0=BA=E5=AF=B8=E6=96=87=E6=A1=88=E4=B8=8E=E4=BD=93=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/plugin/pl_player/controller.dart | 45 +++++++++++++++++++++------- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/lib/plugin/pl_player/controller.dart b/lib/plugin/pl_player/controller.dart index c5ff881b..0b3c8d9b 100644 --- a/lib/plugin/pl_player/controller.dart +++ b/lib/plugin/pl_player/controller.dart @@ -81,8 +81,10 @@ class PlPlayerController { final Rx _direction = 'horizontal'.obs; - final Rx _videoFit = Rx(BoxFit.contain); - final Rx _videoFitDesc = Rx('包含'); + final Rx _videoFit = Rx(videoFitType.first['attr']); + final Rx _videoFitDesc = Rx(videoFitType.first['desc']); + late StreamSubscription _bufferedListenerForVideoFit; + late StreamSubscription _bufferedListenerForEnterFullscreen; /// // ignore: prefer_final_fields @@ -108,13 +110,14 @@ class PlPlayerController { // final Durations durations; - List> videoFitType = [ - {'attr': BoxFit.contain, 'desc': '包含'}, - {'attr': BoxFit.cover, 'desc': '覆盖'}, - {'attr': BoxFit.fill, 'desc': '填充'}, - {'attr': BoxFit.fitHeight, 'desc': '高度适应'}, - {'attr': BoxFit.fitWidth, 'desc': '宽度适应'}, - {'attr': BoxFit.scaleDown, 'desc': '缩小适应'}, + static List> videoFitType = [ + {'attr': BoxFit.contain, 'desc': '自动', 'toast': '缩放至播放器尺寸,保留黑边'}, + {'attr': BoxFit.cover, 'desc': '裁剪', 'toast': '缩放至填满播放器,裁剪超出部分'}, + {'attr': BoxFit.fill, 'desc': '拉伸', 'toast': '拉伸至播放器尺寸,将产生变形'}, + {'attr': BoxFit.none, 'desc': '原始', 'toast': '不缩放,以视频原始尺寸显示'}, + {'attr': BoxFit.fitHeight, 'desc': '等高', 'toast': '缩放至撑满播放器高度'}, + {'attr': BoxFit.fitWidth, 'desc': '等宽', 'toast': '缩放至撑满播放器宽度'}, + {'attr': BoxFit.scaleDown, 'desc': '限制', 'toast': '仅超出时缩小至播放器尺寸'}, ]; PreferredSizeWidget? headerControl; @@ -870,7 +873,7 @@ class PlPlayerController { context: Get.context!, builder: (context) { return AlertDialog( - title: const Text('画面比例'), + title: const Text('视频尺寸'), content: StatefulBuilder(builder: (context, StateSetter setState) { return Wrap( alignment: WrapAlignment.start, @@ -912,13 +915,33 @@ class PlPlayerController { Future setVideoFit() async { List attrs = videoFitType.map((e) => e['attr']).toList(); int index = attrs.indexOf(_videoFit.value); + SmartDialog.showToast(videoFitType[index]['toast'], + displayTime: const Duration(seconds: 1)); videoStorage.put(VideoBoxKey.cacheVideoFit, index); } /// 读取fit Future getVideoFit() async { int fitValue = videoStorage.get(VideoBoxKey.cacheVideoFit, defaultValue: 0); - _videoFit.value = videoFitType[fitValue]['attr']; + var attr = videoFitType[fitValue]['attr']; + // 由于none与scaleDown涉及视频原始尺寸,需要等待视频加载后再设置,否则尺寸会变为0,出现错误 + if (attr == BoxFit.none || attr == BoxFit.scaleDown) { + if (buffered.value == Duration.zero) { + attr = BoxFit.contain; + _bufferedListenerForVideoFit = buffered.listen((status) { + if (status > Duration.zero) { + _bufferedListenerForVideoFit.cancel(); + int fitValue = + videoStorage.get(VideoBoxKey.cacheVideoFit, defaultValue: 0); + var attr = videoFitType[fitValue]['attr']; + if (attr == BoxFit.none || attr == BoxFit.scaleDown) { + _videoFit.value = attr; + } + } + }); + } + } + _videoFit.value = attr; _videoFitDesc.value = videoFitType[fitValue]['desc']; }