现在播放页的实时效果有了

This commit is contained in:
2026-08-23 21:42:37 +08:00
parent d593ce05a1
commit f6eb1b108a
2 changed files with 178 additions and 142 deletions
+25 -40
View File
@@ -130,9 +130,6 @@ class AudioService extends ChangeNotifier {
}
}
// ============================================================
// 核心播放逻辑
// ============================================================
void _playCurrent() {
if (_currentIndex < 0 || _currentIndex >= _queue.length) {
stopPlay();
@@ -142,9 +139,6 @@ class AudioService extends ChangeNotifier {
final song = _queue[_currentIndex];
_currentSong = song;
// ⭐ 不重置任何 Notifier,完全依赖 stream 推送
// position / duration / buffered 由播放器自然更新
_startListening();
notifyListeners();
@@ -154,15 +148,12 @@ class AudioService extends ChangeNotifier {
PlaybackService().play(song.url!);
// ⭐ 兜底:主动同步一次播放器状态(解决首次加载时 stream 未推送的问题)
// ⭐ 延迟同步兜底
_syncPlayerStateDelayed();
_loadMetadataForCurrentSong();
}
// ============================================================
// Metadata 加载
// ============================================================
Future<void> _loadMetadataForCurrentSong() async {
if (_currentIndex < 0 || _currentIndex >= _queue.length) return;
final song = _queue[_currentIndex];
@@ -192,9 +183,6 @@ class AudioService extends ChangeNotifier {
}
}
// ============================================================
// 播放控制
// ============================================================
void next() {
if (_queue.isEmpty) return;
@@ -271,38 +259,39 @@ class AudioService extends ChangeNotifier {
notifyListeners();
}
// ============================================================
// 播放器状态同步(兜底机制)
// ============================================================
void _syncPlayerStateDelayed() {
_syncPlayerStateNow();
Future.delayed(const Duration(milliseconds: 200), () {
_syncPlayerStateNow();
});
Future.delayed(const Duration(milliseconds: 500), () {
_syncPlayerStateNow();
});
}
void _syncPlayerStateNow() {
// ⭐ 公开方法:供 PlayerPage 主动同步
void syncPlayerStateNow() {
final player = PlaybackService().player;
final pos = player.state.position;
final dur = player.state.duration;
final buf = player.state.buffer;
if (dur.inMilliseconds > 0 && durationNotifier.value.inMilliseconds == 0) {
durationNotifier.value = dur;
debugPrint('🎯 [AudioService] sync duration: $dur');
}
debugPrint(
'🎯 [AudioService] syncPlayerStateNow: pos=$pos, dur=$dur, buf=$buf');
if (buf.inMilliseconds > 0 && bufferedNotifier.value.inMilliseconds == 0) {
// ⭐ 无条件更新(不要只判断 == 0)
if (pos.inMilliseconds >= 0) {
positionNotifier.value = pos;
}
if (dur.inMilliseconds > 0) {
durationNotifier.value = dur;
}
if (buf.inMilliseconds >= 0) {
bufferedNotifier.value = buf;
debugPrint('🎯 [AudioService] sync buffer: $buf');
}
}
// ============================================================
// media_kit 状态监听
// ============================================================
void _syncPlayerStateDelayed() {
syncPlayerStateNow();
Future.delayed(const Duration(milliseconds: 200), () {
syncPlayerStateNow();
});
Future.delayed(const Duration(milliseconds: 500), () {
syncPlayerStateNow();
});
}
// ---- 监听 ----
void _startListening() {
if (_listening) return;
_listening = true;
@@ -327,7 +316,6 @@ class AudioService extends ChangeNotifier {
_subscriptions.add(
player.stream.duration.listen((duration) {
debugPrint('🎯 [AudioService] durationStream: $duration');
// 只接受 > 0 的值,避免 0 覆盖正确值
if (duration.inMilliseconds > 0) {
durationNotifier.value = duration;
}
@@ -355,9 +343,6 @@ class AudioService extends ChangeNotifier {
_subscriptions.clear();
}
// ============================================================
// 播放完成处理
// ============================================================
void _onPlaybackCompleted() {
if (_handlingCompletion) {
debugPrint('⚠️ [service] completed ignored: already handling');