feat: custom subtitle padding

Closes #77

Signed-off-by: bggRGjQaUbCoE <githubaccount56556@proton.me>
This commit is contained in:
bggRGjQaUbCoE
2025-01-01 12:24:54 +08:00
parent 144a9b604a
commit dbc93883e8
5 changed files with 239 additions and 172 deletions

View File

@@ -251,13 +251,47 @@ class PlPlayerController {
double? defaultDuration;
late bool enableAutoLongPressSpeed = false;
late bool enableLongShowControl;
RxDouble subtitleFontScale = (1.0).obs;
RxDouble subtitleFontScaleFS = (1.5).obs;
double subtitleFontScale = 1.0;
double subtitleFontScaleFS = 1.5;
late double danmakuLineHeight = GStorage.danmakuLineHeight;
late int subtitlePaddingH = GStorage.subtitlePaddingH;
late int subtitlePaddingB = GStorage.subtitlePaddingB;
// 播放顺序相关
PlayRepeat playRepeat = PlayRepeat.pause;
final GlobalKey<VideoState> key = GlobalKey<VideoState>();
TextStyle get subTitleStyle => TextStyle(
height: 1.5,
fontSize:
16 * (isFullScreen.value ? subtitleFontScaleFS : subtitleFontScale),
letterSpacing: 0.1,
wordSpacing: 0.1,
color: Colors.white,
fontWeight: FontWeight.normal,
backgroundColor: Color(0xaa000000),
);
void updateSubtitleStyle([double? value]) {
key.currentState?.update(
subtitleViewConfiguration: SubtitleViewConfiguration(
style: subTitleStyle.copyWith(
fontSize: 16 *
(value ??
(isFullScreen.value
? subtitleFontScaleFS
: subtitleFontScale))),
padding: EdgeInsets.only(
left: subtitlePaddingH.toDouble(),
right: subtitlePaddingH.toDouble(),
bottom: subtitlePaddingB.toDouble(),
),
textScaleFactor: MediaQuery.textScalerOf(Get.context!).scale(1),
),
);
}
void updateSliderPositionSecond() {
int newSecond = _sliderPosition.value.inSeconds;
if (sliderPositionSeconds.value != newSecond) {
@@ -346,8 +380,8 @@ class PlPlayerController {
setting.get(SettingBoxKey.danmakuFontScale, defaultValue: 1.0);
// 全屏字体大小
fontSizeFSVal = GStorage.danmakuFontScaleFS;
subtitleFontScale.value = GStorage.subtitleFontScale;
subtitleFontScaleFS.value = GStorage.subtitleFontScaleFS;
subtitleFontScale = GStorage.subtitleFontScale;
subtitleFontScaleFS = GStorage.subtitleFontScaleFS;
massiveMode = GStorage.danmakuMassiveMode;
// 弹幕时间
danmakuDurationVal =
@@ -1154,6 +1188,7 @@ class PlPlayerController {
void toggleFullScreen(bool val) {
_isFullScreen.value = val;
updateSubtitleStyle();
}
// 全屏
@@ -1279,12 +1314,14 @@ class PlPlayerController {
setting.put(SettingBoxKey.danmakuOpacity, opacityVal);
setting.put(SettingBoxKey.danmakuFontScale, fontSizeVal);
setting.put(SettingBoxKey.danmakuFontScaleFS, fontSizeFSVal);
setting.put(SettingBoxKey.subtitleFontScale, subtitleFontScale.value);
setting.put(SettingBoxKey.subtitleFontScaleFS, subtitleFontScaleFS.value);
setting.put(SettingBoxKey.subtitleFontScale, subtitleFontScale);
setting.put(SettingBoxKey.subtitleFontScaleFS, subtitleFontScaleFS);
setting.put(SettingBoxKey.danmakuDuration, danmakuDurationVal);
setting.put(SettingBoxKey.strokeWidth, strokeWidth);
setting.put(SettingBoxKey.fontWeight, fontWeight);
setting.put(SettingBoxKey.danmakuLineHeight, danmakuLineHeight);
setting.put(SettingBoxKey.subtitlePaddingH, subtitlePaddingH);
setting.put(SettingBoxKey.subtitlePaddingB, subtitlePaddingB);
}
Future<void> dispose({String type = 'single'}) async {

View File

@@ -78,7 +78,6 @@ class _PLVideoPlayerState extends State<PLVideoPlayer>
late BangumiIntroController? bangumiIntroController;
final GlobalKey _playerKey = GlobalKey();
final GlobalKey<VideoState> _key = GlobalKey<VideoState>();
final RxBool _mountSeekBackwardButton = false.obs;
final RxBool _mountSeekForwardButton = false.obs;
@@ -146,7 +145,6 @@ class _PLVideoPlayerState extends State<PLVideoPlayer>
}
StreamSubscription? _listener;
StreamSubscription? _listenerFS;
@override
void initState() {
@@ -226,7 +224,6 @@ class _PLVideoPlayerState extends State<PLVideoPlayer>
@override
void dispose() {
_listener?.cancel();
_listenerFS?.cancel();
animationController.dispose();
FlutterVolumeController.removeListener();
super.dispose();
@@ -573,39 +570,8 @@ class _PLVideoPlayerState extends State<PLVideoPlayer>
bool get isFullScreen => plPlayerController.isFullScreen.value;
TextStyle get subTitleStyle => TextStyle(
height: 1.5,
fontSize: 16 *
(isFullScreen
? plPlayerController.subtitleFontScaleFS.value
: plPlayerController.subtitleFontScale.value),
letterSpacing: 0.1,
wordSpacing: 0.1,
color: Colors.white,
fontWeight: FontWeight.normal,
backgroundColor: Color(0xaa000000),
);
void _updateSubtitle(double value) {
_key.currentState?.update(
subtitleViewConfiguration: SubtitleViewConfiguration(
style: subTitleStyle.copyWith(fontSize: 16 * value),
padding: const EdgeInsets.all(24.0),
textScaleFactor: MediaQuery.textScalerOf(context).scale(1),
),
);
}
@override
Widget build(BuildContext context) {
_listenerFS?.cancel();
_listenerFS = isFullScreen
? plPlayerController.subtitleFontScaleFS.listen((value) {
_updateSubtitle(value);
})
: _listenerFS = plPlayerController.subtitleFontScale.listen((value) {
_updateSubtitle(value);
});
final Color colorTheme = Theme.of(context).colorScheme.primary;
const TextStyle textStyle = TextStyle(
color: Colors.white,
@@ -747,7 +713,7 @@ class _PLVideoPlayerState extends State<PLVideoPlayer>
_gestureType = null;
},
child: Video(
key: _key,
key: plPlayerController.key,
controller: videoController,
controls: NoVideoControls,
pauseUponEnteringBackgroundMode:
@@ -755,7 +721,7 @@ class _PLVideoPlayerState extends State<PLVideoPlayer>
resumeUponEnteringForegroundMode: true,
// 字幕尺寸调节
subtitleViewConfiguration: SubtitleViewConfiguration(
style: subTitleStyle,
style: plPlayerController.subTitleStyle,
padding: const EdgeInsets.all(24.0),
textScaleFactor: MediaQuery.textScalerOf(context).scale(1),
),