How audio_service works: The AudioHandler concept
minorThe audio_service plugin works by encapsulating your audio logic within an AudioHandler. This handler implements standard system callbacks that respond to media playback requests from various sources (e.g., lock screen, headset buttons, Android Auto, Apple CarPlay) in a uniform way.
You implement these callbacks to trigger your specific audio engine (like just_audio for music or flutter_tts for speech). The AudioHandler acts as the single source of truth, broadcasting state changes to both the system (notifications/controls) and your Flutter UI.
class MyAudioHandler extends BaseAudioHandler
with QueueHandler,
SeekHandler {
final _player = AudioPlayer(); // e.g. just_audio
Future<void> play() => _player.play();
Future<void> pause() => _player.pause();
Future<void> stop() => _player.stop();
Future<void> seek(Duration position) => _player.seek(position);
Future<void> skipToQueueItem(int i) => _player.seek(Duration.zero, index: i);
}