现在正在修复首次启动点击跳曲问题与顺序播放的跳曲问题
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
// lib/controllers/playback_controller.dart
|
||||
import 'package:flutter/foundation.dart'; // ⭐ 添加
|
||||
import '../services/audio_service.dart';
|
||||
import '../services/webdav_service.dart';
|
||||
import '../database/song_database.dart';
|
||||
@@ -12,27 +13,43 @@ class PlaybackController {
|
||||
final SongDatabase _db = SongDatabase();
|
||||
|
||||
// ════════════════════════════════════════════════════════════
|
||||
// 播放 WebDAV 目录中的歌曲(从点击歌曲开始)
|
||||
// 从 WebDAV 目录播放(点击歌曲)
|
||||
// ════════════════════════════════════════════════════════════
|
||||
Future<void> playFromWebDAV({
|
||||
required String directoryPath,
|
||||
required String clickedSongPath,
|
||||
required List<WebDAVItem> allItems,
|
||||
}) async {
|
||||
final musicFiles = allItems.where((item) => !item.isDirectory).toList();
|
||||
// 1. 过滤出所有音乐文件(去掉目录)
|
||||
final musicFiles = allItems.where((item) => !item.isDirectory).toList()
|
||||
..sort((a, b) => a.name.toLowerCase().compareTo(b.name.toLowerCase()));
|
||||
|
||||
if (musicFiles.isEmpty) return;
|
||||
|
||||
// 2. 调试信息
|
||||
debugPrint('🎯 [PlaybackController] musicFiles:');
|
||||
for (int i = 0; i < musicFiles.length; i++) {
|
||||
debugPrint(' $i: ${musicFiles[i].path}');
|
||||
}
|
||||
debugPrint('🎯 [PlaybackController] clickedSongPath: $clickedSongPath');
|
||||
|
||||
// 3. 找到点击歌曲在音乐文件列表中的位置
|
||||
final clickedIndex =
|
||||
musicFiles.indexWhere((item) => item.path == clickedSongPath);
|
||||
debugPrint('🎯 [PlaybackController] clickedIndex: $clickedIndex');
|
||||
|
||||
if (clickedIndex == -1) {
|
||||
debugPrint(
|
||||
'⚠️ [PlaybackController] clicked song not found, fallback to single play');
|
||||
await _playSingleSong(clickedSongPath);
|
||||
return;
|
||||
}
|
||||
|
||||
// 4. 构建 Song 队列
|
||||
final queue = await _buildSongQueue(musicFiles);
|
||||
|
||||
// 5. 应用播放模式
|
||||
final playMode = _audioService.playMode;
|
||||
// ⭐ 修复:显式指定类型
|
||||
List<Song> finalQueue = List<Song>.from(queue);
|
||||
int startIndex = clickedIndex;
|
||||
|
||||
@@ -43,23 +60,27 @@ class PlaybackController {
|
||||
finalQueue = shuffled;
|
||||
}
|
||||
|
||||
debugPrint(
|
||||
'🎯 [PlaybackController] finalQueue length: ${finalQueue.length}, startIndex: $startIndex');
|
||||
|
||||
_audioService.setQueue(finalQueue, startIndex: startIndex);
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════
|
||||
// 播放整个目录(从第一首开始)
|
||||
// 从 WebDAV 目录播放(全部播放)
|
||||
// ════════════════════════════════════════════════════════════
|
||||
Future<void> playAllFromWebDAV({
|
||||
required String directoryPath,
|
||||
required List<WebDAVItem> allItems,
|
||||
}) async {
|
||||
final musicFiles = allItems.where((item) => !item.isDirectory).toList();
|
||||
final musicFiles = allItems.where((item) => !item.isDirectory).toList()
|
||||
..sort((a, b) => a.name.toLowerCase().compareTo(b.name.toLowerCase()));
|
||||
|
||||
if (musicFiles.isEmpty) return;
|
||||
|
||||
final queue = await _buildSongQueue(musicFiles);
|
||||
|
||||
final playMode = _audioService.playMode;
|
||||
// ⭐ 修复:显式指定类型
|
||||
List<Song> finalQueue = List<Song>.from(queue);
|
||||
if (playMode == PlayMode.shuffle) {
|
||||
finalQueue = List<Song>.from(queue)..shuffle();
|
||||
@@ -69,9 +90,10 @@ class PlaybackController {
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════
|
||||
// 播放单曲(fallback)
|
||||
// 单曲播放(fallback)
|
||||
// ════════════════════════════════════════════════════════════
|
||||
Future<void> _playSingleSong(String songPath) async {
|
||||
debugPrint('🎯 [PlaybackController] _playSingleSong: $songPath');
|
||||
final url = WebDAVService.instance.getFileUrl(songPath);
|
||||
final dbSong = await _db.getSongByPath(songPath);
|
||||
final song = Song(
|
||||
@@ -85,7 +107,7 @@ class PlaybackController {
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════
|
||||
// 辅助方法:从音乐文件列表构建 Song 队列
|
||||
// 构建 Song 队列
|
||||
// ════════════════════════════════════════════════════════════
|
||||
Future<List<Song>> _buildSongQueue(List<WebDAVItem> musicFiles) async {
|
||||
final queue = <Song>[];
|
||||
@@ -113,19 +135,10 @@ class PlaybackController {
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════
|
||||
// 播放控制(透传到 AudioService)
|
||||
// 播放控制透传
|
||||
// ════════════════════════════════════════════════════════════
|
||||
void next() {
|
||||
_audioService.next();
|
||||
}
|
||||
|
||||
void previous() {
|
||||
_audioService.previous();
|
||||
}
|
||||
|
||||
void togglePlayMode() {
|
||||
_audioService.togglePlayMode();
|
||||
}
|
||||
|
||||
void next() => _audioService.next();
|
||||
void previous() => _audioService.previous();
|
||||
void togglePlayMode() => _audioService.togglePlayMode();
|
||||
PlayMode get playMode => _audioService.playMode;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user