引入播放列表库,同时引入全新的播放控制器,以控制读库与播放模式的动作,但是功能仍在完善,需要确认部分边界问题

This commit is contained in:
2026-08-27 22:29:20 +08:00
parent ad6617592e
commit e58be9c17f
6 changed files with 677 additions and 126 deletions
+23 -102
View File
@@ -1,6 +1,4 @@
// lib/pages/webdav_file_list_page.dart
// ignore: unused_import
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../services/webdav_service.dart';
@@ -8,6 +6,7 @@ import '../services/playback_service.dart';
import '../services/audio_service.dart';
import '../constants/ui_constants.dart';
import '../base/base_state.dart';
import '../controllers/playback_controller.dart';
class WebDAVFileListPage extends StatefulWidget {
final String currentPath;
@@ -53,16 +52,14 @@ class _WebDAVFileListPageState extends BaseState<WebDAVFileListPage> {
final items =
await WebDAVService.instance.listDirectory(path: _currentPath);
if (mounted) {
// ⭐ 加这个
setState(() {
safeSetState(() {
_items = items;
_isLoading = false;
});
}
} catch (e) {
if (mounted) {
// ⭐ 加这个
setState(() {
safeSetState(() {
_errorMessage = '加载失败: $e';
_isLoading = false;
});
@@ -82,72 +79,22 @@ class _WebDAVFileListPageState extends BaseState<WebDAVFileListPage> {
);
}
// ============================================================
// ⭐ 核心:播放歌曲 + 自动构建队列
// ============================================================
// ============================================================
// 播放单首(点击歌曲)
// ============================================================
void _playSong(WebDAVItem file) async {
try {
// 1. 获取当前目录所有音乐文件(过滤掉目录)
final musicFiles = _items.where((item) => !item.isDirectory).toList();
if (musicFiles.isEmpty) {
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('当前目录没有音乐文件'),
backgroundColor: Colors.orange,
),
);
}
return;
}
// 2. 获取认证头
final headers = await WebDAVService.instance.getAuthHeaders();
// 3. 构建播放队列(所有音乐文件)
final queue = musicFiles.map((item) {
final itemUrl = WebDAVService.instance.getFileUrl(item.path);
return Song(
id: item.path,
title: item.name.replaceAll(RegExp(r'\.[^.]*$'), ''),
artist: '未知艺术家',
url: itemUrl,
);
}).toList();
// 4. 找到当前点击歌曲在队列中的位置
final startIndex = queue.indexWhere((s) => s.id == file.path);
if (startIndex == -1) {
// 极端情况:队列构建有问题,退化为单曲播放
final url = WebDAVService.instance.getFileUrl(file.path);
final song = Song(
id: file.path,
title: file.name.replaceAll(RegExp(r'\.[^.]*$'), ''),
artist: '未知艺术家',
url: url,
);
context.read<AudioService>().setQueue([song], startIndex: 0);
await PlaybackService().play(url, headers: headers);
return;
}
// 5. 设置队列并播放
final audioService = context.read<AudioService>();
audioService.setQueue(queue, startIndex: startIndex);
// 6. 播放(AudioService 内部已经调用了 PlaybackService,但为了确保认证头传递)
// 这里再显式调用一下,确保认证头正确
final targetUrl = queue[startIndex].url!;
await PlaybackService().play(targetUrl, headers: headers);
// 7. 更新 AudioService 的播放状态(确保 UI 同步)
// setQueue 已经调用了 _playCurrent(),所以这里不需要重复调用
// ⭐ 直接使用 PlaybackController 单例
await PlaybackController().playFromWebDAV(
directoryPath: _currentPath,
clickedSongPath: file.path,
allItems: _items,
);
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('正在播放: ${file.name}'),
content: Text('正在播放: ${_safeDecode(file.name)}'),
backgroundColor: const Color(0xFF4CAF50),
duration: const Duration(seconds: 1),
),
@@ -165,47 +112,22 @@ class _WebDAVFileListPageState extends BaseState<WebDAVFileListPage> {
}
}
// ============================================================
// 全部播放(从第一首开始)
// ============================================================
// ============================================================
// 全部播放
// ============================================================
void _playAll() async {
try {
final musicFiles = _items.where((item) => !item.isDirectory).toList();
if (musicFiles.isEmpty) {
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('当前目录没有音乐文件'),
backgroundColor: Colors.orange,
),
);
}
return;
}
final headers = await WebDAVService.instance.getAuthHeaders();
final queue = musicFiles.map((item) {
final itemUrl = WebDAVService.instance.getFileUrl(item.path);
return Song(
id: item.path,
title: item.name.replaceAll(RegExp(r'\.[^.]*$'), ''),
artist: '未知艺术家',
url: itemUrl,
);
}).toList();
final audioService = context.read<AudioService>();
audioService.setQueue(queue, startIndex: 0);
final targetUrl = queue[0].url!;
await PlaybackService().play(targetUrl, headers: headers);
// ⭐ 全部播放使用 playAllFromWebDAV
await PlaybackController().playAllFromWebDAV(
directoryPath: _currentPath,
allItems: _items,
);
if (mounted) {
final musicCount = _items.where((item) => !item.isDirectory).length;
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('开始播放全部 (${queue.length}首)'),
content: Text('开始播放全部 ($musicCount 首)'),
backgroundColor: const Color(0xFF4CAF50),
duration: const Duration(seconds: 1),
),
@@ -257,7 +179,6 @@ class _WebDAVFileListPageState extends BaseState<WebDAVFileListPage> {
onPressed: () => Navigator.pop(context),
),
actions: [
// 全部播放按钮
IconButton(
icon: const Icon(Icons.playlist_play),
onPressed: _playAll,