-->

MPMusicPlayerControllerMutableQueue insert an Appl

2020-06-06 05:03发布

问题:

So I have been trying to use the MusicKit APIs for a few days now. I have been attempting to use the MPMusicPlayerApplicationController and MutableQueue APIs.

I have queue initialized already using setQueue(with: [String]) with an array of store identifiers for Apple Music songs. Then I want to allow the user to reorder the songs in the queue. I use the following code to attempt that.

let musicPlayerController = MPMusicPlayerController.applicationQueuePlayer

musicPlayerController.perform(queueTransaction: { queue in 

  let afterItem = queue.items.first(where: { $0.playbackStoreID == predecessorId })
  let descriptor = MPMusicPlayerStoreQueueDescriptor(storeIDs: [newItemId])
  queue.insert(descriptor, after: afterItem)

}) { (queue, error) in

  // Completion for when items' position update
  if error != nil {
    print(error!)
  }
}

The code above works as expected if afterItem is nil (i.e. the song is correctly inserted at the front of the queue). However, if afterItem is not nil, nothing happens. The queue stays the exact same as if no insert happened and there is no error provided in the completion handler. This problem happens regardless of whether the song being inserted is already in the queue or not.

Am I attempting modifying the queue incorrectly?