基础实现代码框架先上,后面慢慢改逻辑

This commit is contained in:
2026-08-23 19:24:16 +08:00
parent 6c4e77ca00
commit 9a60441e04
9 changed files with 446 additions and 1 deletions
+39 -1
View File
@@ -3,6 +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'; // ⭐ 新增
enum PlayMode {
sequential,
@@ -162,6 +163,44 @@ class AudioService extends ChangeNotifier {
}
PlaybackService().play(song.url!);
// ⭐ 异步加载 metadata(不阻塞播放)
_loadMetadataForCurrentSong();
}
// ⭐ 新增:加载当前歌曲的 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!,
fileName: song.title,
fileId: song.id,
);
// 如果 metadata 有效且与当前不同,更新 Song
if (metadata.isNotEmpty) {
final updatedSong = Song(
id: song.id,
title: metadata.title.isNotEmpty ? metadata.title : song.title,
artist: metadata.artist.isNotEmpty ? metadata.artist : song.artist,
url: song.url,
);
_queue[_currentIndex] = updatedSong;
_currentSong = updatedSong;
notifyListeners();
_onSongChanged?.call(updatedSong);
}
} catch (e) {
// 读取失败,保持原有信息
debugPrint('⚠️ [AudioService] metadata load failed: $e');
}
}
// ---- 下一首 ----
@@ -277,7 +316,6 @@ class AudioService extends ChangeNotifier {
}),
);
// ⭐ 唯一监听 completed 的地方(带防重入)
_subscriptions.add(
player.stream.completed.listen((_) {
_onPlaybackCompleted();