MPMusicPlayerControllerMutableQueue insert an Apple Music song not working

送分小仙女□ 提交于 2019-12-04 14:08:15

问题


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?

来源:https://stackoverflow.com/questions/50592820/mpmusicplayercontrollermutablequeue-insert-an-apple-music-song-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!