132 lines
3.5 KiB
Dart
132 lines
3.5 KiB
Dart
// lib/services/audio_player_handler.dart
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:audio_service/audio_service.dart' as audio_service;
|
|
import '../audio/player_controller.dart';
|
|
import '../audio/playback_state_manager.dart';
|
|
import '../services/audio_service.dart'; // 你的业务 AudioService
|
|
|
|
class AudioPlayerHandler extends audio_service.BaseAudioHandler {
|
|
final PlayerController _player = PlayerController();
|
|
final PlaybackStateManager _state = PlaybackStateManager();
|
|
|
|
AudioPlayerHandler() {
|
|
_player.playingStream.listen((playing) {
|
|
_state.updatePlaying(playing);
|
|
_publishState();
|
|
});
|
|
|
|
_player.positionStream.listen((position) {
|
|
_state.updatePosition(position);
|
|
final current = playbackState.value;
|
|
playbackState.add(audio_service.PlaybackState(
|
|
controls: current.controls,
|
|
processingState: current.processingState,
|
|
playing: current.playing,
|
|
androidCompactActionIndices: current.androidCompactActionIndices,
|
|
updateTime: DateTime.now(),
|
|
));
|
|
});
|
|
|
|
_player.durationStream.listen((duration) {
|
|
_state.updateDuration(duration);
|
|
_publishState();
|
|
});
|
|
|
|
_player.completedStream.listen((_) {
|
|
debugPrint('⏭️ [handler] completed, auto next');
|
|
AudioService().next();
|
|
});
|
|
}
|
|
|
|
void _publishState() {
|
|
final state = _state.playbackState;
|
|
|
|
debugPrint(
|
|
'📡 [publish] '
|
|
'playing=${state.playing}, '
|
|
'controls=${state.controls.map((e) => e.action).toList()}',
|
|
);
|
|
|
|
playbackState.add(state);
|
|
}
|
|
|
|
void updateNotification({
|
|
required String id,
|
|
required String title,
|
|
required String artist,
|
|
}) {
|
|
debugPrint('📢 [handler] updateNotification: $title');
|
|
mediaItem.add(audio_service.MediaItem(
|
|
id: id,
|
|
title: title,
|
|
artist: artist,
|
|
duration: _player.duration,
|
|
));
|
|
}
|
|
|
|
// ---- 控制命令(系统回调) ----
|
|
@override
|
|
Future<void> play() async {
|
|
debugPrint('▶️ [handler] play() called');
|
|
await _player.play();
|
|
}
|
|
|
|
@override
|
|
Future<void> pause() async {
|
|
debugPrint('⏸️ [handler] pause() called');
|
|
await _player.pause();
|
|
}
|
|
|
|
@override
|
|
Future<void> stop() async {
|
|
debugPrint('⏹️ [handler] stop() called');
|
|
_player.stop();
|
|
}
|
|
|
|
@override
|
|
Future<void> seek(Duration position) async {
|
|
debugPrint('⏩ [handler] seek() called: $position');
|
|
_player.seek(position);
|
|
}
|
|
|
|
@override
|
|
Future<void> skipToNext() async {
|
|
debugPrint('⏭️ [handler] skipToNext() called');
|
|
AudioService().next();
|
|
}
|
|
|
|
@override
|
|
Future<void> skipToPrevious() async {
|
|
debugPrint('⏮️ [handler] skipToPrevious() called');
|
|
AudioService().previous();
|
|
}
|
|
|
|
// ⭐ 统一点击处理(仅使用 MediaButton 存在的枚举)
|
|
@override
|
|
Future<void> click(
|
|
[audio_service.MediaButton button =
|
|
audio_service.MediaButton.media]) async {
|
|
debugPrint('🎯 [handler] click() called with button: ${button.name}');
|
|
switch (button) {
|
|
case audio_service.MediaButton.next:
|
|
await skipToNext();
|
|
break;
|
|
case audio_service.MediaButton.previous:
|
|
await skipToPrevious();
|
|
break;
|
|
case audio_service.MediaButton.media:
|
|
// 播放/暂停切换
|
|
if (_player.isPlaying) {
|
|
await pause();
|
|
} else {
|
|
await play();
|
|
}
|
|
break;
|
|
default:
|
|
// 其他按钮(fastForward, rewind等)忽略或fallback
|
|
debugPrint('⚠️ [handler] unhandled MediaButton: $button');
|
|
break;
|
|
}
|
|
}
|
|
}
|