Skip to content

Commit

Permalink
feat: 新增长按倍速递增;调整字幕选择偏好
Browse files Browse the repository at this point in the history
  • Loading branch information
orz12 committed Jan 4, 2025
1 parent 63dd8a4 commit 0fcdde0
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 74 deletions.
4 changes: 2 additions & 2 deletions lib/models/video/play/subtitle.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ enum SubtitlePreference { off, on, withoutAi }
extension SubtitlePreferenceDesc on SubtitlePreference {
static final List<String> _descList = [
'默认不显示字幕',
'选择第一个可用字幕',
'跳过自动生成(ai)字幕,选择第一个可用字幕'
'默认显示,优先选择非自动生成(ai)字幕',
'默认仅显示非自动生成(ai)字幕',
];
get description => _descList[index];
}
Expand Down
39 changes: 30 additions & 9 deletions lib/pages/setting/pages/play_speed_set.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class _PlaySpeedPageState extends State<PlaySpeedPage> {
late double longPressSpeedDefault;
late List<double> customSpeedsList;
late bool enableAutoLongPressSpeed;
late bool enableLongPressSpeedIncrease;
List<Map<dynamic, dynamic>> sheetMenu = [
{
'id': 1,
Expand Down Expand Up @@ -75,6 +76,8 @@ class _PlaySpeedPageState extends State<PlaySpeedPage> {
sheetMenu[1] = newItem;
});
}
enableLongPressSpeedIncrease = settingStorage
.get(SettingBoxKey.enableLongPressSpeedIncrease, defaultValue: false);
}

