当前固定audio_service为0.18.19,同时已优化媒体信息推送的部分能力,尚在进行媒体信息-播放进度的更新

This commit is contained in:
2026-08-23 18:21:49 +08:00
parent 4ebc0e3f8c
commit 1a07664f62
4 changed files with 83 additions and 24 deletions
+13 -2
View File
@@ -51,6 +51,9 @@ class AudioService extends ChangeNotifier {
// ⭐ 防重入标志
bool _handlingCompletion = false;
// ⭐ 歌曲切换回调(用于通知 Handler 更新 MediaItem
void Function(Song)? _onSongChanged;
// ---- Getter(高频字段不走 ChangeNotifier ----
Song? get currentSong => _currentSong;
bool get isPlaying => _isPlaying;
@@ -75,6 +78,11 @@ class AudioService extends ChangeNotifier {
}
}
// ---- 注册歌曲切换回调 ----
void setOnSongChanged(void Function(Song) callback) {
_onSongChanged = callback;
}
// ---- 切换播放模式 ----
void togglePlayMode() {
switch (_playMode) {
@@ -138,6 +146,9 @@ class AudioService extends ChangeNotifier {
final song = _queue[_currentIndex];
_currentSong = song;
// ⭐ 切歌时触发回调(通知 Handler 更新 MediaItem
_onSongChanged?.call(song);
// ⭐ 重置进度(用 ValueNotifier
positionNotifier.value = Duration.zero;
durationNotifier.value = Duration.zero;
@@ -266,7 +277,7 @@ class AudioService extends ChangeNotifier {
}),
);
// ⭐ 唯一监听 completed 的地方
// ⭐ 唯一监听 completed 的地方(带防重入)
_subscriptions.add(
player.stream.completed.listen((_) {
_onPlaybackCompleted();
@@ -282,7 +293,7 @@ class AudioService extends ChangeNotifier {
_subscriptions.clear();
}
// ⭐ 防重入
// ⭐ 防重入的完成事件处理
void _onPlaybackCompleted() {
if (_handlingCompletion) {
debugPrint('⚠️ [service] completed ignored: already handling');