opt handle brightness/volume

Closes #2134

Signed-off-by: dom <githubaccount56556@proton.me>
This commit is contained in:
dom
2026-05-17 18:23:17 +08:00
parent b04c3d878d
commit 6a1db5d26b
3 changed files with 70 additions and 58 deletions

View File

@@ -153,6 +153,54 @@ class _PLVideoPlayerState extends State<PLVideoPlayer>
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<PLVideoPlayer>
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<PLVideoPlayer>
}
Future<void> 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<PLVideoPlayer>
}
void _onPanEnd(ScaleEndDetails details) {
if (Platform.isAndroid &&
_gestureType == .left &&
plPlayerController.setSystemBrightness) {
ScreenBrightnessPlatform.instance.restoreBrightnessMode();
}
if (plPlayerController.showSeekPreview) {
plPlayerController.showPreview.value = false;
}