1
This commit is contained in:
@@ -56,6 +56,11 @@ class FeedPlayerPool {
|
||||
/// 会让视频在后台继续出声(切到"我的"听到同一视频声音的根因)
|
||||
bool _suspended = false;
|
||||
|
||||
/// 断点续播记录:suspend 时保存当前槽位置(stop 断下载但位置不丢),
|
||||
/// rebind 重新 open 后 seek 回断点再播;用后即清
|
||||
int _resumeVideoId = -1;
|
||||
Duration _resumePosition = Duration.zero;
|
||||
|
||||
/// 最近一次 bindWindow(resume 时需等在途 open 完成再 play,避免 play 被
|
||||
/// open(play:false) 内部的 pause 覆盖导致恢复后不播)
|
||||
Future<void>? _pendingBind;
|
||||
@@ -63,7 +68,16 @@ class FeedPlayerPool {
|
||||
/// 初始化(创建 3 槽位并订阅各自进度流)
|
||||
void init() {
|
||||
for (var i = 0; i < slotCount; i++) {
|
||||
final player = _playerFactory?.call() ?? Player();
|
||||
// bufferSize 映射 mpv demuxer-max-bytes(前向缓存,默认 32MB)。
|
||||
// 压到 6MB:pause 时最多再缓冲 ~6MB(当前 HLS 片段下完即停),
|
||||
// 不会像默认配置那样暂停后还预读后续多个分片偷跑流量;
|
||||
// 恢复播放时 demuxer 缓存仍在,从暂停位置无缝继续(抖音行为)。
|
||||
final player = _playerFactory?.call() ??
|
||||
Player(
|
||||
configuration: PlayerConfiguration(
|
||||
bufferSize: 6 * 1024 * 1024,
|
||||
),
|
||||
);
|
||||
final slot = PlayerSlot(player);
|
||||
_slots.add(slot);
|
||||
_hookSlot(slot);
|
||||
@@ -132,6 +146,15 @@ class FeedPlayerPool {
|
||||
slot.videoId = item.videoId;
|
||||
slot.completeNotified = false;
|
||||
if (item.url.isNotEmpty) {
|
||||
// 断点续播:open 前设置 mpv start 属性,mpv 在文件加载完成后自动
|
||||
// 定位到断点。注意不能 open 后再复位 start——open() 发出 loadfile
|
||||
// 即返回、文件异步加载,过早复位会让 mpv 加载时读到 none 从头播。
|
||||
// 所以每次 open 前都显式设置(断点秒数或 none),无残留污染
|
||||
final resumePos =
|
||||
(i == index && item.videoId == _resumeVideoId && _resumeVideoId >= 0)
|
||||
? _resumePosition
|
||||
: Duration.zero;
|
||||
await _setMpvStart(slot.player, resumePos);
|
||||
// 经本地代理:HLS 分片 1G LRU 缓存,回看命中缓存不再下载
|
||||
await slot.player.open(Media(LocalMediaServer.I.wrap(item.url)), play: false);
|
||||
// await 期间用户可能又翻页:本次绑定已过期,退出(最新绑定会接管)
|
||||
@@ -141,33 +164,65 @@ class FeedPlayerPool {
|
||||
|
||||
// 4. 当前槽:播放(播前再校验代际与挂起态,防过期调用/后台挂起时唤醒媒体)
|
||||
if (gen != _generation || _suspended) return;
|
||||
// 断点已在 step 3 经 mpv start 属性应用,此处消费掉记录
|
||||
_resumeVideoId = -1;
|
||||
_resumePosition = Duration.zero;
|
||||
currentSlot.player.play();
|
||||
}
|
||||
|
||||
/// 挂起(切出首页底部 Tab / App 退后台 / 切走当前策略):
|
||||
/// 立即暂停当前槽,并阻止在途 bindWindow 的 open 完成后自动播放
|
||||
void suspend() {
|
||||
_suspended = true;
|
||||
/// 设置 mpv start 属性(zero 表示复位为 none)。仅 Native 平台有效,
|
||||
/// 测试注入的 fake player 无 platform,静默忽略
|
||||
Future<void> _setMpvStart(Player player, Duration pos) async {
|
||||
try {
|
||||
currentSlot.player.pause();
|
||||
final platform = player.platform;
|
||||
if (platform is NativePlayer) {
|
||||
await platform.setProperty(
|
||||
'start',
|
||||
pos > Duration.zero
|
||||
? (pos.inMilliseconds / 1000).toStringAsFixed(3)
|
||||
: 'none',
|
||||
);
|
||||
}
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
/// 恢复(切回首页 / App 回前台 / 策略切回):解除挂起;若 bind 仍在 open,
|
||||
/// 等其完成后再 play,避免 play 被 open(play:false) 的内部 pause 覆盖
|
||||
/// 挂起(切出首页底部 Tab / App 退后台 / 切走当前策略):
|
||||
/// 记录当前槽播放位置后 stop 全部槽位。mpv 的 pause 只停播放线程,
|
||||
/// demuxer 对 HLS 流仍持续预读分片(实测 demuxer-max-bytes 限不住),
|
||||
/// 只有 stop 能可靠断开下载;位置存到 _resumePosition,恢复时 rebind
|
||||
/// 重新 open + seek 续播(分片走本地代理磁盘缓存,零流量)
|
||||
void suspend() {
|
||||
_suspended = true;
|
||||
// 使在途 bindWindow 过期:其 open 完成后不得再 play
|
||||
_generation++;
|
||||
for (var s = 0; s < slotCount; s++) {
|
||||
final slot = _slots[s];
|
||||
if (slot.videoId < 0) continue;
|
||||
if (s == slotOf(_currentIndex)) {
|
||||
_resumeVideoId = slot.videoId;
|
||||
_resumePosition = slot.player.state.position;
|
||||
}
|
||||
unawaited(slot.player.stop());
|
||||
slot.videoId = -1;
|
||||
slot.completeNotified = false;
|
||||
}
|
||||
}
|
||||
|
||||
/// 恢复(切回首页 / App 回前台 / 策略切回):先等待在途 bindWindow 的 open 完成
|
||||
/// (否则 play 会被 open(play:false) 的内部 pause 覆盖导致恢复后不播),然后解除
|
||||
/// 挂起并 play。_suspended 必须在 await 之后才解除——否则等待期间 completed
|
||||
/// listener 的 auto-loop 会因为看到 _suspended=false 而提前 play
|
||||
Future<void> resume() async {
|
||||
_suspended = false;
|
||||
final pending = _pendingBind;
|
||||
if (pending != null) {
|
||||
try {
|
||||
await pending;
|
||||
} catch (_) {}
|
||||
}
|
||||
if (!_suspended) {
|
||||
try {
|
||||
currentSlot.player.play();
|
||||
} catch (_) {}
|
||||
}
|
||||
_suspended = false;
|
||||
try {
|
||||
currentSlot.player.play();
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
/// 每槽位独立订阅:≥95% 或播完触发一次 complete 回调
|
||||
@@ -223,6 +278,9 @@ class FeedPlayerPool {
|
||||
_generation++;
|
||||
// stopAll 后通常跟着 rebind 重新播放(如从登录页返回),解除挂起态
|
||||
_suspended = false;
|
||||
// 彻底重置语义:清掉 suspend 保存的断点,rebind 从头播
|
||||
_resumeVideoId = -1;
|
||||
_resumePosition = Duration.zero;
|
||||
for (final slot in _slots) {
|
||||
if (slot.videoId < 0) continue;
|
||||
unawaited(slot.player.stop());
|
||||
|
||||
@@ -57,13 +57,16 @@ class _FeedScreenState extends ConsumerState<FeedScreen>
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
/// App 退后台挂起、回前台恢复播放(suspend/resume 防在途 open 竞态)
|
||||
/// App 退后台挂起、回前台从断点续播。inactive(下拉通知栏等瞬时态)
|
||||
/// 只轻量 pause 当前槽防出声;paused(真退后台)才走 suspend(stop)
|
||||
/// 彻底断下载——避免瞬时 inactive 触发 stop+重载的画面闪烁
|
||||
@override
|
||||
void didChangeAppLifecycleState(AppLifecycleState state) {
|
||||
final notifier = ref.read(feedProvider(_strategy).notifier);
|
||||
if (state == AppLifecycleState.paused ||
|
||||
state == AppLifecycleState.inactive) {
|
||||
if (state == AppLifecycleState.paused) {
|
||||
notifier.pool.suspend();
|
||||
} else if (state == AppLifecycleState.inactive) {
|
||||
notifier.pool.pauseCurrent();
|
||||
} else if (state == AppLifecycleState.resumed) {
|
||||
// 仅在首页 Tab 可见时恢复(在"我的"等页面回前台不播)
|
||||
if (ref.read(feedTabVisibleProvider)) {
|
||||
@@ -99,11 +102,15 @@ class _FeedScreenState extends ConsumerState<FeedScreen>
|
||||
.events
|
||||
.listen(_handleEvent);
|
||||
_pageController.jumpToPage(0);
|
||||
// jumpToPage 触发新策略 bindWindow 后再 resume:等其在途 open 完成再播放,
|
||||
// 同时解除新策略池可能残留的挂起态
|
||||
// jumpToPage 后 resume + rebindCurrent:suspend 已 stop 槽位,仅 play
|
||||
// 播不起来,必须 rebind 重新 open(有断点记录则 seek 续播)
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
if (mounted) {
|
||||
unawaited(ref.read(feedProvider(_strategy).notifier).pool.resume());
|
||||
unawaited(() async {
|
||||
final n = ref.read(feedProvider(_strategy).notifier);
|
||||
await n.pool.resume();
|
||||
await n.rebindCurrent();
|
||||
}());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -48,6 +48,10 @@ class _FeedVideoPageState extends ConsumerState<FeedVideoPage> {
|
||||
int _likeBurstTrigger = 0;
|
||||
Map<String, dynamic>? _seriesData;
|
||||
|
||||
/// 清屏模式:隐藏右侧互动栏与底部信息区,只留画面和进度条。
|
||||
/// 单击屏幕仍是暂停/继续;恢复显示只能通过长按菜单的「恢复清屏」
|
||||
bool _cleanMode = false;
|
||||
|
||||
static const _rates = [1.0, 1.5, 2.0, 3.0];
|
||||
|
||||
@override
|
||||
@@ -174,6 +178,25 @@ class _FeedVideoPageState extends ConsumerState<FeedVideoPage> {
|
||||
},
|
||||
),
|
||||
|
||||
// 清屏 / 恢复清屏:隐藏右侧互动栏与底部信息,只留进度条。
|
||||
// 恢复只能从长按菜单进入,避免单击误触把界面弹回来
|
||||
ListTile(
|
||||
contentPadding: EdgeInsets.zero,
|
||||
leading: Icon(_cleanMode
|
||||
? Icons.fullscreen
|
||||
: Icons.fullscreen_exit),
|
||||
title: Text(_cleanMode ? '恢复清屏' : '清屏'),
|
||||
subtitle: Text(_cleanMode
|
||||
? '重新显示点赞、评论、收藏、作者与标题'
|
||||
: '隐藏点赞、评论、收藏、作者与标题'),
|
||||
onTap: () {
|
||||
Navigator.of(sheetContext).pop();
|
||||
setState(() => _cleanMode = !_cleanMode);
|
||||
showTopToast(
|
||||
context, _cleanMode ? '已清屏,长按视频可恢复' : '已恢复显示');
|
||||
},
|
||||
),
|
||||
|
||||
// 下载(已下载/下载中置灰)
|
||||
Builder(builder: (_) {
|
||||
final dm = ref.read(downloadManagerProvider);
|
||||
@@ -341,6 +364,7 @@ class _FeedVideoPageState extends ConsumerState<FeedVideoPage> {
|
||||
Widget build(BuildContext context) {
|
||||
return GestureDetector(
|
||||
behavior: HitTestBehavior.opaque,
|
||||
// 单击始终暂停/继续(清屏态也一样),恢复清屏需走长按菜单
|
||||
onTap: widget.pool.toggleCurrent,
|
||||
onDoubleTap: _onDoubleTap,
|
||||
onLongPress: _onLongPress,
|
||||
@@ -349,23 +373,27 @@ class _FeedVideoPageState extends ConsumerState<FeedVideoPage> {
|
||||
child: Stack(
|
||||
fit: StackFit.expand,
|
||||
children: [
|
||||
// 底部渐变(提升文字可读性)
|
||||
const Positioned(
|
||||
// 底部渐变(提升文字可读性):清屏时一并淡出
|
||||
Positioned(
|
||||
left: 0, right: 0, bottom: 0, height: 160,
|
||||
child: IgnorePointer(
|
||||
child: DecoratedBox(
|
||||
decoration: BoxDecoration(
|
||||
gradient: LinearGradient(
|
||||
begin: Alignment.topCenter,
|
||||
end: Alignment.bottomCenter,
|
||||
colors: [Colors.transparent, Colors.black54],
|
||||
child: AnimatedOpacity(
|
||||
opacity: _cleanMode ? 0 : 1,
|
||||
duration: const Duration(milliseconds: 200),
|
||||
child: const DecoratedBox(
|
||||
decoration: BoxDecoration(
|
||||
gradient: LinearGradient(
|
||||
begin: Alignment.topCenter,
|
||||
end: Alignment.bottomCenter,
|
||||
colors: [Colors.transparent, Colors.black54],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
// 播放器(含抖音风格细进度条)
|
||||
// 播放器(含抖音风格细进度条)——清屏时仍保留
|
||||
VideoPlayerView(
|
||||
pool: widget.pool,
|
||||
slotIndex: widget.slotIndex,
|
||||
@@ -380,54 +408,60 @@ class _FeedVideoPageState extends ConsumerState<FeedVideoPage> {
|
||||
// 右侧互动栏(头像/点赞/评论/收藏 — 分享已移至长按菜单)
|
||||
Positioned(
|
||||
right: 8, bottom: 96,
|
||||
child: InteractionBar(
|
||||
item: widget.item,
|
||||
onLike: widget.onLike,
|
||||
onFavorite: widget.onFavorite,
|
||||
onComment: widget.onComment,
|
||||
child: _HidableOverlay(
|
||||
hidden: _cleanMode,
|
||||
child: InteractionBar(
|
||||
item: widget.item,
|
||||
onLike: widget.onLike,
|
||||
onFavorite: widget.onFavorite,
|
||||
onComment: widget.onComment,
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
// 左下信息区 + 系列标题(系列标题在信息区上方,不重叠)
|
||||
Positioned(
|
||||
left: 12, right: 88, bottom: 24,
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
// 系列标题(当前视频属于系列时显示,点击弹出系列列表)
|
||||
if (_hasSeries)
|
||||
GestureDetector(
|
||||
onTap: _showSeriesSheet,
|
||||
child: Container(
|
||||
margin: const EdgeInsets.only(bottom: 8),
|
||||
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 6),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.black54,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
const Icon(Icons.playlist_play, color: Colors.white, size: 16),
|
||||
const SizedBox(width: 4),
|
||||
Text(
|
||||
_seriesTitle,
|
||||
style: const TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 13,
|
||||
fontWeight: FontWeight.w600,
|
||||
child: _HidableOverlay(
|
||||
hidden: _cleanMode,
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
// 系列标题(当前视频属于系列时显示,点击弹出系列列表)
|
||||
if (_hasSeries)
|
||||
GestureDetector(
|
||||
onTap: _showSeriesSheet,
|
||||
child: Container(
|
||||
margin: const EdgeInsets.only(bottom: 8),
|
||||
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 6),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.black54,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
const Icon(Icons.playlist_play, color: Colors.white, size: 16),
|
||||
const SizedBox(width: 4),
|
||||
Text(
|
||||
_seriesTitle,
|
||||
style: const TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 13,
|
||||
fontWeight: FontWeight.w600,
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 2),
|
||||
const Icon(Icons.chevron_right, color: Colors.white, size: 14),
|
||||
],
|
||||
const SizedBox(width: 2),
|
||||
const Icon(Icons.chevron_right, color: Colors.white, size: 14),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
// 视频信息(昵称/标题/描述)
|
||||
VideoInfoSection(item: widget.item),
|
||||
],
|
||||
// 视频信息(昵称/标题/描述)
|
||||
VideoInfoSection(item: widget.item),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
@@ -436,3 +470,24 @@ class _FeedVideoPageState extends ConsumerState<FeedVideoPage> {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// 可隐藏浮层:淡出 + 禁用点击。隐藏时仍占位但不拦截手势,
|
||||
/// 保证点击/长按能穿透到下层的手势层
|
||||
class _HidableOverlay extends StatelessWidget {
|
||||
const _HidableOverlay({required this.hidden, required this.child});
|
||||
|
||||
final bool hidden;
|
||||
final Widget child;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return IgnorePointer(
|
||||
ignoring: hidden,
|
||||
child: AnimatedOpacity(
|
||||
opacity: hidden ? 0 : 1,
|
||||
duration: const Duration(milliseconds: 200),
|
||||
child: child,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,8 +28,9 @@ class _HomeShellState extends ConsumerState<HomeShell> {
|
||||
|
||||
void _onTabSelected(int i) {
|
||||
if (i == _index) return;
|
||||
// 切到非首页:挂起所有 Feed 策略的播放器池(IndexedStack 保活,
|
||||
// suspend 会同时阻止在途 open 完成后自动播放,杜绝后台幽灵声)
|
||||
// 切到非首页:挂起所有 Feed 策略池——suspend 记录断点后 stop 全槽位
|
||||
// (mpv pause 停不住 HLS 预读,只有 stop 能可靠断下载);
|
||||
// 回首页 rebind 重新 open + seek 断点续播,缓存分片零流量(抖音行为)
|
||||
if (i != 0) {
|
||||
for (final s in _feedStrategies) {
|
||||
ref.read(feedProvider(s).notifier).pool.suspend();
|
||||
|
||||
+1
-1
@@ -2,7 +2,7 @@ name: video_app
|
||||
description: "短视频应用(Flutter,iOS/Android 双端)"
|
||||
publish_to: 'none'
|
||||
|
||||
version: 1.3.7+23
|
||||
version: 1.3.15+31
|
||||
|
||||
environment:
|
||||
sdk: ^3.9.2
|
||||
|
||||
Reference in New Issue
Block a user