引入播放列表库,同时引入全新的播放控制器,以控制读库与播放模式的动作,但是功能仍在完善,需要确认部分边界问题
This commit is contained in:
@@ -3,12 +3,14 @@ import 'dart:async';
|
||||
import 'dart:io';
|
||||
import 'dart:typed_data';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:media_kit/media_kit.dart';
|
||||
import 'package:media_kit/media_kit.dart' as media_kit; // ⭐ 加别名
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'playback_service.dart';
|
||||
import '../metadata/metadata_service.dart';
|
||||
import '../database/song_database.dart';
|
||||
import '../utils/artwork_helper.dart';
|
||||
import '../repositories/playlist_repository.dart';
|
||||
import '../models/playlist.dart'; // ⭐ 你的 Playlist 模型
|
||||
|
||||
enum PlayMode {
|
||||
sequential,
|
||||
@@ -48,6 +50,9 @@ class AudioService extends ChangeNotifier {
|
||||
List<int> _shuffledIndices = [];
|
||||
int _shuffledIndex = -1;
|
||||
|
||||
// ---- 当前播放的歌单 ID(用于模式同步) ----
|
||||
String? _currentPlaylistId;
|
||||
|
||||
// ---- 高频进度 ----
|
||||
final ValueNotifier<Duration> positionNotifier = ValueNotifier(Duration.zero);
|
||||
final ValueNotifier<Duration> durationNotifier = ValueNotifier(Duration.zero);
|
||||
@@ -64,6 +69,10 @@ class AudioService extends ChangeNotifier {
|
||||
// ⭐ 播放代数:每次切歌递增,用于校验异步任务是否过期
|
||||
int _playbackGeneration = 0;
|
||||
|
||||
// ---- Repository ----
|
||||
final SongDatabase _db = SongDatabase();
|
||||
final PlaylistRepository _playlistRepo = PlaylistRepository();
|
||||
|
||||
// ---- Getter ----
|
||||
Song? get currentSong => _currentSong;
|
||||
bool get isPlaying => _isPlaying;
|
||||
@@ -71,6 +80,7 @@ class AudioService extends ChangeNotifier {
|
||||
List<Song> get queue => List.unmodifiable(_queue);
|
||||
int get currentIndex => _currentIndex;
|
||||
bool get hasQueue => _queue.isNotEmpty;
|
||||
String? get currentPlaylistId => _currentPlaylistId;
|
||||
|
||||
Duration get position => positionNotifier.value;
|
||||
Duration get duration => durationNotifier.value;
|
||||
@@ -91,6 +101,7 @@ class AudioService extends ChangeNotifier {
|
||||
_onSongChanged = callback;
|
||||
}
|
||||
|
||||
// ---- 切换播放模式(同步更新歌单) ----
|
||||
void togglePlayMode() {
|
||||
switch (_playMode) {
|
||||
case PlayMode.sequential:
|
||||
@@ -104,8 +115,14 @@ class AudioService extends ChangeNotifier {
|
||||
break;
|
||||
}
|
||||
notifyListeners();
|
||||
|
||||
// ⭐ 如果有当前歌单,同步更新歌单的播放模式
|
||||
if (_currentPlaylistId != null) {
|
||||
_playlistRepo.updatePlaylistPlayMode(_currentPlaylistId!, _playMode);
|
||||
}
|
||||
}
|
||||
|
||||
// ---- 设置播放队列 ----
|
||||
void setQueue(List<Song> queue, {int startIndex = 0}) {
|
||||
if (queue.isEmpty) {
|
||||
_clearQueue();
|
||||
@@ -131,10 +148,14 @@ class AudioService extends ChangeNotifier {
|
||||
_currentIndex = -1;
|
||||
_shuffledIndices.clear();
|
||||
_shuffledIndex = -1;
|
||||
_currentPlaylistId = null;
|
||||
stopPlay();
|
||||
}
|
||||
|
||||
// ---- 播放指定歌曲 ----
|
||||
Future<void> playSong(Song song) async {
|
||||
// 播放单曲时清除歌单上下文
|
||||
_currentPlaylistId = null;
|
||||
if (_queue.isEmpty || _queue[_currentIndex].id != song.id) {
|
||||
setQueue([song], startIndex: 0);
|
||||
} else {
|
||||
@@ -142,6 +163,66 @@ class AudioService extends ChangeNotifier {
|
||||
}
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════
|
||||
// 播放歌单
|
||||
// ════════════════════════════════════════════════════════════
|
||||
|
||||
/// 播放整个歌单
|
||||
Future<void> playPlaylist(String playlistId, {int startIndex = 0}) async {
|
||||
_currentPlaylistId = playlistId;
|
||||
|
||||
// 1. 获取歌单的播放模式
|
||||
final playlist = await _playlistRepo.getPlaylist(playlistId);
|
||||
if (playlist != null) {
|
||||
_playMode = playlist.playMode;
|
||||
}
|
||||
|
||||
// 2. 获取歌单歌曲 ID 列表
|
||||
final songIds = await _playlistRepo.getPlaylistSongIds(playlistId);
|
||||
if (songIds.isEmpty) return;
|
||||
|
||||
// 3. 将 song_id 转换为 Song 对象
|
||||
final songs = <Song>[];
|
||||
for (final id in songIds) {
|
||||
final dbSong = await _db.getSongByPath(id);
|
||||
if (dbSong != null) {
|
||||
songs.add(Song(
|
||||
id: dbSong['remote_path'] as String,
|
||||
title: dbSong['title'] as String? ?? '',
|
||||
artist: dbSong['artist'] as String? ?? '未知艺术家',
|
||||
url: dbSong['remote_path'] as String,
|
||||
));
|
||||
} else {
|
||||
// 如果 songs 表中没有记录,用路径作为 fallback
|
||||
songs.add(Song(
|
||||
id: id,
|
||||
title: id.split('/').last.replaceAll(RegExp(r'\.[^.]*$'), ''),
|
||||
artist: '未知艺术家',
|
||||
url: id,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
if (songs.isNotEmpty) {
|
||||
setQueue(songs, startIndex: startIndex);
|
||||
}
|
||||
}
|
||||
|
||||
/// 将当前播放队列保存为歌单
|
||||
Future<Playlist> saveQueueAsPlaylist(String name) async {
|
||||
// ⭐ 等待 createPlaylist 返回 Playlist 对象
|
||||
final playlist = await _playlistRepo.createPlaylist(name);
|
||||
for (int i = 0; i < _queue.length; i++) {
|
||||
final song = _queue[i];
|
||||
await _playlistRepo.addSong(playlist.id, song.id);
|
||||
}
|
||||
return playlist; // ⭐ 直接返回 Playlist 对象
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════
|
||||
// 播放核心
|
||||
// ════════════════════════════════════════════════════════════
|
||||
|
||||
void _playCurrent() {
|
||||
if (_currentIndex < 0 || _currentIndex >= _queue.length) {
|
||||
stopPlay();
|
||||
@@ -177,9 +258,6 @@ class AudioService extends ChangeNotifier {
|
||||
return raw.hashCode.toString();
|
||||
}
|
||||
|
||||
// ⭐ 核心:带 generation 校验的 metadata 加载
|
||||
// lib/services/audio_service.dart
|
||||
|
||||
Future<void> _loadMetadataForCurrentSong(int generation) async {
|
||||
if (_currentIndex < 0 || _currentIndex >= _queue.length) return;
|
||||
|
||||
@@ -188,7 +266,6 @@ class AudioService extends ChangeNotifier {
|
||||
final songId = song.id;
|
||||
|
||||
if (song.url == null || song.url!.isEmpty) {
|
||||
// 没有 URL,直接推送 fallback
|
||||
_onSongChanged?.call(song);
|
||||
return;
|
||||
}
|
||||
@@ -200,7 +277,6 @@ class AudioService extends ChangeNotifier {
|
||||
fileId: song.id,
|
||||
);
|
||||
|
||||
// generation 校验
|
||||
if (_playbackGeneration != generation) {
|
||||
debugPrint('⚠️ [AudioService] metadata stale (generation), ignoring');
|
||||
return;
|
||||
@@ -215,7 +291,6 @@ class AudioService extends ChangeNotifier {
|
||||
return;
|
||||
}
|
||||
|
||||
// 更新 Song
|
||||
final updatedSong = Song(
|
||||
id: song.id,
|
||||
title: metadata.title.isNotEmpty ? metadata.title : song.title,
|
||||
@@ -227,18 +302,16 @@ class AudioService extends ChangeNotifier {
|
||||
_currentSong = updatedSong;
|
||||
notifyListeners();
|
||||
|
||||
// ⭐ 一次性推送完整数据(含 artwork)
|
||||
_onSongChanged?.call(updatedSong);
|
||||
debugPrint(
|
||||
'✅ [AudioService] metadata updated: ${updatedSong.title} - ${updatedSong.artist} (generation $generation)');
|
||||
} catch (e) {
|
||||
debugPrint('⚠️ [AudioService] metadata load failed: $e, using fallback');
|
||||
// 异常时推送 fallback(无 artwork)
|
||||
_onSongChanged?.call(song);
|
||||
}
|
||||
}
|
||||
|
||||
/// 清除当前歌曲的缓存(metadata + 封面图 + 文件缓存),并强制重新播放
|
||||
/// 清除当前歌曲的缓存
|
||||
Future<void> clearCurrentSongCache() async {
|
||||
if (_currentSong == null) return;
|
||||
final song = _currentSong!;
|
||||
@@ -294,6 +367,7 @@ class AudioService extends ChangeNotifier {
|
||||
}
|
||||
}
|
||||
|
||||
// ---- 下一首 ----
|
||||
void next() {
|
||||
if (_queue.isEmpty) return;
|
||||
|
||||
@@ -311,6 +385,7 @@ class AudioService extends ChangeNotifier {
|
||||
_playCurrent();
|
||||
}
|
||||
|
||||
// ---- 上一首 ----
|
||||
void previous() {
|
||||
if (_queue.isEmpty) return;
|
||||
|
||||
@@ -336,6 +411,7 @@ class AudioService extends ChangeNotifier {
|
||||
_playCurrent();
|
||||
}
|
||||
|
||||
// ---- 播放/暂停 ----
|
||||
void togglePlay() {
|
||||
if (_currentSong == null) return;
|
||||
|
||||
@@ -371,6 +447,7 @@ class AudioService extends ChangeNotifier {
|
||||
_currentIndex = -1;
|
||||
_shuffledIndices.clear();
|
||||
_shuffledIndex = -1;
|
||||
_currentPlaylistId = null;
|
||||
stopPlay();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user