现在正在修复首次启动点击跳曲问题与顺序播放的跳曲问题
This commit is contained in:
@@ -3,14 +3,15 @@ import 'dart:async';
|
||||
import 'dart:io';
|
||||
import 'dart:typed_data';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:media_kit/media_kit.dart' as media_kit; // ⭐ 加别名
|
||||
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 模型
|
||||
import '../models/playlist.dart';
|
||||
import 'webdav_service.dart';
|
||||
|
||||
enum PlayMode {
|
||||
sequential,
|
||||
@@ -116,7 +117,6 @@ class AudioService extends ChangeNotifier {
|
||||
}
|
||||
notifyListeners();
|
||||
|
||||
// ⭐ 如果有当前歌单,同步更新歌单的播放模式
|
||||
if (_currentPlaylistId != null) {
|
||||
_playlistRepo.updatePlaylistPlayMode(_currentPlaylistId!, _playMode);
|
||||
}
|
||||
@@ -154,7 +154,6 @@ class AudioService extends ChangeNotifier {
|
||||
|
||||
// ---- 播放指定歌曲 ----
|
||||
Future<void> playSong(Song song) async {
|
||||
// 播放单曲时清除歌单上下文
|
||||
_currentPlaylistId = null;
|
||||
if (_queue.isEmpty || _queue[_currentIndex].id != song.id) {
|
||||
setQueue([song], startIndex: 0);
|
||||
@@ -167,21 +166,17 @@ 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);
|
||||
@@ -193,7 +188,6 @@ class AudioService extends ChangeNotifier {
|
||||
url: dbSong['remote_path'] as String,
|
||||
));
|
||||
} else {
|
||||
// 如果 songs 表中没有记录,用路径作为 fallback
|
||||
songs.add(Song(
|
||||
id: id,
|
||||
title: id.split('/').last.replaceAll(RegExp(r'\.[^.]*$'), ''),
|
||||
@@ -208,19 +202,17 @@ class AudioService extends ChangeNotifier {
|
||||
}
|
||||
}
|
||||
|
||||
/// 将当前播放队列保存为歌单
|
||||
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 对象
|
||||
return playlist;
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════
|
||||
// 播放核心
|
||||
// ⭐ 播放核心(修复:自动添加认证头)
|
||||
// ════════════════════════════════════════════════════════════
|
||||
|
||||
void _playCurrent() {
|
||||
@@ -242,17 +234,37 @@ class AudioService extends ChangeNotifier {
|
||||
return;
|
||||
}
|
||||
|
||||
PlaybackService().play(song.url!);
|
||||
// ⭐ 关键修复:播放时自动添加认证头
|
||||
_playWithHeaders(song.url!);
|
||||
|
||||
_syncPlayerStateDelayed();
|
||||
|
||||
// ⭐ 立即推送基本信息(无 artwork)
|
||||
_onSongChanged?.call(song);
|
||||
|
||||
// ⭐ 异步加载完整 metadata(含 artwork)
|
||||
_loadMetadataForCurrentSong(generation);
|
||||
}
|
||||
|
||||
/// ⭐ 播放带认证头的 URL
|
||||
Future<void> _playWithHeaders(String url) async {
|
||||
final headers = await _getAuthHeadersForUrl(url);
|
||||
await PlaybackService().play(url, headers: headers);
|
||||
}
|
||||
|
||||
/// ⭐ 获取 URL 对应的认证头(WebDAV 需要,本地文件不需要)
|
||||
Future<Map<String, String>> _getAuthHeadersForUrl(String url) async {
|
||||
if (url.startsWith('http://') || url.startsWith('https://')) {
|
||||
try {
|
||||
final headers = await WebDAVService.instance.getAuthHeaders();
|
||||
if (headers.isNotEmpty) {
|
||||
debugPrint('🔑 [AudioService] 已添加认证头到播放请求');
|
||||
return headers;
|
||||
}
|
||||
} catch (e) {
|
||||
debugPrint('⚠️ [AudioService] 获取认证头失败: $e');
|
||||
}
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
String _generateSongKey(String url, int fileSize, int modifiedTime) {
|
||||
final raw = '$url|$fileSize|$modifiedTime';
|
||||
return raw.hashCode.toString();
|
||||
|
||||
Reference in New Issue
Block a user