A cross platform Audiobookshelf client for Android and iOS
Dart
Stop phantom resume after headphones unplug or Android Auto disconnect
Barnabas5aa11621d1ef
diff --git a/lib/services/audio_player_service.dart b/lib/services/audio_player_service.dartindex fbb40f1d..44349693 100644--- a/lib/services/audio_player_service.dart+++ b/lib/services/audio_player_service.dart@@ -223,6 +223,18 @@ class AudioPlayerHandler extends BaseAudioHandler with SeekHandler {Future<void> play() async {debugPrint('[Handler] play() called - routing to service (state=${_player.processingState.name})');debugPrint('[ClickDebug] play() entry: ${_clickDebugSnapshot()}');+ // Mirror the click() guard: a raw play() arriving within 5s of a+ // headphone/AA/BT disconnect is almost always the platform echoing a+ // resume command, not the user. Drop it so playback doesn't jump to+ // the phone speaker after the user unplugs or AA tears down.+ if (_noisyPauseAt != null) {+ final elapsed = DateTime.now().difference(_noisyPauseAt!).inMilliseconds;+ if (elapsed < 5000) {+ debugPrint('[Handler] Ignoring phantom play (${elapsed}ms after platform pause)');+ return;+ }+ _noisyPauseAt = null;+ }_lastHandlerPlayAt = DateTime.now();if (_service != null) {await _service!.play();@@ -242,11 +254,21 @@ class AudioPlayerHandler extends BaseAudioHandler with SeekHandler {// then see playing=false and misinterpret it as "user wants to play",// triggering a cold-start restore. Cancel any pending click so the// platform-initiated pause wins.- if (_clickTimer?.isActive ?? false) {+ final clickPending = _clickTimer?.isActive ?? false;+ if (clickPending) {debugPrint('[Handler] Cancelling pending click (platform pause)');_clickTimer!.cancel();_clickCount = 0;}+ // A pause arriving outside the click resolver is the platform signalling+ // a disconnect (AA tearing down, BT going away, headphones unplugged).+ // Stamp _noisyPauseAt so the play() / click() guards drop any spurious+ // resume commands that follow in the next 5s. becomingNoisy already+ // stamps for the headphone-unplug case; this covers AA/BT disconnect+ // where no noisy event fires.+ if (!_inClickResolver) {+ _noisyPauseAt = DateTime.now();+ }if (_service != null) {await _service!.pause();} else {@@ -332,6 +354,10 @@ class AudioPlayerHandler extends BaseAudioHandler with SeekHandler {int _clickCount = 0;DateTime? _hardwareButtonTime; // cooldown after hardware next/prevDateTime? _noisyPauseAt; // suppress clicks for a window after BT disconnect+ // True while the click resolver is synchronously calling pause()/play().+ // pause() uses this to skip stamping _noisyPauseAt for click-driven pauses,+ // so legit user pause-then-play flows are not blocked by the disconnect guard.+ bool _inClickResolver = false;// [ClickDebug] — timestamp of the last Handler-level pause() call.// Used to correlate phantom play/click commands with a preceding pause.DateTime? _lastHandlerPauseAt;@@ -427,12 +453,17 @@ class AudioPlayerHandler extends BaseAudioHandler with SeekHandler {debugPrint('[ClickDebug] click resolve (count=$count): ${_clickDebugSnapshot()}');switch (count) {case 1:- if (_player.playing) {- debugPrint('[Handler] → single press → PAUSE');- await pause();- } else {- debugPrint('[Handler] → single press → PLAY');- await play();+ _inClickResolver = true;+ try {+ if (_player.playing) {+ debugPrint('[Handler] → single press → PAUSE');+ await pause();+ } else {+ debugPrint('[Handler] → single press → PLAY');+ await play();+ }+ } finally {+ _inClickResolver = false;}break;case 2: