通知中心封面图显示

部分界面的信息更新
缓存系统初步引入
即将进入播放列表更新
This commit is contained in:
2026-08-25 23:09:17 +08:00
parent 0e8921742c
commit f2b4d1f46d
16 changed files with 454 additions and 161 deletions
+78 -25
View File
@@ -1,10 +1,13 @@
// lib/services/audio_service.dart
import 'dart:async';
import 'dart:io';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:media_kit/media_kit.dart';
import 'package:path_provider/path_provider.dart';
import 'playback_service.dart';
import '../metadata/metadata_service.dart';
import 'dart:typed_data';
import '../database/song_database.dart';
import '../utils/artwork_helper.dart';
enum PlayMode {
sequential,
@@ -44,7 +47,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);
@@ -55,7 +58,6 @@ class AudioService extends ChangeNotifier {
bool _handlingCompletion = false;
void Function(Song)? _onSongChanged;
// ⭐ 防 seek 覆盖标志
bool _isUserSeeking = false;
// ---- Getter ----
@@ -81,12 +83,10 @@ class AudioService extends ChangeNotifier {
}
}
// ---- 注册回调 ----
void setOnSongChanged(void Function(Song) callback) {
_onSongChanged = callback;
}
// ---- 切换播放模式 ----
void togglePlayMode() {
switch (_playMode) {
case PlayMode.sequential:
@@ -102,7 +102,6 @@ class AudioService extends ChangeNotifier {
notifyListeners();
}
// ---- 设置播放队列 ----
void setQueue(List<Song> queue, {int startIndex = 0}) {
if (queue.isEmpty) {
_clearQueue();
@@ -131,7 +130,6 @@ class AudioService extends ChangeNotifier {
stopPlay();
}
// ---- 播放指定歌曲 ----
Future<void> playSong(Song song) async {
if (_queue.isEmpty || _queue[_currentIndex].id != song.id) {
setQueue([song], startIndex: 0);
@@ -140,7 +138,6 @@ class AudioService extends ChangeNotifier {
}
}
// ---- 核心:播放当前歌曲 ----
void _playCurrent() {
if (_currentIndex < 0 || _currentIndex >= _queue.length) {
stopPlay();
@@ -159,13 +156,16 @@ class AudioService extends ChangeNotifier {
PlaybackService().play(song.url!);
// 延迟同步兜底(解决首次加载时 stream 未推送的问题)
_syncPlayerStateDelayed();
_loadMetadataForCurrentSong();
}
// ---- 加载 metadata ----
String _generateSongKey(String url, int fileSize, int modifiedTime) {
final raw = '$url|$fileSize|$modifiedTime';
return raw.hashCode.toString();
}
Future<void> _loadMetadataForCurrentSong() async {
if (_currentIndex < 0 || _currentIndex >= _queue.length) return;
final song = _queue[_currentIndex];
@@ -184,12 +184,13 @@ class AudioService extends ChangeNotifier {
title: metadata.title.isNotEmpty ? metadata.title : song.title,
artist: metadata.artist.isNotEmpty ? metadata.artist : song.artist,
url: song.url,
artwork: metadata.artwork,
// ⭐ 如果需要传递 artwork,可以在这里添加字段
artwork: metadata.artwork, // ✅ 已有
);
_queue[_currentIndex] = updatedSong;
_currentSong = updatedSong;
notifyListeners();
// ⭐ 传递完整的 updatedSong(包含 artwork
_onSongChanged?.call(updatedSong);
}
} catch (e) {
@@ -197,7 +198,70 @@ class AudioService extends ChangeNotifier {
}
}
// ---- 下一首 ----
/// 清除当前歌曲的缓存(metadata + 封面图 + 文件缓存),并强制重新播放
Future<void> clearCurrentSongCache() async {
if (_currentSong == null) return;
final song = _currentSong!;
final url = song.url ?? '';
if (url.isEmpty) return;
debugPrint('🗑️ [AudioService] clearing cache for: ${song.title}');
// 1. 获取当前索引
final currentIndex = _currentIndex;
// 2. 尝试获取文件信息生成 song_key
String songKey;
try {
final provider = MetadataService().getProviderForUrl(url);
final file = await provider.getFile(url);
if (file != null && await file.exists()) {
final stat = await file.stat();
songKey = _generateSongKey(
url, stat.size, stat.modified.millisecondsSinceEpoch);
} else {
songKey = url.hashCode.toString();
}
} catch (e) {
songKey = url.hashCode.toString();
}
// 3. 清除 SQLite 记录
final db = SongDatabase();
await db.deleteSong(songKey);
await db.deleteCache(songKey);
debugPrint('🗑️ [AudioService] SQLite records deleted: $songKey');
// 4. 删除封面图
await ArtworkHelper.deleteArtwork(songKey);
debugPrint('🗑️ [AudioService] artwork deleted');
// 5. 删除 metadata 临时缓存文件
try {
final cacheDir = await getTemporaryDirectory();
final cachePath = '${cacheDir.path}/metadata_${url.hashCode}.tmp';
final cacheFile = File(cachePath);
if (await cacheFile.exists()) {
await cacheFile.delete();
debugPrint('🗑️ [AudioService] temp cache file deleted');
}
} catch (e) {
// 忽略
}
// 6. 清除内存缓存
await MetadataService().clearCache(song.id);
debugPrint('🗑️ [AudioService] memory cache cleared');
// 7. 停止并重新播放
if (currentIndex >= 0 && currentIndex < _queue.length) {
stopPlay();
// 确保重新播放同一首歌
_playCurrent();
debugPrint('🔄 [AudioService] song reloaded');
}
}
void next() {
if (_queue.isEmpty) return;
@@ -215,7 +279,6 @@ class AudioService extends ChangeNotifier {
_playCurrent();
}
// ---- 上一首 ----
void previous() {
if (_queue.isEmpty) return;
@@ -241,7 +304,6 @@ class AudioService extends ChangeNotifier {
_playCurrent();
}
// ---- 播放/暂停 ----
void togglePlay() {
if (_currentSong == null) return;
@@ -252,7 +314,6 @@ class AudioService extends ChangeNotifier {
}
}
// ---- 停止播放 ----
void stopPlay() {
_currentSong = null;
_isPlaying = false;
@@ -263,19 +324,16 @@ class AudioService extends ChangeNotifier {
notifyListeners();
}
// ---- 跳转 ----
void seekTo(Duration position) {
_isUserSeeking = true;
PlaybackService().seek(position);
positionNotifier.value = position;
// 800ms 后重置标志(覆盖 media_kit 的 positionStream 推送窗口)
Future.delayed(const Duration(milliseconds: 800), () {
_isUserSeeking = false;
});
}
// ---- 清空队列 ----
void clearQueue() {
_queue.clear();
_currentIndex = -1;
@@ -285,7 +343,6 @@ class AudioService extends ChangeNotifier {
notifyListeners();
}
// ---- 主动状态同步(供 PlayerPage 调用) ----
void syncPlayerStateNow() {
final player = PlaybackService().player;
final pos = player.state.position;
@@ -306,7 +363,6 @@ class AudioService extends ChangeNotifier {
}
}
// ---- 延迟同步(兜底) ----
void _syncPlayerStateDelayed() {
syncPlayerStateNow();
Future.delayed(const Duration(milliseconds: 200), () {
@@ -317,7 +373,6 @@ class AudioService extends ChangeNotifier {
});
}
// ---- 监听 media_kit 状态 ----
void _startListening() {
if (_listening) return;
_listening = true;
@@ -335,7 +390,6 @@ class AudioService extends ChangeNotifier {
_subscriptions.add(
player.stream.position.listen((position) {
// ⭐ 如果是用户主动 seek,忽略这次推送(避免覆盖)
if (_isUserSeeking) {
debugPrint('🎯 [AudioService] positionStream ignored: user seeking');
return;
@@ -374,7 +428,6 @@ class AudioService extends ChangeNotifier {
_subscriptions.clear();
}
// ---- 防重入的完成事件处理 ----
void _onPlaybackCompleted() {
if (_handlingCompletion) {
debugPrint('⚠️ [service] completed ignored: already handling');