A cross platform Audiobookshelf client for Android and iOS
Dart
Battery optimizations: throttle media session, pause accelerometer
Nathan Poulson4fbc6a2df345
diff --git a/lib/screens/app_shell.dart b/lib/screens/app_shell.dartindex 693f8eb6..f3bbb1e1 100644--- a/lib/screens/app_shell.dart+++ b/lib/screens/app_shell.dart@@ -346,12 +346,14 @@ class _AppShellState extends State<AppShell> with WidgetsBindingObserver, Tickervoid didChangeAppLifecycleState(AppLifecycleState state) {if (state == AppLifecycleState.resumed) {context.read<LibraryProvider>().onAppForegrounded();+ SleepTimerService().onAppForegrounded();_refreshDataForTab(_currentIndex);// Check auto sleep in case we resumed into the windowSleepTimerService().checkAutoSleep();_checkForUpdate();} else if (state == AppLifecycleState.paused) {context.read<LibraryProvider>().onAppBackgrounded();+ SleepTimerService().onAppBackgrounded();} else if (state == AppLifecycleState.detached) {final cast = ChromecastService();if (cast.isConnected) cast.disconnect();diff --git a/lib/services/audio_player_service.dart b/lib/services/audio_player_service.dartindex 56c1995f..b9f43cae 100644--- a/lib/services/audio_player_service.dart+++ b/lib/services/audio_player_service.dart@@ -49,6 +49,9 @@ class AudioPlayerHandler extends BaseAudioHandler with SeekHandler {void refreshPlaybackState() {try {playbackState.add(_transformEvent(_player.playbackEvent));+ _lastPlaybackStateUpdate = DateTime.now();+ _lastPlaying = _player.playing;+ _lastProcessingState = _player.processingState;} catch (_) {}}@@ -63,10 +66,27 @@ class AudioPlayerHandler extends BaseAudioHandler with SeekHandler {int _resubscribeCount = 0;DateTime _lastResubscribe = DateTime.now();+ // Throttle playback state updates to avoid excessive notification refreshes+ DateTime _lastPlaybackStateUpdate = DateTime.now();+ bool? _lastPlaying;+ ProcessingState? _lastProcessingState;+void _subscribePlaybackEvents() {_player.playbackEventStream.map(_transformEvent).listen((state) {- playbackState.add(state);+ // Only push state on meaningful changes (play/pause, processing state)+ // or at most every 5 seconds for position updates+ final now = DateTime.now();+ final playingChanged = _player.playing != _lastPlaying;+ final processingChanged = _player.processingState != _lastProcessingState;+ final elapsed = now.difference(_lastPlaybackStateUpdate);++ if (playingChanged || processingChanged || elapsed.inSeconds >= 5) {+ playbackState.add(state);+ _lastPlaybackStateUpdate = now;+ _lastPlaying = _player.playing;+ _lastProcessingState = _player.processingState;+ }// Reset error counter on successful events_resubscribeCount = 0;},diff --git a/lib/services/sleep_timer_service.dart b/lib/services/sleep_timer_service.dartindex 18246424..150cb064 100644--- a/lib/services/sleep_timer_service.dart+++ b/lib/services/sleep_timer_service.dart@@ -534,6 +534,25 @@ class SleepTimerService extends ChangeNotifier {}}+ /// Pause battery-intensive operations when app is backgrounded.+ /// Only pauses shake detection if nothing is playing (user might shake+ /// with screen off to extend sleep timer).+ void onAppBackgrounded() {+ if (!_isPlaybackActive) {+ _accelSub?.cancel();+ _accelSub = null;+ debugPrint('[SleepTimer] Paused shake detection (backgrounded, not playing)');+ }+ }++ /// Resume operations when app is foregrounded.+ void onAppForegrounded() {+ if (isActive && _accelSub == null) {+ _startShakeDetection();+ debugPrint('[SleepTimer] Resumed shake detection (foregrounded)');+ }+ }+@overridevoid dispose() {cancel();