播放进度更新已达成,准备优化通知中心的进度条拖动点击
This commit is contained in:
@@ -14,21 +14,30 @@ class AudioPlayerHandler extends audio_service.BaseAudioHandler {
|
|||||||
String? _currentTitle;
|
String? _currentTitle;
|
||||||
String? _currentArtist;
|
String? _currentArtist;
|
||||||
|
|
||||||
|
// ⭐ 唯一的位置变量
|
||||||
|
Duration _currentPosition = Duration.zero;
|
||||||
|
DateTime _lastPublishTime = DateTime.now();
|
||||||
|
static const Duration _publishInterval = Duration(milliseconds: 500);
|
||||||
|
|
||||||
AudioPlayerHandler() {
|
AudioPlayerHandler() {
|
||||||
// ---- 播放状态 ----
|
|
||||||
_player.playingStream.listen((playing) {
|
_player.playingStream.listen((playing) {
|
||||||
_state.updatePlaying(playing);
|
_state.updatePlaying(playing);
|
||||||
_publishState();
|
_publishState();
|
||||||
});
|
});
|
||||||
|
|
||||||
// ---- 进度变化 ----
|
// ⭐ 位置更新:直接赋值给 _currentPosition
|
||||||
_player.positionStream.listen((position) {
|
_player.positionStream.listen((position) {
|
||||||
|
_currentPosition = position;
|
||||||
_state.updatePosition(position);
|
_state.updatePosition(position);
|
||||||
// ⭐ 0.18.19 版本:通过更新 PlaybackState 的 updateTime 来驱动进度刷新
|
|
||||||
|
final now = DateTime.now();
|
||||||
|
if (now.difference(_lastPublishTime) >= _publishInterval) {
|
||||||
|
_lastPublishTime = now;
|
||||||
|
_updateMediaItemPosition(position);
|
||||||
_publishStateOnly();
|
_publishStateOnly();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// ---- 时长变化 ----
|
|
||||||
_player.durationStream.listen((duration) {
|
_player.durationStream.listen((duration) {
|
||||||
_state.updateDuration(duration);
|
_state.updateDuration(duration);
|
||||||
if (_currentId != null && _currentTitle != null) {
|
if (_currentId != null && _currentTitle != null) {
|
||||||
@@ -43,24 +52,51 @@ class AudioPlayerHandler extends audio_service.BaseAudioHandler {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---- 发布完整状态 ----
|
void _updateMediaItemPosition(Duration position) {
|
||||||
void _publishState() {
|
final currentMediaItem = mediaItem.value;
|
||||||
playbackState.add(_state.playbackState);
|
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 _publishState() {
|
||||||
void _publishStateOnly() {
|
final state = _state.playbackState;
|
||||||
final current = playbackState.value;
|
|
||||||
playbackState.add(audio_service.PlaybackState(
|
playbackState.add(audio_service.PlaybackState(
|
||||||
controls: current.controls,
|
controls: state.controls,
|
||||||
processingState: current.processingState,
|
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,
|
playing: current.playing,
|
||||||
androidCompactActionIndices: current.androidCompactActionIndices,
|
androidCompactActionIndices: current.androidCompactActionIndices,
|
||||||
updateTime: DateTime.now(), // ⭐ 关键:更新时间戳,系统会重新读取位置
|
updatePosition: _currentPosition, // ⭐ 尝试加上
|
||||||
|
updateTime: DateTime.now(),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---- 更新媒体信息 ----
|
|
||||||
void _updateMediaItem({
|
void _updateMediaItem({
|
||||||
required String id,
|
required String id,
|
||||||
required String title,
|
required String title,
|
||||||
@@ -70,18 +106,22 @@ class AudioPlayerHandler extends audio_service.BaseAudioHandler {
|
|||||||
_currentId = id;
|
_currentId = id;
|
||||||
_currentTitle = title;
|
_currentTitle = title;
|
||||||
_currentArtist = artist;
|
_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(
|
mediaItem.add(audio_service.MediaItem(
|
||||||
id: id,
|
id: id,
|
||||||
title: title,
|
title: title,
|
||||||
artist: artist,
|
artist: artist,
|
||||||
duration: duration ?? _player.duration,
|
duration: duration ?? _player.duration,
|
||||||
|
extras: {
|
||||||
|
'position': position.inMilliseconds,
|
||||||
|
},
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---- 外部接口 ----
|
|
||||||
void updateNotification({
|
void updateNotification({
|
||||||
required String id,
|
required String id,
|
||||||
required String title,
|
required String title,
|
||||||
@@ -91,7 +131,6 @@ class AudioPlayerHandler extends audio_service.BaseAudioHandler {
|
|||||||
_publishState();
|
_publishState();
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---- 控制命令 ----
|
|
||||||
@override
|
@override
|
||||||
Future<void> play() async {
|
Future<void> play() async {
|
||||||
debugPrint('▶️ [handler] play() called');
|
debugPrint('▶️ [handler] play() called');
|
||||||
@@ -116,7 +155,9 @@ class AudioPlayerHandler extends audio_service.BaseAudioHandler {
|
|||||||
@override
|
@override
|
||||||
Future<void> seek(Duration position) async {
|
Future<void> seek(Duration position) async {
|
||||||
debugPrint('⏩ [handler] seek() called: $position');
|
debugPrint('⏩ [handler] seek() called: $position');
|
||||||
_player.seek(position);
|
await _player.seek(position);
|
||||||
|
_currentPosition = position;
|
||||||
|
_updateMediaItemPosition(position);
|
||||||
_publishStateOnly();
|
_publishStateOnly();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -152,7 +193,6 @@ class AudioPlayerHandler extends audio_service.BaseAudioHandler {
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
// 其他按钮 fallback
|
|
||||||
if (_player.isPlaying) {
|
if (_player.isPlaying) {
|
||||||
await pause();
|
await pause();
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
+1
-1
@@ -36,7 +36,7 @@ dependencies:
|
|||||||
provider: ^6.1.2
|
provider: ^6.1.2
|
||||||
dio: ^5.4.0 # HTTP 客户端
|
dio: ^5.4.0 # HTTP 客户端
|
||||||
xml: ^6.5.0
|
xml: ^6.5.0
|
||||||
audio_service: 0.18.19
|
audio_service: ^0.18.19
|
||||||
audio_session: ^0.1.21
|
audio_session: ^0.1.21
|
||||||
permission_handler: ^11.3.1
|
permission_handler: ^11.3.1
|
||||||
flutter_cache_manager: ^3.3.1
|
flutter_cache_manager: ^3.3.1
|
||||||
|
|||||||
Reference in New Issue
Block a user