From eba618556155e041421554498223114346d86275 Mon Sep 17 00:00:00 2001 From: Naveen Date: Sun, 15 Oct 2023 22:01:34 +0530 Subject: [PATCH] Avoid preparing the player twice It's costly for large playlists --- .../com/simplemobiletools/musicplayer/extensions/Player.kt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/musicplayer/extensions/Player.kt b/app/src/main/kotlin/com/simplemobiletools/musicplayer/extensions/Player.kt index b6b573f3..d2822bb4 100644 --- a/app/src/main/kotlin/com/simplemobiletools/musicplayer/extensions/Player.kt +++ b/app/src/main/kotlin/com/simplemobiletools/musicplayer/extensions/Player.kt @@ -164,8 +164,10 @@ fun Player.prepareUsingTracks( * items are added using [addRemainingMediaItems]. This helps prevent delays, especially with large queues, and * avoids potential issues like [android.app.ForegroundServiceStartNotAllowedException] when starting from background. */ +var prepareInProgress = false inline fun Player.maybePreparePlayer(context: Context, crossinline callback: (success: Boolean) -> Unit) { - if (currentMediaItem == null) { + if (!prepareInProgress && currentMediaItem == null) { + prepareInProgress = true ensureBackgroundThread { var prepared = false context.audioHelper.getQueuedTracksLazily { tracks, startIndex, startPositionMs ->