From ff0986a4e1f6af5134b3585af8bc035428dd3604 Mon Sep 17 00:00:00 2001 From: 1hitsong <3330318+1hitsong@users.noreply.github.com> Date: Sat, 8 Apr 2023 13:48:57 -0400 Subject: [PATCH] Fix music shuffle function --- components/manager/QueueManager.brs | 57 +++++++++++++++++++++++++++- components/manager/QueueManager.xml | 4 ++ components/music/AudioPlayerView.brs | 43 +++++++++------------ source/Main.brs | 9 +++++ 4 files changed, 86 insertions(+), 27 deletions(-) diff --git a/components/manager/QueueManager.brs b/components/manager/QueueManager.brs index 657f2da2..374e0130 100644 --- a/components/manager/QueueManager.brs +++ b/components/manager/QueueManager.brs @@ -1,7 +1,9 @@ sub init() m.queue = [] + m.originalQueue = [] m.queueTypes = [] m.position = 0 + m.shuffleEnabled = false end sub ' Clear all content from play queue @@ -27,6 +29,11 @@ function getCurrentItem() return getItemByIndex(m.position) end function +' Return whether or not shuffle is enabled +function getIsShuffled() + return m.shuffleEnabled +end function + ' Return the item in the passed index from the play queue function getItemByIndex(index) return m.queue[index] @@ -108,6 +115,54 @@ sub setPosition(newPosition) m.position = newPosition end sub +' Reset shuffle to off state +sub resetShuffle() + m.shuffleEnabled = false +end sub + +' Toggle shuffleEnabled state +sub toggleShuffle() + m.shuffleEnabled = not m.shuffleEnabled + + if m.shuffleEnabled + shuffleQueueItems() + return + end if + + resetQueueItemOrder() +end sub + +' Reset queue items back to original, unshuffled order +sub resetQueueItemOrder() + set(m.originalQueue) +end sub + +' Return original, unshuffled queue +function getUnshuffledQueue() + return m.originalQueue +end function + +' Save a copy of the original queue and randomize order of queue items +sub shuffleQueueItems() + ' By calling getQueue 2 different ways, Roku avoids needing to do a deep copy + m.originalQueue = m.global.queueManager.callFunc("getQueue") + songIDArray = getQueue() + + ' Move the currently playing song to the front of the queue + temp = top() + songIDArray[0] = getCurrentItem() + songIDArray[getPosition()] = temp + + for i = 1 to songIDArray.count() - 1 + j = Rnd(songIDArray.count() - 1) + temp = songIDArray[i] + songIDArray[i] = songIDArray[j] + songIDArray[j] = temp + end for + + set(songIDArray) +end sub + ' Return the fitst item in the play queue function top() return getItemByIndex(0) @@ -115,7 +170,7 @@ end function ' Replace play queue with passed array sub set(items) - setPosition(0) + clear() m.queue = items for each item in items m.queueTypes.push(getItemType(item)) diff --git a/components/manager/QueueManager.xml b/components/manager/QueueManager.xml index 05cb4d54..79ea899f 100644 --- a/components/manager/QueueManager.xml +++ b/components/manager/QueueManager.xml @@ -5,19 +5,23 @@ + + + +