Compare commits

6 Commits
14 changed files with 644 additions and 127 deletions
-1
View File
@@ -1,5 +1,4 @@
// lib/audio/player_controller.dart // lib/audio/player_controller.dart
import 'package:media_kit/media_kit.dart';
import '../services/playback_service.dart'; // ⭐ 修正路径 import '../services/playback_service.dart'; // ⭐ 修正路径
class PlayerController { class PlayerController {
+28
View File
@@ -0,0 +1,28 @@
// lib/metadata/file_provider.dart
import 'dart:io';
abstract class FileProvider {
/// 获取文件,如果是远程文件则先下载到本地缓存
Future<File?> getFile(String url);
}
class LocalFileProvider implements FileProvider {
@override
Future<File?> getFile(String url) async {
final file = File(url);
if (await file.exists()) {
return file;
}
return null;
}
}
class WebDAVFileProvider implements FileProvider {
@override
Future<File?> getFile(String url) async {
// 🔴 第一阶段:先不实现下载,返回 null
// 这样 WebDAV 文件会 fallback 到文件名
// 等核心播放逻辑稳定后,再实现真正的下载缓存
return null;
}
}
+27
View File
@@ -0,0 +1,27 @@
// lib/metadata/metadata_cache.dart
import 'metadata_model.dart';
class MetadataCache {
final Map<String, FinalMetadata> _cache = {};
Future<FinalMetadata?> get(String fileId) async {
return _cache[fileId];
}
Future<void> put(String fileId, FinalMetadata metadata) async {
_cache[fileId] = metadata;
}
Future<void> update(String fileId, FinalMetadata metadata) async {
_cache[fileId] = metadata;
}
Future<List<Map<String, dynamic>>> getHistory(String artist,
{int limit = 10}) async {
return [];
}
Future<void> clear() async {
_cache.clear();
}
}
+16
View File
@@ -0,0 +1,16 @@
// lib/metadata/metadata_constants.dart
class MetadataConstants {
/// 首次 metadata 触发延迟(播放开始后等待时间)
static const Duration initialTriggerDelay = Duration(seconds: 20);
/// 重新确认进度点
static const double recheckProgress75 = 0.75;
static const double recheckProgress100 = 1.0;
/// 置信度阈值
static const double confidenceThreshold = 0.6;
/// 最小历史证据数量
static const int minHistoryCount = 4;
}
+143
View File
@@ -0,0 +1,143 @@
// lib/metadata/metadata_model.dart
import 'dart:typed_data';
/// 从音频文件读取的原始元数据
class RawMetadata {
final String title;
final String artist;
final String album;
final List<String> genres; // ⬅️ 改为 List<String>
final List<String> performers; // ⬅️ 新增
final int year;
final int trackNumber;
final int trackTotal;
final int discNumber;
final int totalDisc;
final Duration? duration;
final Uint8List? artwork;
const RawMetadata({
this.title = '',
this.artist = '',
this.album = '',
this.genres = const [],
this.performers = const [],
this.year = 0,
this.trackNumber = 0,
this.trackTotal = 0,
this.discNumber = 0,
this.totalDisc = 0,
this.duration,
this.artwork,
});
bool get isEmpty =>
title.isEmpty &&
artist.isEmpty &&
album.isEmpty &&
genres.isEmpty &&
performers.isEmpty &&
year == 0;
bool get isNotEmpty => !isEmpty;
}
/// 标准化后的元数据
class NormalizedMetadata {
final String title;
final String artist;
final String album;
final List<String> genres;
final List<String> performers;
final int year;
final int trackNumber;
final int trackTotal;
final int discNumber;
final int totalDisc;
final Duration? duration;
final Uint8List? artwork;
const NormalizedMetadata({
this.title = '',
this.artist = '',
this.album = '',
this.genres = const [],
this.performers = const [],
this.year = 0,
this.trackNumber = 0,
this.trackTotal = 0,
this.discNumber = 0,
this.totalDisc = 0,
this.duration,
this.artwork,
});
bool get isEmpty =>
title.isEmpty &&
artist.isEmpty &&
album.isEmpty &&
genres.isEmpty &&
performers.isEmpty &&
year == 0;
bool get isNotEmpty => !isEmpty;
}
/// 最终确认的元数据
class FinalMetadata {
final String title;
final String artist;
final String album;
final String genre; // ⬅️ 简化为单个流派(取第一个)
final List<String> genres; // ⬅️ 保留完整列表
final List<String> performers;
final int year;
final int trackNumber;
final int trackTotal;
final int discNumber;
final int totalDisc;
final Duration? duration;
final Uint8List? artwork;
final String source;
const FinalMetadata({
this.title = '',
this.artist = '',
this.album = '',
this.genre = '',
this.genres = const [],
this.performers = const [],
this.year = 0,
this.trackNumber = 0,
this.trackTotal = 0,
this.discNumber = 0,
this.totalDisc = 0,
this.duration,
this.artwork,
this.source = 'unknown',
});
bool get isEmpty =>
title.isEmpty &&
artist.isEmpty &&
album.isEmpty &&
genre.isEmpty &&
genres.isEmpty &&
performers.isEmpty &&
year == 0;
bool get isNotEmpty => !isEmpty;
}
/// 候选元数据
class CandidateMetadata {
final NormalizedMetadata metadata;
final double confidence;
final Map<String, double> evidence;
const CandidateMetadata({
required this.metadata,
required this.confidence,
this.evidence = const {},
});
}
+89
View File
@@ -0,0 +1,89 @@
// lib/metadata/metadata_normalizer.dart
import 'metadata_model.dart';
class MetadataNormalizer {
NormalizedMetadata normalize(RawMetadata raw,
{String fileName = '', String filePath = ''}) {
// 1. 修剪空格
final title = raw.title.trim();
final artist = raw.artist.trim();
final album = raw.album.trim();
// 2. 如果 title 为空,从文件名推断
final finalTitle =
title.isNotEmpty ? title : _inferTitleFromFileName(fileName);
// 3. 如果 artist 为空,从路径推断或使用默认值
final finalArtist =
artist.isNotEmpty ? artist : _inferArtistFromPath(filePath);
// 4. 如果 performers 不为空且 artist 为空,用 performers 的第一个
final finalArtist2 = finalArtist.isNotEmpty
? finalArtist
: (raw.performers.isNotEmpty ? raw.performers.first : '未知艺术家');
return NormalizedMetadata(
title: finalTitle,
artist: finalArtist2,
album: album,
genres: raw.genres,
performers: raw.performers,
year: raw.year,
trackNumber: raw.trackNumber,
trackTotal: raw.trackTotal,
discNumber: raw.discNumber,
totalDisc: raw.totalDisc,
duration: raw.duration,
artwork: raw.artwork,
);
}
String _inferTitleFromFileName(String fileName) {
final dotIndex = fileName.lastIndexOf('.');
if (dotIndex > 0) {
return fileName.substring(0, dotIndex).trim();
}
return fileName.trim();
}
String _inferArtistFromPath(String filePath) {
final parts = filePath.split(RegExp(r'[/\\]'));
if (parts.length >= 2) {
final candidate = parts[parts.length - 2].trim();
if (candidate.isNotEmpty && !candidate.contains(' ')) {
return candidate;
}
}
return '未知艺术家';
}
CandidateMetadata evaluate(
NormalizedMetadata normalized, List<Map<String, dynamic>> history) {
return CandidateMetadata(
metadata: normalized,
confidence: 0.8,
evidence: {'normalized': 0.8},
);
}
FinalMetadata decide(CandidateMetadata candidate, {double threshold = 0.6}) {
return FinalMetadata(
title: candidate.metadata.title,
artist: candidate.metadata.artist,
album: candidate.metadata.album,
genre: candidate.metadata.genres.isNotEmpty
? candidate.metadata.genres.first
: '',
genres: candidate.metadata.genres,
performers: candidate.metadata.performers,
year: candidate.metadata.year,
trackNumber: candidate.metadata.trackNumber,
trackTotal: candidate.metadata.trackTotal,
discNumber: candidate.metadata.discNumber,
totalDisc: candidate.metadata.totalDisc,
duration: candidate.metadata.duration,
artwork: candidate.metadata.artwork,
source: 'normalized',
);
}
}
+39
View File
@@ -0,0 +1,39 @@
// lib/metadata/metadata_reader.dart
import 'dart:io';
import 'package:audio_metadata_reader/audio_metadata_reader.dart' as amr;
import 'metadata_model.dart';
class MetadataReader {
Future<RawMetadata> readRawMetadata(File file) async {
try {
final meta = amr.readMetadata(file, getImage: false);
return RawMetadata(
title: meta.title ?? '',
artist: meta.artist ?? '',
album: meta.album ?? '',
// ⭐ genres 和 performers 是非空 List<String>,直接使用
genres: meta.genres,
performers: meta.performers,
year: _toInt(meta.year),
trackNumber: _toInt(meta.trackNumber),
trackTotal: _toInt(meta.trackTotal),
discNumber: _toInt(meta.discNumber),
totalDisc: _toInt(meta.totalDisc),
duration: meta.duration,
);
} catch (e) {
return const RawMetadata();
}
}
/// 安全转换为 int,处理 Object 类型
int _toInt(dynamic value) {
if (value == null) return 0;
if (value is int) return value;
if (value is String) return int.tryParse(value) ?? 0;
// 有些字段可能是 double 或 num
if (value is num) return value.toInt();
return 0;
}
}
+47
View File
@@ -0,0 +1,47 @@
// lib/metadata/metadata_service.dart
import 'metadata_model.dart';
import 'metadata_cache.dart';
class MetadataService {
static final MetadataService _instance = MetadataService._internal();
factory MetadataService() => _instance;
MetadataService._internal();
final MetadataCache _cache = MetadataCache();
/// 临时版本:不进行实际文件读取,只返回基于文件名的 fallback
Future<FinalMetadata> getMetadata({
required String url,
required String fileName,
required String fileId,
bool forceRefresh = false,
}) async {
// 1. 检查缓存(如果有)
if (!forceRefresh) {
final cached = await _cache.get(fileId);
if (cached != null && cached.isNotEmpty) {
return cached;
}
}
// 2. 暂时不读取文件,直接 fallback
final fallback = _fallbackFromFileName(fileName);
// 3. 缓存结果(即使 fallback 也缓存,避免频繁调用)
if (fallback.isNotEmpty) {
await _cache.put(fileId, fallback);
}
return fallback;
}
FinalMetadata _fallbackFromFileName(String fileName) {
final dotIndex = fileName.lastIndexOf('.');
final title = dotIndex > 0 ? fileName.substring(0, dotIndex) : fileName;
return FinalMetadata(
title: title,
artist: '未知艺术家',
source: 'filename',
);
}
}
+153 -100
View File
@@ -43,6 +43,41 @@ class _PlayerPageState extends State<PlayerPage> {
} }
} }
@override
void initState() {
super.initState();
// ⭐ 页面加载时主动拉取状态(多次重试)
_syncStateWithRetry();
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
// ⭐ 依赖变化时(如从后台返回)重新同步
WidgetsBinding.instance.addPostFrameCallback((_) {
if (mounted) {
context.read<AudioService>().syncPlayerStateNow();
}
});
}
void _syncStateWithRetry() {
final service = context.read<AudioService>();
// 立即同步
service.syncPlayerStateNow();
// 延迟重试
const delays = [200, 500, 800, 1200];
for (final delay in delays) {
Future.delayed(Duration(milliseconds: delay), () {
if (mounted) {
service.syncPlayerStateNow();
}
});
}
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final service = context.watch<AudioService>(); final service = context.watch<AudioService>();
@@ -75,7 +110,6 @@ class _PlayerPageState extends State<PlayerPage> {
} }
final isPlaying = service.isPlaying; final isPlaying = service.isPlaying;
final duration = service.duration;
return Scaffold( return Scaffold(
backgroundColor: const Color(0xFF0E1211), backgroundColor: const Color(0xFF0E1211),
@@ -156,7 +190,7 @@ class _PlayerPageState extends State<PlayerPage> {
], ],
), ),
const SizedBox(height: 40), const SizedBox(height: 40),
_buildProgressSection(service, duration), _buildProgressSection(service),
const SizedBox(height: 32), const SizedBox(height: 32),
_buildControlButtons(service, isPlaying), _buildControlButtons(service, isPlaying),
const SizedBox(height: 16), const SizedBox(height: 16),
@@ -166,118 +200,137 @@ class _PlayerPageState extends State<PlayerPage> {
); );
} }
Widget _buildProgressSection(AudioService service, Duration duration) { Widget _buildProgressSection(AudioService service) {
return ValueListenableBuilder( return ValueListenableBuilder(
valueListenable: service.positionNotifier, valueListenable: service.positionNotifier,
builder: (context, position, _) { builder: (context, position, _) {
final progress = duration.inMilliseconds > 0 return ValueListenableBuilder(
? position.inMilliseconds / duration.inMilliseconds valueListenable: service.durationNotifier,
: 0.0; builder: (context, duration, _) {
// ⭐ 如果 duration 为 0,显示加载指示器
if (duration.inMilliseconds == 0) {
return const Padding(
padding: EdgeInsets.symmetric(vertical: 8),
child: LinearProgressIndicator(
color: Color(0xFFB8D4D0),
backgroundColor: Color(0xFF2A3332),
),
);
}
final bufferProgress = duration.inMilliseconds > 0 final progress = duration.inMilliseconds > 0
? (service.bufferedNotifier.value.inMilliseconds / ? position.inMilliseconds / duration.inMilliseconds
duration.inMilliseconds) : 0.0;
.clamp(0.0, 1.0)
: 0.0;
final displayProgress = _dragProgress ?? progress; final bufferProgress = duration.inMilliseconds > 0
? (service.bufferedNotifier.value.inMilliseconds /
duration.inMilliseconds)
.clamp(0.0, 1.0)
: 0.0;
return Column( final displayProgress = _dragProgress ?? progress;
children: [
SizedBox(
height: 20,
child: LayoutBuilder(
builder: (context, constraints) {
final width = constraints.maxWidth;
final bufferWidth = width * bufferProgress.clamp(0.0, 1.0);
final progressWidth = width * displayProgress.clamp(0.0, 1.0);
return Stack( return Column(
alignment: Alignment.centerLeft, children: [
children: [ SizedBox(
Container( height: 20,
width: width, child: LayoutBuilder(
height: 4, builder: (context, constraints) {
decoration: BoxDecoration( final width = constraints.maxWidth;
color: Colors.grey[800], final bufferWidth =
borderRadius: BorderRadius.circular(2), width * bufferProgress.clamp(0.0, 1.0);
), final progressWidth =
), width * displayProgress.clamp(0.0, 1.0);
Container(
width: bufferWidth, return Stack(
height: 4, alignment: Alignment.centerLeft,
decoration: BoxDecoration( children: [
color: const Color(0xFF4A7A7A), Container(
borderRadius: BorderRadius.circular(2), width: width,
), height: 4,
), decoration: BoxDecoration(
Container( color: Colors.grey[800],
width: progressWidth, borderRadius: BorderRadius.circular(2),
height: 4,
decoration: BoxDecoration(
color: const Color(0xFFB8D4D0),
borderRadius: BorderRadius.circular(2),
),
),
Positioned.fill(
child: SliderTheme(
data: SliderTheme.of(context).copyWith(
trackHeight: 0,
activeTrackColor: Colors.transparent,
inactiveTrackColor: Colors.transparent,
thumbColor: Colors.transparent,
overlayColor: Colors.transparent,
thumbShape: const RoundSliderThumbShape(
enabledThumbRadius: 0,
), ),
), ),
child: Slider( Container(
value: displayProgress.clamp(0.0, 1.0), width: bufferWidth,
min: 0.0, height: 4,
max: 1.0, decoration: BoxDecoration(
onChanged: (value) { color: const Color(0xFF4A7A7A),
setState(() { borderRadius: BorderRadius.circular(2),
_dragProgress = value; ),
});
},
onChangeEnd: (value) {
final newPosition = Duration(
milliseconds:
(value * duration.inMilliseconds).round(),
);
service.seekTo(newPosition);
setState(() {
_dragProgress = null;
});
},
), ),
), Container(
), width: progressWidth,
], height: 4,
); decoration: BoxDecoration(
}, color: const Color(0xFFB8D4D0),
), borderRadius: BorderRadius.circular(2),
), ),
Row( ),
mainAxisAlignment: MainAxisAlignment.spaceBetween, Positioned.fill(
children: [ child: SliderTheme(
Text( data: SliderTheme.of(context).copyWith(
_formatDuration(position), trackHeight: 0,
style: TextStyle( activeTrackColor: Colors.transparent,
fontSize: 12, inactiveTrackColor: Colors.transparent,
color: Colors.grey[500], thumbColor: Colors.transparent,
overlayColor: Colors.transparent,
thumbShape: const RoundSliderThumbShape(
enabledThumbRadius: 0,
),
),
child: Slider(
value: displayProgress.clamp(0.0, 1.0),
min: 0.0,
max: 1.0,
onChanged: (value) {
setState(() {
_dragProgress = value;
});
},
onChangeEnd: (value) {
final newPosition = Duration(
milliseconds:
(value * duration.inMilliseconds)
.round(),
);
service.seekTo(newPosition);
setState(() {
_dragProgress = null;
});
},
),
),
),
],
);
},
), ),
), ),
Text( Row(
_formatDuration(duration), mainAxisAlignment: MainAxisAlignment.spaceBetween,
style: TextStyle( children: [
fontSize: 12, Text(
color: Colors.grey[500], _formatDuration(position),
), style: TextStyle(
fontSize: 12,
color: Colors.grey[500],
),
),
Text(
_formatDuration(duration),
style: TextStyle(
fontSize: 12,
color: Colors.grey[500],
),
),
],
), ),
], ],
), );
], },
); );
}, },
); );
+1
View File
@@ -1,4 +1,5 @@
// lib/pages/webdav_file_list_page.dart // lib/pages/webdav_file_list_page.dart
// ignore: unused_import
import 'dart:convert'; import 'dart:convert';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
+1
View File
@@ -39,6 +39,7 @@ class AudioPlayerHandler extends audio_service.BaseAudioHandler {
}); });
_player.durationStream.listen((duration) { _player.durationStream.listen((duration) {
debugPrint('🎯 [durationStream] duration=$duration');
_state.updateDuration(duration); _state.updateDuration(duration);
if (_currentId != null && _currentTitle != null) { if (_currentId != null && _currentTitle != null) {
_updateMediaItem( _updateMediaItem(
+75 -26
View File
@@ -3,6 +3,7 @@ import 'dart:async';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:media_kit/media_kit.dart'; import 'package:media_kit/media_kit.dart';
import 'playback_service.dart'; import 'playback_service.dart';
import '../metadata/metadata_service.dart';
enum PlayMode { enum PlayMode {
sequential, sequential,
@@ -29,7 +30,7 @@ class AudioService extends ChangeNotifier {
factory AudioService() => _instance; factory AudioService() => _instance;
AudioService._internal(); AudioService._internal();
// ---- 基础状态(低频,触发 UI 重建) ---- // ---- 基础状态 ----
Song? _currentSong; Song? _currentSong;
bool _isPlaying = false; bool _isPlaying = false;
PlayMode _playMode = PlayMode.sequential; PlayMode _playMode = PlayMode.sequential;
@@ -40,7 +41,7 @@ class AudioService extends ChangeNotifier {
List<int> _shuffledIndices = []; List<int> _shuffledIndices = [];
int _shuffledIndex = -1; int _shuffledIndex = -1;
// ---- 高频进度(用 ValueNotifier,不触发全局重建) ---- // ---- 高频进度 ----
final ValueNotifier<Duration> positionNotifier = ValueNotifier(Duration.zero); final ValueNotifier<Duration> positionNotifier = ValueNotifier(Duration.zero);
final ValueNotifier<Duration> durationNotifier = ValueNotifier(Duration.zero); final ValueNotifier<Duration> durationNotifier = ValueNotifier(Duration.zero);
final ValueNotifier<Duration> bufferedNotifier = ValueNotifier(Duration.zero); final ValueNotifier<Duration> bufferedNotifier = ValueNotifier(Duration.zero);
@@ -48,13 +49,10 @@ class AudioService extends ChangeNotifier {
bool _listening = false; bool _listening = false;
final List<StreamSubscription> _subscriptions = []; final List<StreamSubscription> _subscriptions = [];
// ⭐ 防重入标志
bool _handlingCompletion = false; bool _handlingCompletion = false;
// ⭐ 歌曲切换回调(用于通知 Handler 更新 MediaItem
void Function(Song)? _onSongChanged; void Function(Song)? _onSongChanged;
// ---- Getter(高频字段不走 ChangeNotifier ---- // ---- Getter ----
Song? get currentSong => _currentSong; Song? get currentSong => _currentSong;
bool get isPlaying => _isPlaying; bool get isPlaying => _isPlaying;
PlayMode get playMode => _playMode; PlayMode get playMode => _playMode;
@@ -62,7 +60,6 @@ class AudioService extends ChangeNotifier {
int get currentIndex => _currentIndex; int get currentIndex => _currentIndex;
bool get hasQueue => _queue.isNotEmpty; bool get hasQueue => _queue.isNotEmpty;
// ---- 兼容旧代码:提供 getter 返回 ValueNotifier 的值 ----
Duration get position => positionNotifier.value; Duration get position => positionNotifier.value;
Duration get duration => durationNotifier.value; Duration get duration => durationNotifier.value;
Duration get bufferedPosition => bufferedNotifier.value; Duration get bufferedPosition => bufferedNotifier.value;
@@ -78,12 +75,10 @@ class AudioService extends ChangeNotifier {
} }
} }
// ---- 注册歌曲切换回调 ----
void setOnSongChanged(void Function(Song) callback) { void setOnSongChanged(void Function(Song) callback) {
_onSongChanged = callback; _onSongChanged = callback;
} }
// ---- 切换播放模式 ----
void togglePlayMode() { void togglePlayMode() {
switch (_playMode) { switch (_playMode) {
case PlayMode.sequential: case PlayMode.sequential:
@@ -99,7 +94,6 @@ class AudioService extends ChangeNotifier {
notifyListeners(); notifyListeners();
} }
// ---- 设置播放队列 ----
void setQueue(List<Song> queue, {int startIndex = 0}) { void setQueue(List<Song> queue, {int startIndex = 0}) {
if (queue.isEmpty) { if (queue.isEmpty) {
_clearQueue(); _clearQueue();
@@ -128,7 +122,6 @@ class AudioService extends ChangeNotifier {
stopPlay(); stopPlay();
} }
// ---- 播放指定歌曲 ----
Future<void> playSong(Song song) async { Future<void> playSong(Song song) async {
if (_queue.isEmpty || _queue[_currentIndex].id != song.id) { if (_queue.isEmpty || _queue[_currentIndex].id != song.id) {
setQueue([song], startIndex: 0); setQueue([song], startIndex: 0);
@@ -146,14 +139,6 @@ class AudioService extends ChangeNotifier {
final song = _queue[_currentIndex]; final song = _queue[_currentIndex];
_currentSong = song; _currentSong = song;
// ⭐ 切歌时触发回调(通知 Handler 更新 MediaItem
_onSongChanged?.call(song);
// ⭐ 重置进度(用 ValueNotifier
positionNotifier.value = Duration.zero;
durationNotifier.value = Duration.zero;
bufferedNotifier.value = Duration.zero;
_startListening(); _startListening();
notifyListeners(); notifyListeners();
@@ -162,9 +147,42 @@ class AudioService extends ChangeNotifier {
} }
PlaybackService().play(song.url!); PlaybackService().play(song.url!);
// ⭐ 延迟同步兜底
_syncPlayerStateDelayed();
_loadMetadataForCurrentSong();
}
Future<void> _loadMetadataForCurrentSong() async {
if (_currentIndex < 0 || _currentIndex >= _queue.length) return;
final song = _queue[_currentIndex];
if (song.url == null || song.url!.isEmpty) return;
try {
final metadata = await MetadataService().getMetadata(
url: song.url!,
fileName: song.title,
fileId: song.id,
);
if (metadata.isNotEmpty) {
final updatedSong = Song(
id: song.id,
title: metadata.title.isNotEmpty ? metadata.title : song.title,
artist: metadata.artist.isNotEmpty ? metadata.artist : song.artist,
url: song.url,
);
_queue[_currentIndex] = updatedSong;
_currentSong = updatedSong;
notifyListeners();
_onSongChanged?.call(updatedSong);
}
} catch (e) {
debugPrint('⚠️ [AudioService] metadata load failed: $e');
}
} }
// ---- 下一首 ----
void next() { void next() {
if (_queue.isEmpty) return; if (_queue.isEmpty) return;
@@ -182,7 +200,6 @@ class AudioService extends ChangeNotifier {
_playCurrent(); _playCurrent();
} }
// ---- 上一首 ----
void previous() { void previous() {
if (_queue.isEmpty) return; if (_queue.isEmpty) return;
@@ -208,7 +225,6 @@ class AudioService extends ChangeNotifier {
_playCurrent(); _playCurrent();
} }
// ---- 播放/暂停 ----
void togglePlay() { void togglePlay() {
if (_currentSong == null) return; if (_currentSong == null) return;
@@ -243,7 +259,39 @@ class AudioService extends ChangeNotifier {
notifyListeners(); notifyListeners();
} }
// ---- 监听 media_kit 状态 ---- // ⭐ 公开方法:供 PlayerPage 主动同步
void syncPlayerStateNow() {
final player = PlaybackService().player;
final pos = player.state.position;
final dur = player.state.duration;
final buf = player.state.buffer;
debugPrint(
'🎯 [AudioService] syncPlayerStateNow: pos=$pos, dur=$dur, buf=$buf');
// ⭐ 无条件更新(不要只判断 == 0)
if (pos.inMilliseconds >= 0) {
positionNotifier.value = pos;
}
if (dur.inMilliseconds > 0) {
durationNotifier.value = dur;
}
if (buf.inMilliseconds >= 0) {
bufferedNotifier.value = buf;
}
}
void _syncPlayerStateDelayed() {
syncPlayerStateNow();
Future.delayed(const Duration(milliseconds: 200), () {
syncPlayerStateNow();
});
Future.delayed(const Duration(milliseconds: 500), () {
syncPlayerStateNow();
});
}
// ---- 监听 ----
void _startListening() { void _startListening() {
if (_listening) return; if (_listening) return;
_listening = true; _listening = true;
@@ -267,7 +315,10 @@ class AudioService extends ChangeNotifier {
_subscriptions.add( _subscriptions.add(
player.stream.duration.listen((duration) { player.stream.duration.listen((duration) {
durationNotifier.value = duration; debugPrint('🎯 [AudioService] durationStream: $duration');
if (duration.inMilliseconds > 0) {
durationNotifier.value = duration;
}
}), }),
); );
@@ -277,7 +328,6 @@ class AudioService extends ChangeNotifier {
}), }),
); );
// ⭐ 唯一监听 completed 的地方(带防重入)
_subscriptions.add( _subscriptions.add(
player.stream.completed.listen((_) { player.stream.completed.listen((_) {
_onPlaybackCompleted(); _onPlaybackCompleted();
@@ -293,7 +343,6 @@ class AudioService extends ChangeNotifier {
_subscriptions.clear(); _subscriptions.clear();
} }
// ⭐ 防重入的完成事件处理
void _onPlaybackCompleted() { void _onPlaybackCompleted() {
if (_handlingCompletion) { if (_handlingCompletion) {
debugPrint('⚠️ [service] completed ignored: already handling'); debugPrint('⚠️ [service] completed ignored: already handling');
+24
View File
@@ -25,6 +25,14 @@ packages:
url: "https://pub.flutter-io.cn" url: "https://pub.flutter-io.cn"
source: hosted source: hosted
version: "2.13.1" version: "2.13.1"
audio_metadata_reader:
dependency: "direct main"
description:
name: audio_metadata_reader
sha256: "79b08282447dfc4b7ff955f9c4eff824d7284c7b021ccfe8204550d8801a08a5"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.7.1"
audio_service: audio_service:
dependency: "direct main" dependency: "direct main"
description: description:
@@ -73,6 +81,14 @@ packages:
url: "https://pub.flutter-io.cn" url: "https://pub.flutter-io.cn"
source: hosted source: hosted
version: "1.4.1" version: "1.4.1"
charset:
dependency: transitive
description:
name: charset
sha256: "27802032a581e01ac565904ece8c8962564b1070690794f0072f6865958ce8b9"
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.0.1"
clock: clock:
dependency: transitive dependency: transitive
description: description:
@@ -224,6 +240,14 @@ packages:
url: "https://pub.flutter-io.cn" url: "https://pub.flutter-io.cn"
source: hosted source: hosted
version: "4.8.0" version: "4.8.0"
intl:
dependency: transitive
description:
name: intl
sha256: "1ca20c894b1717686a2319b8548763d812bc0aabdac580420a44c5178c57a867"
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.20.3"
jni: jni:
dependency: transitive dependency: transitive
description: description:
+1
View File
@@ -41,6 +41,7 @@ dependencies:
permission_handler: ^11.3.1 permission_handler: ^11.3.1
flutter_cache_manager: ^3.3.1 flutter_cache_manager: ^3.3.1
sqflite: ^2.3.0 sqflite: ^2.3.0
audio_metadata_reader: ^1.7.1
# The following adds the Cupertino Icons font to your application. # The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons. # Use with the CupertinoIcons class for iOS style icons.