fix(player): 分离首页与播放页播放上下文
This commit is contained in:
1 parent
440188f261
commit
6667330ce0
8 files changed
+312
-362
No files matched your search
@@ -192,6 +192,7 @@ class PlayerLogic extends ChangeNotifier {
|
||||
void start({
|
||||
required PlayerFactory factory,
|
||||
required PlaybackCoordinator coordinator,
|
||||
bool initialUserWantsPlay = true,
|
||||
}) {
|
||||
_started = true;
|
||||
_sessionFactory = factory;
|
||||
@@ -209,69 +210,41 @@ class PlayerLogic extends ChangeNotifier {
|
||||
session.setRouteVisible(false);
|
||||
}
|
||||
unawaited(loadResumePoint());
|
||||
unawaited(selectEpisode(initialIndex));
|
||||
}
|
||||
|
||||
/// 独立播放页把 feed 的“剧索引”切换成 series 的“集索引”,保留 active player。
|
||||
void adoptSeriesHandoff({
|
||||
required Drama drama,
|
||||
required List<Episode> seriesEpisodes,
|
||||
required int episodeIndex,
|
||||
}) {
|
||||
if (episodeIndex < 0 || episodeIndex >= seriesEpisodes.length) return;
|
||||
homeMode = false;
|
||||
embedded = false;
|
||||
feedDramas = null;
|
||||
pageDrama = drama;
|
||||
initialIndex = episodeIndex;
|
||||
queue = PlaybackQueue.series(drama);
|
||||
visibleIndices = List<int>.generate(queue.length, (i) => i);
|
||||
sessionEpisodes
|
||||
..clear()
|
||||
..addAll(seriesEpisodes);
|
||||
session.remapQueuePreservingActive(
|
||||
List<Episode>.of(seriesEpisodes),
|
||||
currentIndex: episodeIndex,
|
||||
notify: false,
|
||||
preloadIndex: episodeIndex + 1 < seriesEpisodes.length
|
||||
? episodeIndex + 1
|
||||
: null,
|
||||
previousIndex: episodeIndex > 0 ? episodeIndex - 1 : null,
|
||||
unawaited(
|
||||
selectEpisode(
|
||||
initialIndex,
|
||||
userWantsPlay: initialUserWantsPlay ? null : false,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// 返回推荐页时恢复剧维度 queue,并把当前集写回该剧的 feed 条目。
|
||||
void restoreFeedHandoff({
|
||||
required PlayerPage page,
|
||||
required PlaybackQueue sourceQueue,
|
||||
required List<Episode> sourceEpisodes,
|
||||
required int sourceIndex,
|
||||
}) {
|
||||
if (sourceIndex < 0 || sourceIndex >= sourceQueue.length) return;
|
||||
final currentEpisode = session.currentEpisode;
|
||||
final feedEpisodes = List<Episode>.of(sourceEpisodes);
|
||||
feedEpisodes[sourceIndex] = currentEpisode;
|
||||
final entries = List<PlaybackEntry>.of(sourceQueue.entries);
|
||||
entries[sourceIndex] = PlaybackEntry(
|
||||
entries[sourceIndex].drama,
|
||||
currentEpisode,
|
||||
);
|
||||
feedDramas = page.feedDramas;
|
||||
pageDrama = page.drama;
|
||||
homeMode = page.homeMode;
|
||||
embedded = page.embedded;
|
||||
initialIndex = sourceIndex;
|
||||
queue = PlaybackQueue.fromEntries(entries);
|
||||
visibleIndices = List<int>.generate(queue.length, (i) => i);
|
||||
sessionEpisodes
|
||||
..clear()
|
||||
..addAll(feedEpisodes);
|
||||
session.remapQueuePreservingActive(
|
||||
List<Episode>.of(feedEpisodes),
|
||||
currentIndex: sourceIndex,
|
||||
preloadIndex: nextVisibleIndex(sourceIndex),
|
||||
previousIndex: previousVisibleIndex(sourceIndex),
|
||||
);
|
||||
/// 独立播放页覆盖首页时只暂停并保存进度,首页 session 与 controller 保持存活。
|
||||
Future<void> pauseForFullPlayer() async {
|
||||
if (!_started) return;
|
||||
cancelScrub();
|
||||
await session.pauseForRouteHandoff();
|
||||
await saveCurrentProgress();
|
||||
}
|
||||
|
||||
/// 返回首页前把仍存活的 feed controller 定位到第 1 集起点。
|
||||
Future<void> prepareFeedResume() async {
|
||||
if (!_started) return;
|
||||
await session.playbackSettled;
|
||||
final player = session.active;
|
||||
if (player is VideoPlayerPort && player.controller.value.isInitialized) {
|
||||
await player.seekTo(Duration.zero);
|
||||
}
|
||||
}
|
||||
|
||||
/// 独立播放页关闭并恢复首页路由后,等原生播放器实际恢复再结束交接。
|
||||
Future<void> resumeAfterFullPlayer() async {
|
||||
if (!_started) return;
|
||||
if (visibleIndices.isNotEmpty) {
|
||||
await session.resumeForRouteHandoff();
|
||||
} else {
|
||||
session.setRouteVisible(false);
|
||||
await session.playbackSettled;
|
||||
}
|
||||
}
|
||||
|
||||
/// 串行替换首页 feed 队列;频道变化时重置到第 1 个,刷新时尽量保留当前剧。
|
||||
@@ -310,6 +283,7 @@ class PlayerLogic extends ChangeNotifier {
|
||||
activate: activatePlayer,
|
||||
coordinator: coordinator,
|
||||
waitForFrame: _waitForFrame,
|
||||
warmNeighborsEnabled: !homeMode,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1053,7 +1027,11 @@ class PlayerLogic extends ChangeNotifier {
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> selectEpisode(int index, {int? revision}) async {
|
||||
Future<void> selectEpisode(
|
||||
int index, {
|
||||
int? revision,
|
||||
bool? userWantsPlay,
|
||||
}) async {
|
||||
final feedRevisionAtStart = revision ?? feedRevision;
|
||||
final request = ++pageRequest;
|
||||
cancelScrub();
|
||||
@@ -1088,7 +1066,7 @@ class PlayerLogic extends ChangeNotifier {
|
||||
return;
|
||||
}
|
||||
if (!ready) return;
|
||||
await _selectVisible(index);
|
||||
await _selectVisible(index, userWantsPlay: userWantsPlay);
|
||||
}
|
||||
|
||||
/// 收费集只有在本会话确认可播或解锁成功后才解除门禁。
|
||||
@@ -1098,11 +1076,12 @@ class PlayerLogic extends ChangeNotifier {
|
||||
}
|
||||
|
||||
/// 按可见队列选择一集,并预热上一集与下一集。
|
||||
Future<void> _selectVisible(int index) {
|
||||
Future<void> _selectVisible(int index, {bool? userWantsPlay}) {
|
||||
return session.select(
|
||||
index,
|
||||
preloadIndex: nextVisibleIndex(index),
|
||||
previousIndex: previousVisibleIndex(index),
|
||||
userWantsPlay: userWantsPlay,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1237,9 +1216,7 @@ class PlayerLogic extends ChangeNotifier {
|
||||
Drama drama,
|
||||
int index,
|
||||
) async {
|
||||
session.setRouteVisible(false);
|
||||
await session.playbackSettled;
|
||||
await saveCurrentProgress();
|
||||
await pauseForFullPlayer();
|
||||
if (!context.mounted) return;
|
||||
await Navigator.of(context).push(
|
||||
FadePageRoute<void>(
|
||||
@@ -1248,10 +1225,13 @@ class PlayerLogic extends ChangeNotifier {
|
||||
initialIndex: index,
|
||||
wallet: wallet,
|
||||
downloads: downloads,
|
||||
isolatePlaybackSession: true,
|
||||
),
|
||||
),
|
||||
);
|
||||
if (isMounted()) session.setRouteVisible(active);
|
||||
if (!isMounted()) return;
|
||||
await prepareFeedResume();
|
||||
await resumeAfterFullPlayer();
|
||||
}
|
||||
|
||||
Future<void> showEpisodes(BuildContext context) async {
|
||||
|
||||
Reference in new issue
Block a user