更新了部分逻辑
This commit is contained in:
@@ -10,11 +10,13 @@ class AudioPlayerHandler extends BaseAudioHandler {
|
|||||||
AudioPlayerHandler() {
|
AudioPlayerHandler() {
|
||||||
_localAudio = local_audio.AudioService();
|
_localAudio = local_audio.AudioService();
|
||||||
|
|
||||||
|
// 监听播放状态
|
||||||
_playback.player.stream.playing.listen((playing) {
|
_playback.player.stream.playing.listen((playing) {
|
||||||
_updatePlaybackState(playing: playing);
|
_updatePlaybackState(playing: playing);
|
||||||
_syncMediaItem();
|
_syncMediaItem();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// 监听播放完成 → 自动下一首
|
||||||
_playback.player.stream.completed.listen((_) {
|
_playback.player.stream.completed.listen((_) {
|
||||||
debugPrint('🎵 [handler] playback completed, auto next');
|
debugPrint('🎵 [handler] playback completed, auto next');
|
||||||
_localAudio.next();
|
_localAudio.next();
|
||||||
@@ -22,6 +24,7 @@ class AudioPlayerHandler extends BaseAudioHandler {
|
|||||||
_syncMediaItem();
|
_syncMediaItem();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// 监听时长变化 → 更新 MediaItem
|
||||||
_playback.player.stream.duration.listen((duration) {
|
_playback.player.stream.duration.listen((duration) {
|
||||||
final current = mediaItem.value;
|
final current = mediaItem.value;
|
||||||
if (current != null && current.duration != duration) {
|
if (current != null && current.duration != duration) {
|
||||||
@@ -32,9 +35,8 @@ class AudioPlayerHandler extends BaseAudioHandler {
|
|||||||
|
|
||||||
void _syncMediaItem() {
|
void _syncMediaItem() {
|
||||||
final song = _localAudio.currentSong;
|
final song = _localAudio.currentSong;
|
||||||
debugPrint(
|
|
||||||
'🎵 [handler] _syncMediaItem: currentSong = ${song?.title ?? "null"}');
|
|
||||||
if (song != null) {
|
if (song != null) {
|
||||||
|
debugPrint('🎵 [handler] syncing media item: ${song.title}');
|
||||||
mediaItem.add(MediaItem(
|
mediaItem.add(MediaItem(
|
||||||
id: song.id,
|
id: song.id,
|
||||||
title: song.title,
|
title: song.title,
|
||||||
@@ -123,34 +125,40 @@ class AudioPlayerHandler extends BaseAudioHandler {
|
|||||||
_syncMediaItem();
|
_syncMediaItem();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ⭐ 核心修复:正确区分 play 和 pause
|
||||||
@override
|
@override
|
||||||
Future<void> click([MediaButton button = MediaButton.media]) async {
|
Future<void> click([MediaButton button = MediaButton.media]) async {
|
||||||
debugPrint(
|
debugPrint('🎵 [audio_service] click(): ${button.name} (index: ${button.index})');
|
||||||
'🎵 [audio_service] click(): ${button.name} (index: ${button.index})');
|
|
||||||
|
|
||||||
final name = button.name.toLowerCase();
|
final name = button.name.toLowerCase();
|
||||||
|
|
||||||
if (name.contains('play') || name.contains('media')) {
|
// ⭐ 优先处理 pause(蓝牙 PAUSE 会进入这里)
|
||||||
debugPrint('🎵 → play() via click');
|
|
||||||
await play();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (name.contains('pause')) {
|
if (name.contains('pause')) {
|
||||||
debugPrint('🎵 → pause() via click');
|
debugPrint('🎵 → pause()');
|
||||||
await pause();
|
await pause();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 处理 play(包括 media 类型的按钮)
|
||||||
|
if (name.contains('play') || name.contains('media')) {
|
||||||
|
debugPrint('🎵 → play()');
|
||||||
|
await play();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (name.contains('next') || name.contains('skip_next')) {
|
if (name.contains('next') || name.contains('skip_next')) {
|
||||||
debugPrint('🎵 → skipToNext() via click');
|
debugPrint('🎵 → skipToNext()');
|
||||||
await skipToNext();
|
await skipToNext();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (name.contains('previous') || name.contains('skip_previous')) {
|
if (name.contains('previous') || name.contains('skip_previous')) {
|
||||||
debugPrint('🎵 → skipToPrevious() via click');
|
debugPrint('🎵 → skipToPrevious()');
|
||||||
await skipToPrevious();
|
await skipToPrevious();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// fallback:根据当前播放状态 toggle
|
||||||
debugPrint('🎵 → fallback toggle');
|
debugPrint('🎵 → fallback toggle');
|
||||||
if (_playback.player.state.playing) {
|
if (_playback.player.state.playing) {
|
||||||
await pause();
|
await pause();
|
||||||
@@ -158,4 +166,4 @@ class AudioPlayerHandler extends BaseAudioHandler {
|
|||||||
await play();
|
await play();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user