播放进度更新已达成,准备优化通知中心的进度条拖动点击
This commit is contained in:
@@ -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<void> play() async {
|
||||
debugPrint('▶️ [handler] play() called');
|
||||
@@ -116,7 +155,9 @@ class AudioPlayerHandler extends audio_service.BaseAudioHandler {
|
||||
@override
|
||||
Future<void> 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 {
|
||||
|
||||
+1
-1
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user