Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Add verbose [Battery] debug logging for background playback audit

Nathan Poulsonad30411f0f20
diff --git a/lib/services/audio_player_service.dart b/lib/services/audio_player_service.dart
index 298d7883..3b38a22e 100644
--- a/lib/services/audio_player_service.dart
+++ b/lib/services/audio_player_service.dart
@@ -82,6 +82,8 @@ class AudioPlayerHandler extends BaseAudioHandler with SeekHandler {
final elapsed = now.difference(_lastPlaybackStateUpdate);
if (playingChanged || processingChanged || elapsed.inSeconds >= 5) {
+ final reason = playingChanged ? 'play/pause' : processingChanged ? 'procState=${_player.processingState}' : '5s tick';
+ debugPrint('[Battery] PlaybackState pushed ($reason, pos=${_player.position.inSeconds}s)');
playbackState.add(state);
_lastPlaybackStateUpdate = now;
_lastPlaying = _player.playing;
@@ -1820,6 +1822,8 @@ class AudioPlayerService extends ChangeNotifier {
}
int _lastSyncSecond = -1;
+ int _posStreamTickCount = 0;
+ DateTime _lastPosStreamLog = DateTime.now();
StreamSubscription? _eqSessionSub;
@@ -1870,7 +1874,7 @@ class AudioPlayerService extends ChangeNotifier {
// listener saves every 5s; this only matters when that stream goes silent.
debugPrint('[Battery] bgSaveTimer STARTED (60s interval)');
_bgSaveTimer = Timer.periodic(const Duration(seconds: 60), (_) async {
- // [Battery] bgSaveTimer TICK - removed to reduce log noise
+ debugPrint('[Battery] bgSaveTimer TICK (playing=${_player?.playing}, item=$_currentItemId)');
if (_currentItemId == null || _player == null || !_player!.playing) return;
final pos = position;
final posSec = pos.inMilliseconds / 1000.0;
@@ -1910,6 +1914,15 @@ class AudioPlayerService extends ChangeNotifier {
final sec = absolutePos.inSeconds;
final posSec = absolutePos.inMilliseconds / 1000.0;
+ // [Battery] Heartbeat: log position stream activity every 30s
+ _posStreamTickCount++;
+ final now = DateTime.now();
+ if (now.difference(_lastPosStreamLog).inSeconds >= 30) {
+ debugPrint('[Battery] positionStream heartbeat: $_posStreamTickCount ticks in ${now.difference(_lastPosStreamLog).inSeconds}s, pos=${posSec.toStringAsFixed(1)}s');
+ _posStreamTickCount = 0;
+ _lastPosStreamLog = now;
+ }
+
// ─── Position-reset guard ────────────────────────────
// ExoPlayer can seek to 0 on STATE_ENDED. If we were near the end
// and suddenly jump to near 0 without a user seek, treat it as
@@ -1967,6 +1980,7 @@ class AudioPlayerService extends ChangeNotifier {
}
if (chapterIdx >= 0 && chapterIdx != _lastNotifiedChapterIndex) {
+ debugPrint('[Battery] Chapter change: idx=$chapterIdx "$chapterTitle" at ${posSec.toStringAsFixed(1)}s');
_lastNotifiedChapterIndex = chapterIdx;
_currentChapterStart = chapterStart;
_currentChapterEnd = chapterEnd;
@@ -2056,6 +2070,7 @@ class AudioPlayerService extends ChangeNotifier {
debugPrint('[Battery] stuckCheckTimer STARTED (10s interval)');
_stuckCheckTimer = Timer.periodic(const Duration(seconds: 10), (_) async {
+ debugPrint('[Battery] stuckCheckTimer TICK (playing=${_player?.playing})');
// Only check while actively playing
if (_player == null || !_player!.playing) {
_stuckCheckLastPosition = -1;
@@ -2210,6 +2225,7 @@ class AudioPlayerService extends ChangeNotifier {
Future<void> _saveProgressLocal(Duration pos) async {
if (_currentItemId == null) return;
final ct = pos.inMilliseconds / 1000.0;
+ debugPrint('[Battery] _saveProgressLocal: ${ct.toStringAsFixed(1)}s');
// Use compound key for podcast episodes
final progressKey = _currentEpisodeId != null
? '$_currentItemId-$_currentEpisodeId'
@@ -2315,7 +2331,7 @@ class AudioPlayerService extends ChangeNotifier {
debugPrint('[Battery] bgSaveTimer RESTARTED (play)');
_bgSaveTimer?.cancel();
_bgSaveTimer = Timer.periodic(const Duration(seconds: 60), (_) async {
- // [Battery] bgSaveTimer TICK - removed to reduce log noise
+ debugPrint('[Battery] bgSaveTimer TICK (playing=${_player?.playing}, item=$_currentItemId)');
if (_currentItemId == null || _player == null || !_player!.playing) return;
final pos = position;
final posSec = pos.inMilliseconds / 1000.0;
diff --git a/lib/services/home_widget_service.dart b/lib/services/home_widget_service.dart
index 2c79b092..402800ef 100644
--- a/lib/services/home_widget_service.dart
+++ b/lib/services/home_widget_service.dart
@@ -297,7 +297,7 @@ class HomeWidgetService {
if (_progressTimer?.isActive == true) return;
debugPrint('[Battery] HomeWidget progressTimer STARTED (60s interval)');
_progressTimer = Timer.periodic(const Duration(seconds: 60), (_) {
- // [Battery] HomeWidget TICK - removed to reduce log noise
+ debugPrint('[Battery] HomeWidget progressTimer TICK');
_scheduleUpdate();
});
}
diff --git a/lib/services/sleep_timer_service.dart b/lib/services/sleep_timer_service.dart
index 508deebb..88b9b347 100644
--- a/lib/services/sleep_timer_service.dart
+++ b/lib/services/sleep_timer_service.dart
@@ -194,6 +194,9 @@ class SleepTimerService extends ChangeNotifier {
// Only count down when playing
if (isPlaying) {
_timeRemaining -= const Duration(seconds: 1);
+ if (_timeRemaining.inSeconds % 30 == 0) {
+ debugPrint('[Battery] SleepTimer TICK: ${_timeRemaining.inMinutes}m${_timeRemaining.inSeconds % 60}s remaining');
+ }
// Wind-down warning at 30 seconds: vibration + optional fade
if (!_warningSent && _timeRemaining <= _warningThreshold && _timeRemaining.inSeconds > 0) {
@@ -215,7 +218,7 @@ class SleepTimerService extends ChangeNotifier {
_player.setVolume((_fadeStartVolume * fraction).clamp(0.0, 1.0));
}
- notifyListeners();
+ notifyListeners(); // fires every 1s while playing - drives UI countdown
}
});
}