Skip to content

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

fix(player): restart finished episodes instead of resuming at the end

Predidit6f59a62dc3e1
diff --git a/lib/pages/history/history_controller.dart b/lib/pages/history/history_controller.dart
index 270425d0..256dca3d 100644
--- a/lib/pages/history/history_controller.dart
+++ b/lib/pages/history/history_controller.dart
@@ -22,10 +22,14 @@ abstract class _HistoryController with Store {
}
Future<void> updateHistory(
- PlaybackHistoryIdentity identity, Duration progress) async {
+ PlaybackHistoryIdentity identity,
+ Duration progress, {
+ Duration duration = Duration.zero,
+ }) async {
await _historyRepository.updateHistory(
identity: identity,
progress: progress,
+ duration: duration,
);
init();
}
@@ -61,21 +65,6 @@ abstract class _HistoryController with Store {
init();
}
- Future<void> clearProgress(
- BangumiItem bangumiItem,
- String adapterName,
- int episode, {
- String entryKind = HistoryEntryKind.online,
- }) async {
- await _historyRepository.clearProgress(
- bangumiItem,
- adapterName,
- episode,
- entryKind: entryKind,
- );
- init();
- }
-
Future<void> clearAll() async {
await _historyRepository.clearAllHistories();
histories.clear();
diff --git a/lib/pages/player/controller/player_playback_controller.dart b/lib/pages/player/controller/player_playback_controller.dart
index 0db7c184..df1edf59 100644
--- a/lib/pages/player/controller/player_playback_controller.dart
+++ b/lib/pages/player/controller/player_playback_controller.dart
@@ -78,6 +78,9 @@ abstract class _PlayerPlaybackController with Store {
int buttonSkipTime = 80;
int arrowKeySkipTime = 10;
+ /// 历史记录传入的 offset
+ int startOffset = 0;
+
/// 当前超分辨率模式
@observable
SuperResolutionMode superResolutionMode = SuperResolutionMode.off;
@@ -132,6 +135,36 @@ abstract class _PlayerPlaybackController with Store {
buffer = Duration.zero;
duration = Duration.zero;
completed = false;
+ startOffset = 0;
+ }
+
+ /// 本次会话是否从距结尾 [nearEndWatchedThreshold] 以内的位置起播。
+ /// 这样的"播放完成"并非真正看完,应从头重播而非切下一集
+ bool get resumedNearEnd {
+ if (startOffset <= 0 || duration <= Duration.zero) {
+ return false;
+ }
+ return Duration(seconds: startOffset) >= duration - nearEndWatchedThreshold;
+ }
+
+ /// 从头重播当前视频。[startOffset] 在重播落地后才归零,
+ /// 避免自动连播在此期间抢先触发
+ Future<void> restartFromBeginning() async {
+ final player = mediaPlayer;
+ if (player == null) {
+ return;
+ }
+ try {
+ await player.seek(Duration.zero);
+ if (!isCurrentPlayer(player)) {
+ return;
+ }
+ await player.play();
+ startOffset = 0;
+ } catch (e) {
+ KazumiLogger()
+ .w('PlayerController: failed to restart from beginning', error: e);
+ }
}
bool get playerPlaying {
@@ -196,6 +229,7 @@ abstract class _PlayerPlaybackController with Store {
required bool Function() canInstall,
int offset = 0,
}) async {
+ startOffset = offset;
superResolutionMode = SuperResolutionMode.fromStorageValue(
GStorage.getSetting(SettingsKeys.defaultSuperResolutionMode),
);
diff --git a/lib/pages/player/player_item.dart b/lib/pages/player/player_item.dart
index aa334d9f..6f8ee526 100644
--- a/lib/pages/player/player_item.dart
+++ b/lib/pages/player/player_item.dart
@@ -1039,30 +1039,35 @@ class _PlayerItemState extends State<PlayerItem>
historyController.updateHistory(
historyIdentity,
playerController.playback.playerPosition,
+ duration: playerController.playback.playerDuration,
);
}
final playingSelection = videoPageController.playbackEpisode;
final playingRoadData =
videoPageController.roadList[playingSelection.road];
- if (playerController.playback.completed &&
- playingSelection.episode < playingRoadData.data.length &&
- !videoPageController.loading &&
- autoPlayNext) {
- final nextSelection = VideoEpisodeSelection(
- episode: playingSelection.episode + 1,
- road: playingSelection.road,
- );
- // Resolution failures surface through the controller's failed state
- // instead of silently retrying here every second.
- final nextRef = videoPageController.resolveEpisode(nextSelection);
- if (nextRef != null) {
- KazumiDialog.showToast(message: '正在加载${nextRef.displayTitle}');
+ if (playerController.playback.completed && !videoPageController.loading) {
+ if (playerController.playback.resumedNearEnd) {
+ // Completion of a stale near-end resume is not a real watch;
+ // replay from the beginning instead of advancing.
+ unawaited(playerController.playback.restartFromBeginning());
+ } else if (playingSelection.episode < playingRoadData.data.length &&
+ autoPlayNext) {
+ final nextSelection = VideoEpisodeSelection(
+ episode: playingSelection.episode + 1,
+ road: playingSelection.road,
+ );
+ // Resolution failures surface through the controller's failed state
+ // instead of silently retrying here every second.
+ final nextRef = videoPageController.resolveEpisode(nextSelection);
+ if (nextRef != null) {
+ KazumiDialog.showToast(message: '正在加载${nextRef.displayTitle}');
+ }
+ try {
+ playerTimer!.cancel();
+ } catch (_) {}
+ widget.changeEpisode(playingSelection.episode + 1,
+ currentRoad: playingSelection.road);
}
- try {
- playerTimer!.cancel();
- } catch (_) {}
- widget.changeEpisode(playingSelection.episode + 1,
- currentRoad: playingSelection.road);
}
playerController.setSyncPlayCurrentPosition();
});
diff --git a/lib/repositories/history_repository.dart b/lib/repositories/history_repository.dart
index 4a6ad9fd..e9abc281 100644
--- a/lib/repositories/history_repository.dart
+++ b/lib/repositories/history_repository.dart
@@ -1,5 +1,6 @@
import 'package:hive_ce/hive.dart';
import 'package:kazumi/services/storage/storage.dart';
+import 'package:kazumi/utils/constants.dart';
import 'package:kazumi/modules/bangumi/bangumi_item.dart';
import 'package:kazumi/modules/history/history_module.dart';
import 'package:kazumi/services/sync/history_sync_service.dart';
@@ -40,9 +41,12 @@ abstract class IHistoryRepository {
///
/// [identity] 播放历史身份
/// [progress] 观看进度
+ /// [duration] 视频总时长;距结尾 [nearEndWatchedThreshold] 以内的进度视为
+ /// 已看完,归零保存。传 [Duration.zero] 表示时长未知,不做该判断
Future<void> updateHistory({
required PlaybackHistoryIdentity identity,
required Duration progress,
+ Duration duration = Duration.zero,
});
/// 获取上次观看的进度
@@ -74,18 +78,6 @@ abstract class IHistoryRepository {
/// [history] 要删除的历史记录
Future<void> deleteHistory(History history);
- /// 清空特定集数的观看进度
- ///
- /// [bangumiItem] 番剧信息
- /// [adapterName] 适配器名称
- /// [episode] 集数
- Future<void> clearProgress(
- BangumiItem bangumiItem,
- String adapterName,
- int episode, {
- String entryKind = HistoryEntryKind.online,
- });
-
/// 清空所有历史记录
Future<void> clearAllHistories();
@@ -202,7 +194,13 @@ class HistoryRepository implements IHistoryRepository {
Future<void> updateHistory({
required PlaybackHistoryIdentity identity,
required Duration progress,
+ Duration duration = Duration.zero,
}) async {
+ // 距结尾过近视为已看完,归零保存
+ if (duration > Duration.zero &&
+ progress >= duration - nearEndWatchedThreshold) {
+ progress = Duration.zero;
+ }
await _storageCoordinator.run(() async {
try {
if (!identity.canRecord) {
@@ -353,39 +351,6 @@ class HistoryRepository implements IHistoryRepository {
});
}
- @override
- Future<void> clearProgress(
- BangumiItem bangumiItem,
- String adapterName,
- int episode, {
- String entryKind = HistoryEntryKind.online,
- }) async {
- await _storageCoordinator.run(() async {
- try {
- var history = _findHistory(adapterName, bangumiItem, entryKind);
- if (history != null && history.progresses[episode] != null) {
- final nowMs = DateTime.now().millisecondsSinceEpoch;
- history.progresses[episode]!.progress = Duration.zero;
- history.progresses[episode]!.updatedAtMs = nowMs;
- await _historiesBox.put(history.key, history);
- await _progressSyncAppender(
- history: history,
- episode: episode,
- road: history.progresses[episode]!.road,
- progressMs: 0,
- updatedAt: nowMs,
- );
- }
- } catch (e, stackTrace) {
- KazumiLogger().e(
- 'GStorage: clear progress failed. bangumi=${bangumiItem.name}, episode=$episode',
- error: e,
- stackTrace: stackTrace,
- );
- }
- });
- }
-
@override
Future<void> clearAllHistories() async {
await _storageCoordinator.run(() async {
diff --git a/lib/utils/constants.dart b/lib/utils/constants.dart
index 93bc9b4c..2a1b2829 100644
--- a/lib/utils/constants.dart
+++ b/lib/utils/constants.dart
@@ -169,6 +169,10 @@ const List<double> defaultPlaySpeedList = [
3.0,
];
+/// 距视频结尾在此阈值内视为"已看完":
+/// 保存历史时进度归零,续播起点落在其中时从头播放
+const Duration nearEndWatchedThreshold = Duration(seconds: 5);
+
const String danmakuOnSvg = '''
<svg xmlns="http://www.w3.org/2000/svg" data-pointer="none" viewBox="0 0 24 24">
<path fill="#FFFFFF" fill-rule="evenodd" d="M11.989 4.828c-.47 0-.975.004-1.515.012l-1.71-2.566a1.008 1.008 0 0 0-1.678 1.118l.999 1.5c-.681.018-1.403.04-2.164.068a4.013 4.013 0 0 0-3.83 3.44c-.165 1.15-.245 2.545-.245 4.185 0 1.965.115 3.67.35 5.116a4.012 4.012 0 0 0 3.763 3.363l.906.046c1.205.063 1.808.095 3.607.095a.988.988 0 0 0 0-1.975c-1.758 0-2.339-.03-3.501-.092l-.915-.047a2.037 2.037 0 0 1-1.91-1.708c-.216-1.324-.325-2.924-.325-4.798 0-1.563.076-2.864.225-3.904.14-.977.96-1.713 1.945-1.747 2.444-.087 4.465-.13 6.063-.131 1.598 0 3.62.044 6.064.13.96.034 1.71.81 1.855 1.814.075.524.113 1.962.141 3.065v.002c.01.342.017.65.025.88a.987.987 0 1 0 1.974-.068c-.008-.226-.016-.523-.025-.856v-.027c-.03-1.118-.073-2.663-.16-3.276-.273-1.906-1.783-3.438-3.74-3.507-.9-.032-1.743-.058-2.531-.078l1.05-1.46a1.008 1.008 0 0 0-1.638-1.177l-1.862 2.59c-.38-.004-.744-.007-1.088-.007h-.13Zm.521 4.775h-1.32v4.631h2.222v.847h-2.618v1.078h2.618l.003.678c.36.026.714.163 1.01.407h.11v-1.085h2.694v-1.078h-2.695v-.847H16.8v-4.63h-1.276a8.59 8.59 0 0 0 .748-1.42L15.183 7.8a14.232 14.232 0 0 1-.814 1.804h-1.518l.693-.308a8.862 8.862 0 0 0-.814-1.408l-1.045.352c.297.396.572.847.825 1.364Zm-4.18 3.564.154-1.485h1.98V8.294h-3.2v.98H9.33v1.43H7.472l-.308 3.453h2.277c0 1.166-.044 1.925-.12 2.277-.078.352-.386.528-.936.528-.308 0-.616-.022-.902-.055l.297 1.067.062.005c.285.02.551.04.818.04 1.001-.067 1.562-.419 1.694-1.057.11-.638.176-1.903.176-3.795h-2.2Zm7.458.11v-.858h-1.254v.858h1.254Zm-2.376-.858v.858h-1.199v-.858h1.2Zm-1.199-.946h1.2v-.902h-1.2v.902Zm2.321 0v-.902h1.254v.902h-1.254Z" clip-rule="evenodd"/>
diff --git a/test/history_repository_test.dart b/test/history_repository_test.dart
index 593ccfd3..eed0c21c 100644
--- a/test/history_repository_test.dart
+++ b/test/history_repository_test.dart
@@ -178,6 +178,53 @@ void main() {
expect(repository.getHistory('plugin', item), isNull);
});
+ test('stores zero progress when position is near the end of the video',
+ () async {
+ final repository = HistoryRepository(
+ historiesBox: historiesBox,
+ privateModeReader: () => privateMode,
+ progressSyncAppender: _noopHistorySync,
+ deleteSyncAppender: _noopDeleteSync,
+ clearSyncAppender: _noopClearSync,
+ );
+ final item = _item(5);
+ final identity = PlaybackHistoryIdentity.online(
+ bangumiItem: item,
+ pluginName: 'plugin',
+ episodeNumber: 1,
+ episodeTitle: 'EP1',
+ road: 0,
+ onlineBangumiSrc: 'https://example.com/source',
+ episodePageUrl: '/online/1',
+ );
+
+ await repository.updateHistory(
+ identity: identity,
+ progress: const Duration(minutes: 23, seconds: 57),
+ duration: const Duration(minutes: 24),
+ );
+ var history = repository.getHistory('plugin', item);
+ expect(history!.progresses[1]!.progress, Duration.zero);
+
+ await repository.updateHistory(
+ identity: identity,
+ progress: const Duration(minutes: 20),
+ duration: const Duration(minutes: 24),
+ );
+ history = repository.getHistory('plugin', item);
+ expect(history!.progresses[1]!.progress, const Duration(minutes: 20));
+
+ await repository.updateHistory(
+ identity: identity,
+ progress: const Duration(minutes: 23, seconds: 57),
+ );
+ history = repository.getHistory('plugin', item);
+ expect(
+ history!.progresses[1]!.progress,
+ const Duration(minutes: 23, seconds: 57),
+ );
+ });
+
test('serializes repository writes with snapshot reconciliation', () async {
final appendStarted = Completer<void>();
final allowAppendToFinish = Completer<void>();