现在版本下,通知中心的进度条拖动seek能力边界问题已全部修复

This commit is contained in:
2026-08-24 23:49:32 +08:00
parent bb466ca534
commit b75ea9e4ce
2 changed files with 35 additions and 5 deletions
+3
View File
@@ -78,6 +78,9 @@ class AudioPlayerHandler extends audio_service.BaseAudioHandler {
androidCompactActionIndices: state.androidCompactActionIndices, androidCompactActionIndices: state.androidCompactActionIndices,
updatePosition: _currentPosition, updatePosition: _currentPosition,
updateTime: DateTime.now(), updateTime: DateTime.now(),
systemActions: const {
audio_service.MediaAction.seek,
},
)); ));
} }
+32 -5
View File
@@ -41,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);
@@ -52,6 +52,9 @@ class AudioService extends ChangeNotifier {
bool _handlingCompletion = false; bool _handlingCompletion = false;
void Function(Song)? _onSongChanged; void Function(Song)? _onSongChanged;
// ⭐ 防 seek 覆盖标志
bool _isUserSeeking = false;
// ---- Getter ---- // ---- Getter ----
Song? get currentSong => _currentSong; Song? get currentSong => _currentSong;
bool get isPlaying => _isPlaying; bool get isPlaying => _isPlaying;
@@ -75,10 +78,12 @@ 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:
@@ -94,6 +99,7 @@ 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();
@@ -122,6 +128,7 @@ 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);
@@ -130,6 +137,7 @@ class AudioService extends ChangeNotifier {
} }
} }
// ---- 核心:播放当前歌曲 ----
void _playCurrent() { void _playCurrent() {
if (_currentIndex < 0 || _currentIndex >= _queue.length) { if (_currentIndex < 0 || _currentIndex >= _queue.length) {
stopPlay(); stopPlay();
@@ -148,12 +156,13 @@ class AudioService extends ChangeNotifier {
PlaybackService().play(song.url!); PlaybackService().play(song.url!);
// 延迟同步兜底 // 延迟同步兜底(解决首次加载时 stream 未推送的问题)
_syncPlayerStateDelayed(); _syncPlayerStateDelayed();
_loadMetadataForCurrentSong(); _loadMetadataForCurrentSong();
} }
// ---- 加载 metadata ----
Future<void> _loadMetadataForCurrentSong() async { Future<void> _loadMetadataForCurrentSong() async {
if (_currentIndex < 0 || _currentIndex >= _queue.length) return; if (_currentIndex < 0 || _currentIndex >= _queue.length) return;
final song = _queue[_currentIndex]; final song = _queue[_currentIndex];
@@ -183,6 +192,7 @@ class AudioService extends ChangeNotifier {
} }
} }
// ---- 下一首 ----
void next() { void next() {
if (_queue.isEmpty) return; if (_queue.isEmpty) return;
@@ -200,6 +210,7 @@ class AudioService extends ChangeNotifier {
_playCurrent(); _playCurrent();
} }
// ---- 上一首 ----
void previous() { void previous() {
if (_queue.isEmpty) return; if (_queue.isEmpty) return;
@@ -225,6 +236,7 @@ class AudioService extends ChangeNotifier {
_playCurrent(); _playCurrent();
} }
// ---- 播放/暂停 ----
void togglePlay() { void togglePlay() {
if (_currentSong == null) return; if (_currentSong == null) return;
@@ -235,6 +247,7 @@ class AudioService extends ChangeNotifier {
} }
} }
// ---- 停止播放 ----
void stopPlay() { void stopPlay() {
_currentSong = null; _currentSong = null;
_isPlaying = false; _isPlaying = false;
@@ -245,11 +258,19 @@ class AudioService extends ChangeNotifier {
notifyListeners(); notifyListeners();
} }
// ---- 跳转 ----
void seekTo(Duration position) { void seekTo(Duration position) {
_isUserSeeking = true;
PlaybackService().seek(position); PlaybackService().seek(position);
positionNotifier.value = position; positionNotifier.value = position;
// 800ms 后重置标志(覆盖 media_kit 的 positionStream 推送窗口)
Future.delayed(const Duration(milliseconds: 800), () {
_isUserSeeking = false;
});
} }
// ---- 清空队列 ----
void clearQueue() { void clearQueue() {
_queue.clear(); _queue.clear();
_currentIndex = -1; _currentIndex = -1;
@@ -259,7 +280,7 @@ class AudioService extends ChangeNotifier {
notifyListeners(); notifyListeners();
} }
// ⭐ 公开方法:供 PlayerPage 主动同步 // ---- 主动状态同步(供 PlayerPage 调用) ----
void syncPlayerStateNow() { void syncPlayerStateNow() {
final player = PlaybackService().player; final player = PlaybackService().player;
final pos = player.state.position; final pos = player.state.position;
@@ -269,7 +290,6 @@ class AudioService extends ChangeNotifier {
debugPrint( debugPrint(
'🎯 [AudioService] syncPlayerStateNow: pos=$pos, dur=$dur, buf=$buf'); '🎯 [AudioService] syncPlayerStateNow: pos=$pos, dur=$dur, buf=$buf');
// ⭐ 无条件更新(不要只判断 == 0)
if (pos.inMilliseconds >= 0) { if (pos.inMilliseconds >= 0) {
positionNotifier.value = pos; positionNotifier.value = pos;
} }
@@ -281,6 +301,7 @@ class AudioService extends ChangeNotifier {
} }
} }
// ---- 延迟同步(兜底) ----
void _syncPlayerStateDelayed() { void _syncPlayerStateDelayed() {
syncPlayerStateNow(); syncPlayerStateNow();
Future.delayed(const Duration(milliseconds: 200), () { Future.delayed(const Duration(milliseconds: 200), () {
@@ -291,7 +312,7 @@ class AudioService extends ChangeNotifier {
}); });
} }
// ---- 监听 ---- // ---- 监听 media_kit 状态 ----
void _startListening() { void _startListening() {
if (_listening) return; if (_listening) return;
_listening = true; _listening = true;
@@ -309,6 +330,11 @@ class AudioService extends ChangeNotifier {
_subscriptions.add( _subscriptions.add(
player.stream.position.listen((position) { player.stream.position.listen((position) {
// ⭐ 如果是用户主动 seek,忽略这次推送(避免覆盖)
if (_isUserSeeking) {
debugPrint('🎯 [AudioService] positionStream ignored: user seeking');
return;
}
positionNotifier.value = position; positionNotifier.value = position;
}), }),
); );
@@ -343,6 +369,7 @@ 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');