现在的问题比较大,因为metadata的引入导致原有的部分播放进度信息推送错误

This commit is contained in:
2026-08-23 20:29:31 +08:00
parent 9a60441e04
commit d593ce05a1
9 changed files with 203 additions and 167 deletions
+59 -33
View File
@@ -3,7 +3,7 @@ import 'dart:async';
import 'package:flutter/material.dart';
import 'package:media_kit/media_kit.dart';
import 'playback_service.dart';
import '../metadata/metadata_service.dart'; // ⭐ 新增
import '../metadata/metadata_service.dart';
enum PlayMode {
sequential,
@@ -30,7 +30,7 @@ class AudioService extends ChangeNotifier {
factory AudioService() => _instance;
AudioService._internal();
// ---- 基础状态(低频,触发 UI 重建) ----
// ---- 基础状态 ----
Song? _currentSong;
bool _isPlaying = false;
PlayMode _playMode = PlayMode.sequential;
@@ -41,7 +41,7 @@ class AudioService extends ChangeNotifier {
List<int> _shuffledIndices = [];
int _shuffledIndex = -1;
// ---- 高频进度(用 ValueNotifier,不触发全局重建) ----
// ---- 高频进度 ----
final ValueNotifier<Duration> positionNotifier = ValueNotifier(Duration.zero);
final ValueNotifier<Duration> durationNotifier = ValueNotifier(Duration.zero);
final ValueNotifier<Duration> bufferedNotifier = ValueNotifier(Duration.zero);
@@ -49,13 +49,10 @@ class AudioService extends ChangeNotifier {
bool _listening = false;
final List<StreamSubscription> _subscriptions = [];
// ⭐ 防重入标志
bool _handlingCompletion = false;
// ⭐ 歌曲切换回调(用于通知 Handler 更新 MediaItem
void Function(Song)? _onSongChanged;
// ---- Getter(高频字段不走 ChangeNotifier ----
// ---- Getter ----
Song? get currentSong => _currentSong;
bool get isPlaying => _isPlaying;
PlayMode get playMode => _playMode;
@@ -63,7 +60,6 @@ class AudioService extends ChangeNotifier {
int get currentIndex => _currentIndex;
bool get hasQueue => _queue.isNotEmpty;
// ---- 兼容旧代码:提供 getter 返回 ValueNotifier 的值 ----
Duration get position => positionNotifier.value;
Duration get duration => durationNotifier.value;
Duration get bufferedPosition => bufferedNotifier.value;
@@ -79,12 +75,10 @@ class AudioService extends ChangeNotifier {
}
}
// ---- 注册歌曲切换回调 ----
void setOnSongChanged(void Function(Song) callback) {
_onSongChanged = callback;
}
// ---- 切换播放模式 ----
void togglePlayMode() {
switch (_playMode) {
case PlayMode.sequential:
@@ -100,7 +94,6 @@ class AudioService extends ChangeNotifier {
notifyListeners();
}
// ---- 设置播放队列 ----
void setQueue(List<Song> queue, {int startIndex = 0}) {
if (queue.isEmpty) {
_clearQueue();
@@ -129,7 +122,6 @@ class AudioService extends ChangeNotifier {
stopPlay();
}
// ---- 播放指定歌曲 ----
Future<void> playSong(Song song) async {
if (_queue.isEmpty || _queue[_currentIndex].id != song.id) {
setQueue([song], startIndex: 0);
@@ -138,6 +130,9 @@ class AudioService extends ChangeNotifier {
}
}
// ============================================================
// 核心播放逻辑
// ============================================================
void _playCurrent() {
if (_currentIndex < 0 || _currentIndex >= _queue.length) {
stopPlay();
@@ -147,13 +142,8 @@ class AudioService extends ChangeNotifier {
final song = _queue[_currentIndex];
_currentSong = song;
// ⭐ 切歌时触发回调(通知 Handler 更新 MediaItem
_onSongChanged?.call(song);
// ⭐ 重置进度(用 ValueNotifier
positionNotifier.value = Duration.zero;
durationNotifier.value = Duration.zero;
bufferedNotifier.value = Duration.zero;
// ⭐ 不重置任何 Notifier,完全依赖 stream 推送
// position / duration / buffered 由播放器自然更新
_startListening();
notifyListeners();
@@ -164,27 +154,27 @@ class AudioService extends ChangeNotifier {
PlaybackService().play(song.url!);
// ⭐ 异步加载 metadata(不阻塞播放
// ⭐ 兜底:主动同步一次播放器状态(解决首次加载时 stream 未推送的问题
_syncPlayerStateDelayed();
_loadMetadataForCurrentSong();
}
// ⭐ 新增:加载当前歌曲的 metadata
// ============================================================
// Metadata 加载
// ============================================================
Future<void> _loadMetadataForCurrentSong() async {
if (_currentIndex < 0 || _currentIndex >= _queue.length) return;
final song = _queue[_currentIndex];
if (song.url == null || song.url!.isEmpty) return;
try {
// 注意:filePath 需要是本地文件路径
// WebDAV 文件可能需要先缓存到本地
final metadata = await MetadataService().getMetadata(
filePath: song.url!,
url: song.url!,
fileName: song.title,
fileId: song.id,
);
// 如果 metadata 有效且与当前不同,更新 Song
if (metadata.isNotEmpty) {
final updatedSong = Song(
id: song.id,
@@ -198,12 +188,13 @@ class AudioService extends ChangeNotifier {
_onSongChanged?.call(updatedSong);
}
} catch (e) {
// 读取失败,保持原有信息
debugPrint('⚠️ [AudioService] metadata load failed: $e');
}
}
// ---- 下一首 ----
// ============================================================
// 播放控制
// ============================================================
void next() {
if (_queue.isEmpty) return;
@@ -221,7 +212,6 @@ class AudioService extends ChangeNotifier {
_playCurrent();
}
// ---- 上一首 ----
void previous() {
if (_queue.isEmpty) return;
@@ -247,7 +237,6 @@ class AudioService extends ChangeNotifier {
_playCurrent();
}
// ---- 播放/暂停 ----
void togglePlay() {
if (_currentSong == null) return;
@@ -282,7 +271,38 @@ class AudioService extends ChangeNotifier {
notifyListeners();
}
// ---- 监听 media_kit 状态 ----
// ============================================================
// 播放器状态同步(兜底机制)
// ============================================================
void _syncPlayerStateDelayed() {
_syncPlayerStateNow();
Future.delayed(const Duration(milliseconds: 200), () {
_syncPlayerStateNow();
});
Future.delayed(const Duration(milliseconds: 500), () {
_syncPlayerStateNow();
});
}
void _syncPlayerStateNow() {
final player = PlaybackService().player;
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');
}
if (buf.inMilliseconds > 0 && bufferedNotifier.value.inMilliseconds == 0) {
bufferedNotifier.value = buf;
debugPrint('🎯 [AudioService] sync buffer: $buf');
}
}
// ============================================================
// media_kit 状态监听
// ============================================================
void _startListening() {
if (_listening) return;
_listening = true;
@@ -306,7 +326,11 @@ class AudioService extends ChangeNotifier {
_subscriptions.add(
player.stream.duration.listen((duration) {
durationNotifier.value = duration;
debugPrint('🎯 [AudioService] durationStream: $duration');
// 只接受 > 0 的值,避免 0 覆盖正确值
if (duration.inMilliseconds > 0) {
durationNotifier.value = duration;
}
}),
);
@@ -331,7 +355,9 @@ class AudioService extends ChangeNotifier {
_subscriptions.clear();
}
// ⭐ 防重入的完成事件处理
// ============================================================
// 播放完成处理
// ============================================================
void _onPlaybackCompleted() {
if (_handlingCompletion) {
debugPrint('⚠️ [service] completed ignored: already handling');