Skip to content

基于自定义规则的番剧采集APP,支持流媒体在线观看,支持弹幕,支持实时超分辨率。

feat(player): pause playback while editing danmaku (#2711) (#2711)

JesseLee9a20a019c045
diff --git a/lib/pages/player/player_item_panel.dart b/lib/pages/player/player_item_panel.dart
index 41df01bb..d63d4a09 100644
--- a/lib/pages/player/player_item_panel.dart
+++ b/lib/pages/player/player_item_panel.dart
@@ -14,6 +14,7 @@ import 'package:kazumi/pages/player/controller/player_super_resolution.dart';
import 'package:kazumi/bean/widget/embedded_native_control_area.dart';
import 'package:kazumi/pages/player/player_panel_hold.dart';
import 'package:kazumi/services/player/pip_utils.dart';
+import 'package:kazumi/services/storage/storage.dart';
import 'package:kazumi/pages/video/video_controller.dart';
import 'package:kazumi/bean/dialog/dialog_helper.dart';
import 'package:kazumi/pages/player/player_controller.dart';
@@ -98,6 +99,14 @@ class _PlayerItemPanelState extends State<PlayerItemPanel> {
late final PlayerController playerController;
final TextEditingController textController = TextEditingController();
final FocusNode textFieldFocus = FocusNode();
+ int? _danmakuPauseTick;
+
+ /// Whether this editor currently owns a pause. The sleep timer bumps
+ /// [TimedShutdownService.expiryTicks] when it actually expires, which revokes
+ /// the ownership even if the panel was rebuilt in between.
+ bool get _pausedByDanmakuEdit =>
+ _danmakuPauseTick == TimedShutdownService().expiryTicks.value;
+ bool _holdResumeForDestinationSheet = false;
String? _cachedDanmakuColorHex;
late Widget _cachedDanmakuOnIcon;
@@ -117,21 +126,30 @@ class _PlayerItemPanelState extends State<PlayerItemPanel> {
@override
void dispose() {
+ textFieldFocus.removeListener(_onDanmakuEditFocusChanged);
textController.dispose();
textFieldFocus.dispose();
super.dispose();
}
Future<void> _submitDanmakuText(String message) async {
+ _holdResumeForDestinationSheet = true;
textFieldFocus.unfocus();
if (message.trim().isEmpty) {
KazumiDialog.showToast(message: '弹幕内容为空');
+ _holdResumeForDestinationSheet = false;
+ _onDanmakuEditFocusChanged();
return;
}
final destination = await showDanmakuDestinationSheet(context);
- if (!mounted || destination == null) {
+ _holdResumeForDestinationSheet = false;
+ if (!mounted) {
+ return;
+ }
+ _onDanmakuEditFocusChanged();
+ if (destination == null) {
return;
}
@@ -171,6 +189,46 @@ class _PlayerItemPanelState extends State<PlayerItemPanel> {
textController.clear();
}
+ // A SyncPlay room is left alone: a local pause there is either broadcast to
+ // everyone or undone by the next server state message.
+ void _onDanmakuEditFocusChanged() {
+ if (playerController.syncplay.hasSession) return;
+ if (textFieldFocus.hasFocus) {
+ if (GStorage.getSetting<bool>(SettingsKeys.pauseOnDanmakuEdit) &&
+ playerController.playback.playing) {
+ _danmakuPauseTick = TimedShutdownService().expiryTicks.value;
+ unawaited(playerController.pause(enableSync: false));
+ }
+ } else if (_canResumeAfterDanmakuEdit) {
+ _danmakuPauseTick = null;
+ unawaited(playerController.play(enableSync: false));
+ }
+ }
+
+ /// Only undoes a pause this editor created, and only when nothing else owns
+ /// playback: no destination sheet, no app in the background (desktop window
+ /// switches report inactive), and no dialog on top.
+ bool get _canResumeAfterDanmakuEdit =>
+ _pausedByDanmakuEdit &&
+ !_holdResumeForDestinationSheet &&
+ (WidgetsBinding.instance.lifecycleState ?? AppLifecycleState.resumed) ==
+ AppLifecycleState.resumed &&
+ (ModalRoute.of(context)?.isCurrent ?? false);
+
+
+ // Keeps the tap that dismisses the composer away from the video and the play
+ // button. The region must stay in the text field's group, or the paste/copy/
+ // select-all toolbar stops responding.
+ Widget _danmakuEditBarrier(Widget child) {
+ return ListenableBuilder(
+ listenable: textFieldFocus,
+ builder: (context, _) => TextFieldTapRegion(
+ consumeOutsideTaps: textFieldFocus.hasFocus && _pausedByDanmakuEdit,
+ child: child,
+ ),
+ );
+ }
+
Widget get _danmakuTextField {
return Observer(builder: (context) {
return PlayerPanelHoldFocusRegion(
@@ -284,6 +342,7 @@ class _PlayerItemPanelState extends State<PlayerItemPanel> {
void initState() {
super.initState();
playerController = widget.playerController;
+ textFieldFocus.addListener(_onDanmakuEditFocusChanged);
final visibility = widget.panelVisibilityController
.drive(CurveTween(curve: Curves.easeInOut));
_topOffsetAnimation = visibility.drive(Tween<Offset>(
@@ -614,7 +673,8 @@ class _PlayerItemPanelState extends State<PlayerItemPanel> {
children: [
_buildDanmakuToggleButton(context),
_danmakuSettingsButton,
- if (constraints.maxWidth > 600) _danmakuTextField,
+ if (constraints.maxWidth > 600)
+ _danmakuEditBarrier(_danmakuTextField),
],
),
),
@@ -643,7 +703,7 @@ class _PlayerItemPanelState extends State<PlayerItemPanel> {
),
if (playerController.danmaku.danmakuOn) ...[
_danmakuSettingsButton,
- Expanded(child: _danmakuTextField),
+ Expanded(child: _danmakuEditBarrier(_danmakuTextField)),
] else
const Spacer(),
],
diff --git a/lib/pages/settings/danmaku/danmaku_settings.dart b/lib/pages/settings/danmaku/danmaku_settings.dart
index d2500327..69252772 100644
--- a/lib/pages/settings/danmaku/danmaku_settings.dart
+++ b/lib/pages/settings/danmaku/danmaku_settings.dart
@@ -28,6 +28,7 @@ class _DanmakuSettingsPageState extends State<DanmakuSettingsPage> {
late bool danmakuColor;
late bool danmakuMassive;
late bool danmakuDeduplication;
+ late bool pauseOnDanmakuEdit;
late bool danmakuBiliBiliSource;
late bool danmakuGamerSource;
late bool danmakuDanDanSource;
@@ -63,6 +64,8 @@ class _DanmakuSettingsPageState extends State<DanmakuSettingsPage> {
danmakuMassive = GStorage.getSetting(SettingsKeys.danmakuMassive);
danmakuDeduplication =
GStorage.getSetting<bool>(SettingsKeys.danmakuDeduplication);
+ pauseOnDanmakuEdit =
+ GStorage.getSetting<bool>(SettingsKeys.pauseOnDanmakuEdit);
danmakuBiliBiliSource =
GStorage.getSetting<bool>(SettingsKeys.danmakuBiliBiliSource);
danmakuGamerSource =
@@ -310,6 +313,18 @@ class _DanmakuSettingsPageState extends State<DanmakuSettingsPage> {
description: Text('相同内容弹幕过多时合并为一条弹幕'),
initialValue: danmakuDeduplication,
),
+ SettingsTile.switchTile(
+ leading: Icons.pause_circle_outline_rounded,
+ onToggle: (value) async {
+ pauseOnDanmakuEdit = value ?? !pauseOnDanmakuEdit;
+ await GStorage.putSetting<bool>(
+ SettingsKeys.pauseOnDanmakuEdit, pauseOnDanmakuEdit);
+ setState(() {});
+ },
+ title: Text('编辑弹幕时暂停播放'),
+ description: Text('聚焦弹幕输入框时暂停,输入结束后继续;一起看房间内不生效'),
+ initialValue: pauseOnDanmakuEdit,
+ ),
],
),
SettingsSection(
diff --git a/lib/services/player/timed_shutdown_service.dart b/lib/services/player/timed_shutdown_service.dart
index 01f9fde8..39eb9d19 100644
--- a/lib/services/player/timed_shutdown_service.dart
+++ b/lib/services/player/timed_shutdown_service.dart
@@ -17,6 +17,10 @@ class TimedShutdownService {
VoidCallback? _onExpiredCallback;
final ValueNotifier<int> remainingSecondsNotifier = ValueNotifier<int>(0);
+
+ /// Bumped every time the timer actually expires, so the mounted player panel
+ /// can drop its resume ownership without the service holding a stale widget.
+ final ValueNotifier<int> expiryTicks = ValueNotifier<int>(0);
final ValueNotifier<int> setMinutesNotifier = ValueNotifier<int>(0);
bool get isActive => _shutdownTimer != null && _shutdownTimer!.isActive;
@@ -62,6 +66,7 @@ class TimedShutdownService {
void _onTimerExpired() {
setMinutesNotifier.value = 0;
+ expiryTicks.value++;
try {
_onExpiredCallback?.call();
} catch (e) {
diff --git a/lib/services/storage/settings_keys.dart b/lib/services/storage/settings_keys.dart
index 54e262f3..aacf6a3e 100644
--- a/lib/services/storage/settings_keys.dart
+++ b/lib/services/storage/settings_keys.dart
@@ -1,5 +1,12 @@
+import 'package:flutter/foundation.dart';
import 'package:kazumi/services/player/syncplay_endpoint.dart';
+/// Desktop platforms (Windows/macOS/Linux), used for setting defaults.
+bool get _isDesktopPlatform =>
+ defaultTargetPlatform == TargetPlatform.windows ||
+ defaultTargetPlatform == TargetPlatform.macOS ||
+ defaultTargetPlatform == TargetPlatform.linux;
+
enum SettingGroup {
player,
danmaku,
@@ -102,6 +109,13 @@ class SettingsKeys {
true,
group: SettingGroup.danmaku,
);
+ /// Off on desktop and on on mobile, like the B 站 client.
+ static final pauseOnDanmakuEdit = SettingKey<bool>(
+ 'pauseOnDanmakuEdit',
+ true,
+ group: SettingGroup.danmaku,
+ defaultResolver: (_) => !_isDesktopPlatform,
+ );
static const danmakuBorder = SettingKey<bool>(
_SettingBoxKey.danmakuBorder,
true,
@@ -576,6 +590,7 @@ class SettingsKeys {
buttonSkipTime,
arrowKeySkipTime,
danmakuEnhance,
+ pauseOnDanmakuEdit,
danmakuBorder,
danmakuBorderSize,
danmakuOpacity,