From 6a1db5d26bf9363c98e2c0b00e43cdbf63cda4db Mon Sep 17 00:00:00 2001 From: dom Date: Sun, 17 May 2026 18:23:17 +0800 Subject: [PATCH] opt handle brightness/volume Closes #2134 Signed-off-by: dom --- lib/plugin/pl_player/view/view.dart | 96 ++++++++++++++++++----------- pubspec.lock | 18 +++--- pubspec.yaml | 14 +---- 3 files changed, 70 insertions(+), 58 deletions(-) diff --git a/lib/plugin/pl_player/view/view.dart b/lib/plugin/pl_player/view/view.dart index af85a8680..b4a8b165e 100644 --- a/lib/plugin/pl_player/view/view.dart +++ b/lib/plugin/pl_player/view/view.dart @@ -153,6 +153,54 @@ class _PLVideoPlayerState extends State bool _pauseDueToPauseUponEnteringBackgroundMode = false; StreamSubscription? _brightnessListener; + void _onBrightnessChanged(double value) { + if (mounted && _gestureType != .left) { + _brightnessValue.value = value; + } + } + + void _getSystemBrightness() { + ScreenBrightnessPlatform.instance.system.then((res) { + if (mounted) { + _brightnessValue.value = res; + } + }); + } + + void _getAppBrightness() { + ScreenBrightnessPlatform.instance.application.then((res) { + if (mounted) { + _brightnessValue.value = res; + } + }); + } + + void _onVolumeChanged(double value) { + if (mounted && !plPlayerController.volumeInterceptEventStream) { + plPlayerController.volume.value = value; + if (Platform.isIOS && !FlutterVolumeController.showSystemUI) { + plPlayerController + ..volumeIndicator.value = true + ..volumeTimer?.cancel() + ..volumeTimer = Timer( + const Duration(milliseconds: 800), + () { + if (mounted) { + plPlayerController.volumeIndicator.value = false; + } + }, + ); + } + } + } + + void _getCurrVolume() { + FlutterVolumeController.getVolume().then((res) { + if (mounted) { + plPlayerController.volume.value = res!; + } + }); + } int? tmpSubtitlePaddingB; StreamSubscription? _controlsListener; @@ -220,51 +268,29 @@ class _PLVideoPlayerState extends State videoController = plPlayerController.videoController!; if (PlatformUtils.isMobile) { - Future.microtask(() async { + Future.microtask(() { try { FlutterVolumeController.updateShowSystemUI(true); - plPlayerController.volume.value = - (await FlutterVolumeController.getVolume())!; - FlutterVolumeController.addListener((double value) { - if (mounted && !plPlayerController.volumeInterceptEventStream) { - plPlayerController.volume.value = value; - if (Platform.isIOS && !FlutterVolumeController.showSystemUI) { - plPlayerController - ..volumeIndicator.value = true - ..volumeTimer?.cancel() - ..volumeTimer = Timer(const Duration(milliseconds: 800), () { - if (mounted) { - plPlayerController.volumeIndicator.value = false; - } - }); - } - } - }, emitOnStart: false); + _getCurrVolume(); + FlutterVolumeController.addListener( + _onVolumeChanged, + emitOnStart: false, + ); } catch (_) {} - }); - Future.microtask(() async { try { - void listener(double value) { - if (mounted) { - _brightnessValue.value = value; - } - } - if (Platform.isIOS || plPlayerController.setSystemBrightness) { - _brightnessValue.value = - await ScreenBrightnessPlatform.instance.system; + _getSystemBrightness(); _brightnessListener = ScreenBrightnessPlatform .instance .onSystemScreenBrightnessChanged - .listen(listener); + .listen(_onBrightnessChanged); } else { - _brightnessValue.value = - await ScreenBrightnessPlatform.instance.application; + _getAppBrightness(); _brightnessListener = ScreenBrightnessPlatform .instance .onApplicationScreenBrightnessChanged - .listen(listener); + .listen(_onBrightnessChanged); } } catch (_) {} }); @@ -321,6 +347,7 @@ class _PLVideoPlayerState extends State } Future setBrightness(double value) async { + _brightnessValue.value = value; try { if (Platform.isIOS || plPlayerController.setSystemBrightness) { await ScreenBrightnessPlatform.instance.setSystemScreenBrightness( @@ -1105,11 +1132,6 @@ class _PLVideoPlayerState extends State } void _onPanEnd(ScaleEndDetails details) { - if (Platform.isAndroid && - _gestureType == .left && - plPlayerController.setSystemBrightness) { - ScreenBrightnessPlatform.instance.restoreBrightnessMode(); - } if (plPlayerController.showSeekPreview) { plPlayerController.showPreview.value = false; } diff --git a/pubspec.lock b/pubspec.lock index 3aa7a30c8..f17a4fc47 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -1531,11 +1531,10 @@ packages: screen_brightness_android: dependency: "direct overridden" description: - path: screen_brightness_android - ref: dev - resolved-ref: "9823a66d9a0af8d9f5d0d9e98f87bb17e399235e" - url: "https://github.com/bggRGjQaUbCoE/screen_brightness.git" - source: git + name: screen_brightness_android + sha256: "2df5e0488a22a73271a43d92dbcd80643df6910cf69690faacaf9929ae9ade3b" + url: "https://pub.dev" + source: hosted version: "2.1.4" screen_brightness_ios: dependency: "direct overridden" @@ -1548,11 +1547,10 @@ packages: screen_brightness_platform_interface: dependency: "direct main" description: - path: screen_brightness_platform_interface - ref: dev - resolved-ref: "9823a66d9a0af8d9f5d0d9e98f87bb17e399235e" - url: "https://github.com/bggRGjQaUbCoE/screen_brightness.git" - source: git + name: screen_brightness_platform_interface + sha256: "59d50850d6735d677780fc7359c8e997d0ff6df91c8465161c9e617a7b0a11d8" + url: "https://pub.dev" + source: hosted version: "2.1.1" screen_retriever: dependency: "direct main" diff --git a/pubspec.yaml b/pubspec.yaml index fdf0d6a2d..505b4edb2 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -209,17 +209,9 @@ dependency_overrides: mime: ^2.0.0 path: ^1.9.1 rxdart: ^0.28.0 - screen_brightness_android: - git: - url: https://github.com/bggRGjQaUbCoE/screen_brightness.git - path: screen_brightness_android - ref: dev - screen_brightness_ios: ^2.1.2 - screen_brightness_platform_interface: - git: - url: https://github.com/bggRGjQaUbCoE/screen_brightness.git - path: screen_brightness_platform_interface - ref: dev + screen_brightness_android: ^2.1.4 + screen_brightness_ios: ^2.1.3 + screen_brightness_platform_interface: ^2.1.1 dev_dependencies: flutter_test: