From 140bc7fbc5c29f4c725f4e193c713c1d26c8c1a8 Mon Sep 17 00:00:00 2001 From: Benjamin Porter Date: Mon, 5 Jan 2026 16:58:10 -0700 Subject: [PATCH] Skip progress updates when backgrounded When we are backgrounded, these queue events build up and can cause performance issues as the queue is processed. We don't need to process these when backgrounded, so skip them. Also add small delay when resuming for webview to load before sending it update events --- .../audiobookshelf/app/plugins/AbsAudioPlayer.kt | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/android/app/src/main/java/com/audiobookshelf/app/plugins/AbsAudioPlayer.kt b/android/app/src/main/java/com/audiobookshelf/app/plugins/AbsAudioPlayer.kt index a5f1935e..41d7cbf7 100644 --- a/android/app/src/main/java/com/audiobookshelf/app/plugins/AbsAudioPlayer.kt +++ b/android/app/src/main/java/com/audiobookshelf/app/plugins/AbsAudioPlayer.kt @@ -82,6 +82,8 @@ class AbsAudioPlayer : Plugin() { } override fun onLocalMediaProgressUpdate(localMediaProgress: LocalMediaProgress) { + // Skip progress updates when app is backgrounded to prevent event queue buildup + if (!isInForeground) return notifyListeners("onLocalMediaProgressUpdate", JSObject(jacksonMapper.writeValueAsString(localMediaProgress))) } @@ -128,20 +130,21 @@ class AbsAudioPlayer : Plugin() { override fun handleOnPause() { super.handleOnPause() isInForeground = false - Log.d(tag, "App paused - disabling frequent event emission") } override fun handleOnResume() { super.handleOnResume() isInForeground = true - Log.d(tag, "App resumed - enabling event emission and sending current state") - // Send current state to UI after resume to sync up + // Send current state to UI after resume to sync up (with small delay to let WebView fully resume) if (::playerNotificationService.isInitialized && playerNotificationService.currentPlaybackSession != null) { - Handler(Looper.getMainLooper()).post { + Handler(Looper.getMainLooper()).postDelayed({ playerNotificationService.sendClientMetadata(PlayerState.READY) playerNotificationService.sleepTimerManager.sendCurrentSleepTimerState() - } + playerNotificationService.mediaProgressSyncer.currentLocalMediaProgress?.let { + playerNotificationService.clientEventEmitter?.onLocalMediaProgressUpdate(it) + } + }, 100) } }