// 添加自定义倍速
Expand Down Expand Up @@ -114,6 +117,7 @@ class _PlaySpeedPageState extends State<PlaySpeedPage> {
customSpeedsList.add(customSpeed);
await videoStorage.put(
VideoBoxKey.customSpeedsList, customSpeedsList);
PlPlayerController.updateSettingsIfExist();
setState(() {});
Get.back();
},
Expand Down Expand Up @@ -197,6 +201,7 @@ class _PlaySpeedPageState extends State<PlaySpeedPage> {
customSpeedsList.removeAt(index);
await videoStorage.put(VideoBoxKey.customSpeedsList, customSpeedsList);
}
PlPlayerController.updateSettingsIfExist();
setState(() {});
SmartDialog.showToast('操作成功');
}
Expand All @@ -218,18 +223,10 @@ class _PlaySpeedPageState extends State<PlaySpeedPage> {
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding:
const EdgeInsets.only(left: 14, right: 14, top: 6, bottom: 0),
child: Text(
'点击下方按钮设置默认(长按)倍速',
style: TextStyle(color: Theme.of(context).colorScheme.outline),
),
),
ListTile(
dense: false,
title:
Text('默认倍速', style: Theme.of(context).textTheme.titleMedium),
Text('当前默认倍速', style: Theme.of(context).textTheme.titleMedium),
subtitle: Text(playSpeedDefault.toString()),
),
SetSwitchItem(
Expand All @@ -244,6 +241,7 @@ class _PlaySpeedPageState extends State<PlaySpeedPage> {
sheetMenu[1] = newItem;
enableAutoLongPressSpeed = val;
});
PlPlayerController.updateSettingsIfExist();
},
),
!enableAutoLongPressSpeed
Expand All @@ -254,6 +252,21 @@ class _PlaySpeedPageState extends State<PlaySpeedPage> {
subtitle: Text(longPressSpeedDefault.toString()),
)
: const SizedBox(),
const SetSwitchItem(
title: '长按倍速递增',
subTitle: '每长按半秒,倍速*1.15,最大8倍速',
setKey: SettingBoxKey.enableLongPressSpeedIncrease,
defaultVal: false,
callFn: PlPlayerController.updateSettingsIfExist,
),
Padding(
padding:
const EdgeInsets.only(left: 14, right: 14, top: 6, bottom: 0),
child: Text(
'点击下方按钮设置默认倍速、默认长按倍速',
style: TextStyle(color: Theme.of(context).colorScheme.outline),
),
),
Padding(
padding: const EdgeInsets.only(
left: 14,
Expand Down Expand Up @@ -335,6 +348,14 @@ class _PlaySpeedPageState extends State<PlaySpeedPage> {
),
),
),
ListTile(
subtitle: Text(
'注:由于播放器性能限制,4k、8k视频以大于2倍速播放,可能会出现卡顿、音画不同步等问题,请酌情选择。',
style: TextStyle(
color: Theme.of(context).colorScheme.outline,
),
),
)
],
),
),
Expand Down
7 changes: 4 additions & 3 deletions lib/pages/setting/play_setting.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class _PlaySettingState extends State<PlaySetting> {
leading: Icon(Icons.comment_outlined),
setKey: SettingBoxKey.enableShowDanmaku,
defaultVal: true,
callFn: PlPlayerController.updateSettingsIfExist,
),
ListTile(
dense: false,
Expand All @@ -86,6 +87,7 @@ class _PlaySettingState extends State<PlaySetting> {
leading: Icon(MdiIcons.playPause),
setKey: SettingBoxKey.autoPlayEnable,
defaultVal: true,
callFn: PlPlayerController.updateSettingsIfExist,
),
const SetSwitchItem(
title: '左右侧双击快退/快进',
Expand Down Expand Up @@ -141,6 +143,7 @@ class _PlaySettingState extends State<PlaySetting> {
);
if (result != null) {
setting.put(SettingBoxKey.subtitlePreference, result);
PlPlayerController.updateSettingsIfExist();
defaultSubtitlePreference = result;
setState(() {});
}
Expand Down Expand Up @@ -186,9 +189,7 @@ class _PlaySettingState extends State<PlaySetting> {
leading: Icon(MdiIcons.locationExit),
setKey: SettingBoxKey.continuePlayInBackground,
defaultVal: false,
callFn: (val) {
SmartDialog.showToast('如果该设置未生效,请重启');
},
callFn: PlPlayerController.updateSettingsIfExist,
),
const SetSwitchItem(
title: '应用内小窗',
Expand Down
2 changes: 1 addition & 1 deletion lib/pages/setting/style_setting.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class _StyleSettingState extends State<StyleSetting> {
AutoOrientation.portraitUpMode();
SmartDialog.showToast('已关闭横屏适配');
}
PlPlayerController.updateSettings();
PlPlayerController.updateSettingsIfExist();
}),
// const SetSwitchItem(
// title: '改用侧边栏',
Expand Down
125 changes: 78 additions & 47 deletions lib/plugin/pl_player/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class PlPlayerController {
final Rx<bool> _showControls = false.obs;
final Rx<bool> _showVolumeStatus = false.obs;
final Rx<bool> _showBrightnessStatus = false.obs;
final Rx<bool> _doubleSpeedStatus = false.obs;
final RxDouble _doubleSpeedStatus = 0.0.obs;
final Rx<bool> _controlsLock = false.obs;
final Rx<bool> _isFullScreen = false.obs;
// 默认投稿视频格式
Expand Down Expand Up @@ -247,8 +247,8 @@ class PlPlayerController {
/// 镜像
Rx<bool> get flipX => _flipX;

/// 是否长按倍速
Rx<bool> get doubleSpeedStatus => _doubleSpeedStatus;
/// 长按倍速值(0为非长按倍速)
RxDouble get doubleSpeedStatus => _doubleSpeedStatus;

Rx<bool> isBuffering = true.obs;

Expand Down Expand Up @@ -281,7 +281,8 @@ class PlPlayerController {
late bool massiveMode;
late List<double> speedsList;
// int? defaultDuration;
late bool enableAutoLongPressSpeed = false;
late bool enableAutoLongPressSpeed;
late bool enableLongPressSpeedIncrease;
late bool enableLongShowControl;
late bool horizontalScreen;

Expand Down Expand Up @@ -330,17 +331,11 @@ class PlPlayerController {
return _instance != null;
}

static void updateSettings() {
_instance?.horizontalScreen =
setting.get(SettingBoxKey.horizontalScreen, defaultValue: false);
}

static Future<void> playIfExists(
{bool repeat = false, bool hideControls = true}) async {
await _instance?.play(repeat: repeat, hideControls: hideControls);
}

// try to get PlayerStatus
static PlayerStatus? getPlayerStatusIfExists() {
return _instance?.playerStatus.status.value;
}
Expand All @@ -365,9 +360,11 @@ class PlPlayerController {
await _instance?.setVolume(volumeNew, videoPlayerVolume: videoPlayerVolume);
}

// 添加一个私有构造函数
PlPlayerController._() {
_videoType = videoType;
static void updateSettingsIfExist() {
_instance?.updateSettings();
}

void updateSettings() {
isOpenDanmu.value =
setting.get(SettingBoxKey.enableShowDanmaku, defaultValue: true);
blockTypes = setting.get(SettingBoxKey.danmakuBlockType, defaultValue: []);
Expand Down Expand Up @@ -403,14 +400,16 @@ class PlPlayerController {
.toDouble();
enableAutoLongPressSpeed = setting
.get(SettingBoxKey.enableAutoLongPressSpeed, defaultValue: false);
// 后台播放
_continuePlayInBackground.value = setting
.get(SettingBoxKey.continuePlayInBackground, defaultValue: false);
enableLongPressSpeedIncrease = setting
.get(SettingBoxKey.enableLongPressSpeedIncrease, defaultValue: false);
if (!enableAutoLongPressSpeed) {
_longPressSpeed.value = videoStorage
.get(VideoBoxKey.longPressSpeedDefault, defaultValue: 3.0)
.toDouble();
}
// 后台播放
_continuePlayInBackground.value = setting
.get(SettingBoxKey.continuePlayInBackground, defaultValue: false);
enableLongShowControl =
setting.get(SettingBoxKey.enableLongShowControl, defaultValue: false);
horizontalScreen =
Expand Down Expand Up @@ -439,6 +438,12 @@ class PlPlayerController {
speedsList.add(i.value);
}
speedsList.sort();
}

// 添加一个私有构造函数
PlPlayerController._() {
_videoType = videoType;
updateSettings();
// _playerEventSubs = onPlayerStatusChanged.listen((PlayerStatus status) {
// if (status == PlayerStatus.playing) {
// WakelockPlus.enable();
Expand Down Expand Up @@ -560,32 +565,8 @@ class PlPlayerController {
}
await _initializePlayer(seekTo: seekTo);
if (videoType.value != 'live' && _cid != 0) {
refreshSubtitles().then((value) {
if (_vttSubtitles.isNotEmpty) {
if (_vttSubtitlesIndex > 0 &&
_vttSubtitlesIndex < _vttSubtitles.length) {
setSubtitle(_vttSubtitlesIndex.value);
} else {
String preference = setting.get(SettingBoxKey.subtitlePreference,
defaultValue: SubtitlePreference.values.first.code);
if (preference == 'on') {
setSubtitle(1);
} else if (preference == 'withoutAi') {
bool found = false;
for (int i = 1; i < _vttSubtitles.length; i++) {
if (_vttSubtitles[i]['language']!.startsWith('ai')) {
continue;
}
found = true;
setSubtitle(i);
break;
}
if (!found) _vttSubtitlesIndex.value = 0;
} else {
_vttSubtitlesIndex.value = 0;
}
}
}
refreshSubtitles().then((_) {
chooseSubtitle();
});
}
} catch (err, stackTrace) {
Expand Down Expand Up @@ -964,7 +945,7 @@ class PlPlayerController {
// danmakuController!.updateOption(updatedOption);
// } catch (_) {}
// fix 长按倍速后放开不恢复
if (!doubleSpeedStatus.value) {
if (doubleSpeedStatus.value == 0) {
_playbackSpeed.value = speed;
}
}
Expand Down Expand Up @@ -1267,12 +1248,23 @@ class PlPlayerController {
if (controlsLock.value) {
return;
}
_doubleSpeedStatus.value = val;
if (val) {
await setPlaybackSpeed(
enableAutoLongPressSpeed ? playbackSpeed * 2 : longPressSpeed);
_doubleSpeedStatus.value =
enableAutoLongPressSpeed ? playbackSpeed * 2 : longPressSpeed;
await setPlaybackSpeed(_doubleSpeedStatus.value);
if (enableLongPressSpeedIncrease) {
Timer.periodic(const Duration(milliseconds: 500), (timer) async {
if (_doubleSpeedStatus.value > 0) {
_doubleSpeedStatus.value = min(8, _doubleSpeedStatus.value * 1.15);
await setPlaybackSpeed(_doubleSpeedStatus.value);
} else {
timer.cancel();
}
});
}
} else {
print(playbackSpeed);
print("playbackSpeed: $playbackSpeed");
_doubleSpeedStatus.value = 0;
await setPlaybackSpeed(playbackSpeed);
}
}
Expand Down Expand Up @@ -1654,6 +1646,45 @@ class PlPlayerController {
return;
}

void chooseSubtitle() {
if (_vttSubtitles.isEmpty) return;

int findSubtitleWithoutAi() {
return _vttSubtitles.indexWhere((element) {
return !element['language']!.startsWith('ai');
});
}

void setSubtitleFallback(int defaultIndex) {
int index = findSubtitleWithoutAi();
setSubtitle(index != -1 ? index : defaultIndex);
}

String preference = setting.get(SettingBoxKey.subtitlePreference,
defaultValue: SubtitlePreference.values.first.code);

if (_vttSubtitlesIndex < 1 || _vttSubtitlesIndex >= _vttSubtitles.length) {
switch (preference) {
case 'on':
setSubtitleFallback(1);
break;
case 'withoutAi':
setSubtitleFallback(0);
break;
default:
setSubtitle(0);
}
return;
}

if (_vttSubtitles[_vttSubtitlesIndex.value]['language']!.startsWith('ai')) {
setSubtitleFallback(
preference == 'withoutAi' ? 0 : _vttSubtitlesIndex.value);
} else {
setSubtitle(_vttSubtitlesIndex.value);
}
}

// 设定字幕轨道
setSubtitle(int index) {
if (index == 0) {
Expand Down
8 changes: 5 additions & 3 deletions lib/plugin/pl_player/view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -842,7 +842,7 @@ class _PLVideoPlayerState extends State<PLVideoPlayer>
translation: const Offset(0.0, 0.3), // 上下偏移量(负数向上偏移)
child: AnimatedOpacity(
curve: Curves.easeInOut,
opacity: _.doubleSpeedStatus.value ? 1.0 : 0.0,
opacity: _.doubleSpeedStatus.value > 0 ? 1.0 : 0.0,
duration: const Duration(milliseconds: 150),
child: Container(
alignment: Alignment.center,
Expand All @@ -851,10 +851,12 @@ class _PLVideoPlayerState extends State<PLVideoPlayer>
borderRadius: BorderRadius.circular(16.0),
),
height: 32.0,
width: 70.0,
width: 85.0,
child: Center(
child: Obx(() => Text(
'${_.enableAutoLongPressSpeed ? _.playbackSpeed * 2 : _.longPressSpeed}倍速中',
_.doubleSpeedStatus.value > 0
? '${_.doubleSpeedStatus.value.toStringAsFixed(2)}倍速中'
: '${_.playbackSpeed}倍速',
style: const TextStyle(
color: Colors.white, fontSize: 13),
)),
Expand Down
Loading

0 comments on commit 0fcdde0

Please sign in to comment.