From be49823b6049c6bd3fb83dbdcb3f82ce04247ae5 Mon Sep 17 00:00:00 2001 From: lxh2875931338 Date: Sun, 23 Aug 2026 18:58:43 +0800 Subject: [PATCH] =?UTF-8?q?=E6=92=AD=E6=94=BE=E8=BF=9B=E5=BA=A6=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E5=B7=B2=E8=BE=BE=E6=88=90=EF=BC=8C=E5=87=86=E5=A4=87?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=80=9A=E7=9F=A5=E4=B8=AD=E5=BF=83=E7=9A=84?= =?UTF-8?q?=E8=BF=9B=E5=BA=A6=E6=9D=A1=E6=8B=96=E5=8A=A8=E7=82=B9=E5=87=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/services/audio_player_handler.dart | 80 +++++++++++++++++++------- pubspec.yaml | 2 +- 2 files changed, 61 insertions(+), 21 deletions(-) diff --git a/lib/services/audio_player_handler.dart b/lib/services/audio_player_handler.dart index 7464b62..83e7ceb 100644 --- a/lib/services/audio_player_handler.dart +++ b/lib/services/audio_player_handler.dart @@ -14,21 +14,30 @@ class AudioPlayerHandler extends audio_service.BaseAudioHandler { String? _currentTitle; String? _currentArtist; + // ⭐ 唯一的位置变量 + Duration _currentPosition = Duration.zero; + DateTime _lastPublishTime = DateTime.now(); + static const Duration _publishInterval = Duration(milliseconds: 500); + AudioPlayerHandler() { - // ---- 播放状态 ---- _player.playingStream.listen((playing) { _state.updatePlaying(playing); _publishState(); }); - // ---- 进度变化 ---- + // ⭐ 位置更新:直接赋值给 _currentPosition _player.positionStream.listen((position) { + _currentPosition = position; _state.updatePosition(position); - // ⭐ 0.18.19 版本:通过更新 PlaybackState 的 updateTime 来驱动进度刷新 - _publishStateOnly(); + + final now = DateTime.now(); + if (now.difference(_lastPublishTime) >= _publishInterval) { + _lastPublishTime = now; + _updateMediaItemPosition(position); + _publishStateOnly(); + } }); - // ---- 时长变化 ---- _player.durationStream.listen((duration) { _state.updateDuration(duration); if (_currentId != null && _currentTitle != null) { @@ -43,24 +52,51 @@ class AudioPlayerHandler extends audio_service.BaseAudioHandler { }); } - // ---- 发布完整状态 ---- - void _publishState() { - playbackState.add(_state.playbackState); + void _updateMediaItemPosition(Duration position) { + final currentMediaItem = mediaItem.value; + if (currentMediaItem != null) { + mediaItem.add(audio_service.MediaItem( + id: currentMediaItem.id, + title: currentMediaItem.title, + artist: currentMediaItem.artist, + duration: currentMediaItem.duration, + extras: { + 'position': position.inMilliseconds, + ...?currentMediaItem.extras, + }, + )); + } } - // ---- 0.18.19 版本:只更新时间戳,驱动系统刷新进度 ---- - void _publishStateOnly() { - final current = playbackState.value; + void _publishState() { + final state = _state.playbackState; playbackState.add(audio_service.PlaybackState( - controls: current.controls, - processingState: current.processingState, + controls: state.controls, + processingState: state.processingState, + playing: state.playing, + androidCompactActionIndices: state.androidCompactActionIndices, + updateTime: DateTime.now(), + )); + } + + void _publishStateOnly() { + final current = playbackState.value; + // ⭐ 使用类成员 _currentPosition,不再重复声明 + debugPrint( + '📡 [publishStateOnly] position=$_currentPosition, playing=${current.playing}'); + + playbackState.add(audio_service.PlaybackState( + controls: current.controls.isNotEmpty + ? current.controls + : _state.playbackState.controls, + processingState: _state.playbackState.processingState, playing: current.playing, androidCompactActionIndices: current.androidCompactActionIndices, - updateTime: DateTime.now(), // ⭐ 关键:更新时间戳,系统会重新读取位置 + updatePosition: _currentPosition, // ⭐ 尝试加上 + updateTime: DateTime.now(), )); } - // ---- 更新媒体信息 ---- void _updateMediaItem({ required String id, required String title, @@ -70,18 +106,22 @@ class AudioPlayerHandler extends audio_service.BaseAudioHandler { _currentId = id; _currentTitle = title; _currentArtist = artist; + final position = _player.position; - debugPrint('📢 [handler] updateMediaItem: $title - $artist'); + debugPrint( + '📢 [handler] updateMediaItem: $title - $artist (position=${position.inSeconds}s)'); mediaItem.add(audio_service.MediaItem( id: id, title: title, artist: artist, duration: duration ?? _player.duration, + extras: { + 'position': position.inMilliseconds, + }, )); } - // ---- 外部接口 ---- void updateNotification({ required String id, required String title, @@ -91,7 +131,6 @@ class AudioPlayerHandler extends audio_service.BaseAudioHandler { _publishState(); } - // ---- 控制命令 ---- @override Future play() async { debugPrint('▶️ [handler] play() called'); @@ -116,7 +155,9 @@ class AudioPlayerHandler extends audio_service.BaseAudioHandler { @override Future seek(Duration position) async { debugPrint('⏩ [handler] seek() called: $position'); - _player.seek(position); + await _player.seek(position); + _currentPosition = position; + _updateMediaItemPosition(position); _publishStateOnly(); } @@ -152,7 +193,6 @@ class AudioPlayerHandler extends audio_service.BaseAudioHandler { } break; default: - // 其他按钮 fallback if (_player.isPlaying) { await pause(); } else { diff --git a/pubspec.yaml b/pubspec.yaml index 129637e..f7e8f9e 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -36,7 +36,7 @@ dependencies: provider: ^6.1.2 dio: ^5.4.0 # HTTP 客户端 xml: ^6.5.0 - audio_service: 0.18.19 + audio_service: ^0.18.19 audio_session: ^0.1.21 permission_handler: ^11.3.1 flutter_cache_manager: ^3.3.1