I am really struggling to figure something out and I've looked all over the internet and I can't find an answer. As you know, music apps like Spotify and Apple Music itself let the user rearrange the up next queue even while the user is listening to a song. I haven't been able to figure out how to do the same. I want to be able to completely rearrange the queue including adding, deleting, and moving around songs in the up next queue. I've tried various ways of doing so such as
musicPlayer.setQueue([queue])
but this would require me to call musicPlayer.prepareToPlay()
afterwords to update the queue which would end the current song and initiate the new queue.
I've also tried using
musicPlayer.perform(queueTransaction: { (currentQueue) in
let oldItems = currentQueue.items
var currentItem: MPMediaItem? = nil
for (i, item) in oldItems.enumerated() {
if i == musicPlayer.indexOfNowPlayingItem {
currentItem = item
continue
}
currentQueue.remove(item)
}
currentQueue.insert(queue, after: currentItem)
}, completionHandler: { (newQueue, error) in
if let error = error {
print("there was an error updating the queue", error)
return
}
print("=== NEW QUEUE ===") //insert does not work
for item in newQueue.items {
print(" -", item.title ?? "[No Title]")
}
})
but as you may know from this question: MPMusicPlayerControllerMutableQueue insert an Apple Music song not working
the insert after item function of a queue doesn't seem to work (and I've also tried setting item to nil which sometimes removed the current item and set the queue to nothing). I also know about musicPlayer.prepend([queue])
which will add the new queue after the currently playing item but I would also have to remove the previous queue from memory so the up next queue doesn't end up being over 300 songs long. I've also tried using the prepend
function inside the queue transaction but sometimes the song would stop in the middle and the music player would set the nowPlayingItem
to nil. I really hope someone knows the answer to this because I've been having this issue for over 4 days now and I can't seem to find a solution that works 100% of the time.