Skip to content
Open
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 87 additions & 8 deletions app/src/main/kotlin/com/metrolist/music/playback/MusicService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ class MusicService :

// Tracks the original queue size to distinguish original items from auto-added ones
private var originalQueueSize: Int = 0
private var manualQueueCount: Int = 0

private var consecutivePlaybackErr = 0
private var retryJob: Job? = null
Expand Down Expand Up @@ -1422,6 +1423,7 @@ class MusicService :
}
// Reset original queue size when starting a new queue
originalQueueSize = 0
manualQueueCount = 0
if (queue.preloadItem != null) {
player.setMediaItem(queue.preloadItem!!.toMediaItem())
player.prepare()
Expand Down Expand Up @@ -1778,11 +1780,42 @@ class MusicService :
}
}
}

// Counts consecutive manual-queue items immediately ahead of the current index.
// Stops at the first item without the flag so items behind the current position
// or non-manual items further ahead are never counted.
private fun recomputeManualQueueCount(): Int {
val startIndex = player.currentMediaItemIndex + 1
var count = 0
for (i in startIndex until player.mediaItemCount) {
val extras = player.getMediaItemAt(i).mediaMetadata.extras
if (extras?.getBoolean(IS_MANUAL_QUEUE, false) == true) {
count++
} else {
break
}
}
return count
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
fun addToQueue(items: List<MediaItem>) {
// Tag items as manual before insertion to track priority and avoid counter drift
val stampedItems = items.map { item ->
item.buildUpon()
.setMediaMetadata(
item.mediaMetadata
.buildUpon()
.setExtras(
(item.mediaMetadata.extras ?: android.os.Bundle()).apply {
putBoolean(IS_MANUAL_QUEUE, true)
}
)
.build()
)
.build()
}

// Remove duplicates if enabled
if (dataStore.get(PreventDuplicateTracksInQueueKey, false)) {
val itemIds = items.map { it.mediaId }.toSet()
val itemIds = stampedItems.map { it.mediaId }.toSet()
val indicesToRemove = mutableListOf<Int>()
val currentIndex = player.currentMediaItemIndex

Expand All @@ -1798,14 +1831,56 @@ class MusicService :
}
}

player.addMediaItems(items)
// Check if repeat mode is active
val repeatMode = player.repeatMode
val isRepeatActive = repeatMode != REPEAT_MODE_OFF

// Calculate where items will be inserted (FIFO logic)
// Insert after current song + already present manual queue items
val currentIndex = player.currentMediaItemIndex
val insertIndex = if (isRepeatActive) {
currentIndex + manualQueueCount + 1
} else {
player.mediaItemCount
}

// Insert items based on repeat mode
if (isRepeatActive) {
// Insert immediately after the current item when repeat is active
player.addMediaItems(insertIndex, stampedItems)
} else {
// Add to end of queue when repeat is not active
player.addMediaItems(stampedItems)
}

// Update manual queue count (FIFO order)
manualQueueCount += stampedItems.size
// Professional shuffle management
if (player.shuffleModeEnabled) {
val shufflePlaylistFirst = dataStore.get(ShufflePlaylistFirstKey, false)
applyShuffleOrder(player.currentMediaItemIndex, player.mediaItemCount, shufflePlaylistFirst)
val totalItems = player.mediaItemCount
val random = java.util.Random()

// Remove indices that must have FIXED position:
// [Passed] + [Current] + [Manual Queue]
val fixedPathCount = currentIndex + manualQueueCount + 1

val flexibleIndices = (fixedPathCount until totalItems).toMutableList()
flexibleIndices.shuffle(random)

val finalShuffleOrder = IntArray(totalItems)
// Put first indices in linear order (Absolute priority)
for (i in 0 until fixedPathCount) {
finalShuffleOrder[i] = i
}
// Rest of playlist is shuffled after the queue
for (i in flexibleIndices.indices) {
finalShuffleOrder[fixedPathCount + i] = flexibleIndices[i]
}

player.setShuffleOrder(DefaultShuffleOrder(finalShuffleOrder, random.nextLong()))
}
player.prepare()
}

}
fun toggleLibrary() {
scope.launch {
val songToToggle = currentSong.first()
Expand Down Expand Up @@ -2230,6 +2305,10 @@ class MusicService :
}
previousMediaItemIndex = player.currentMediaItemIndex

// Rescan instead of decrementing: a seek can cross any number of items
if (reason == Player.MEDIA_ITEM_TRANSITION_REASON_AUTO || reason == Player.MEDIA_ITEM_TRANSITION_REASON_SEEK) {
manualQueueCount = recomputeManualQueueCount()
}
lastPlaybackSpeed = -1.0f // force update song

setupLoudnessEnhancer()
Expand Down Expand Up @@ -4160,7 +4239,7 @@ class MusicService :
private const val MIN_GAIN_MB = -1500 // Minimum gain in millibels (-15 dB)

private const val TAG = "MusicService"

private const val IS_MANUAL_QUEUE = "is_manual_queue"
@Volatile
var isRunning = false
private set
Expand Down