This commit is contained in:
lzcode
2026-10-16 20:03:58 +08:00
parent 9962136fc8
commit ebad02ce31
19 changed files with 622 additions and 240 deletions
+23 -18
View File
@@ -66,25 +66,33 @@ class FeedPlayerPool {
/// 绑定窗口:index 为当前页;items 为已解析好播放地址的窗口数据源
Future<void> bindWindow(int index, List<({int videoId, String url})> items) async {
// 1. 先把**所有**出窗/旧槽位强制停掉(不保留任何正在播放的声音)
// 只允许窗口内 [index-1, index, index+1] 这 3 个槽位持有资源
// 1. 先切换当前页指针(currentSlot / isCurrentPlaying / 完成回调都依赖它)
_currentIndex = index;
// 窗口内应保留的槽位集合 [index-1, index, index+1]
final activeSet = <int>{};
for (var offset = -1; offset <= 1; offset++) {
final i = index + offset;
if (i >= 0 && i < items.length) activeSet.add(slotOf(i));
}
// 2. 立即静音(同步执行,不等后续网络 open):
// - 出窗槽位:stop(断音 + 清缓存),并复位
// - 窗口内槽位:全部 pause(含刚划走的上一页,防止切页期间串音)
// 这一步必须早于任何 await,否则 open 新视频期间旧视频仍在出声
for (var s = 0; s < slotCount; s++) {
if (activeSet.contains(s)) continue;
final slot = _slots[s];
if (slot.videoId >= 0) {
// 确保旧视频彻底静音 + 停止解码(libmpv stop 不会立即断音 + 清缓存)
try { await slot.player.stop(); } catch (_) {}
if (slot.videoId < 0) continue;
if (activeSet.contains(s)) {
try { slot.player.pause(); } catch (_) {}
} else {
unawaited(slot.player.stop());
slot.videoId = -1;
slot.completeNotified = false;
}
}
// 2. 窗口内槽位按需加载(open 完才 play)
// 3. 窗口内槽位按需加载(open 完才 play)
for (var offset = -1; offset <= 1; offset++) {
final i = index + offset;
if (i < 0 || i >= items.length) continue;
@@ -98,17 +106,6 @@ class FeedPlayerPool {
}
}
// 3. 窗口内非当前:pause 预缓冲(但确保不出声)
for (var offset = -1; offset <= 1; offset++) {
final i = index + offset;
if (i < 0 || i >= items.length) continue;
final slot = _slots[slotOf(i)];
if (i == index) continue; // 当前页留到最后统一 play
try {
slot.player.pause();
} catch (_) {}
}
// 4. 当前槽:播放
currentSlot.player.play();
}
@@ -150,6 +147,14 @@ class FeedPlayerPool {
void playCurrent() => currentSlot.player.play();
void toggleCurrent() => currentSlot.player.playOrPause();
/// 翻页瞬间静音:暂停除目标槽位外的所有槽位,防止多个视频同时出声
void pauseExcept({required int slotIndex}) {
for (var i = 0; i < slotCount; i++) {
if (i == slotIndex) continue;
try { _slots[i].player.pause(); } catch (_) {}
}
}
/// 释放全部资源(Feed 页 dispose 时调用)
Future<void> dispose() async {
for (final sub in _subs) {