ios - MPMusicPlayerControllerMutableQueue 插入 Apple Music 歌曲不起作用

标签 ios swift cocoa-touch mpmusicplayercontroller apple-musickit

所以这几天我一直在尝试使用 MusicKit API。我一直在尝试使用 MPMusicPlayerApplicationControllerMutableQueue蜜蜂。

我已经使用 setQueue(with: [String]) 初始化了队列,其中包含 Apple Music 歌曲的商店标识符数组。然后我想让用户重新排序队列中的歌曲。我使用以下代码来尝试。

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!)
  }
}

如果 afterItem 为 nil(即歌曲被正确插入到队列的前面),上面的代码将按预期工作。但是,如果 afterItem 不为 nil,则什么也不会发生。队列保持完全相同,就好像没有插入发生一样,并且完成处理程序中没有提供错误。无论插入的歌曲是否已在队列中,都会出现此问题。

我是否试图错误地修改队列?

最佳答案

好的,我找到了解决方案。

如果你想改变队列。

你需要返回查询

let musicPlayerController = MPMusicPlayerController.applicationQueuePlayer

musicPlayerController.perform(queueTransaction: { queue in 

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

  //return the modification here.
  return queue.insert(descriptor, after: afterItem)

}) { (queue, error) in

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

关于ios - MPMusicPlayerControllerMutableQueue 插入 Apple Music 歌曲不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50592820/

相关文章:

ios - 在 Swift 3 中录制音频并将其发送到服务器

html - 导航栏大标题-动画问题

iphone - UIWindows 或 UIView

objective-c - UIButton 调用选择器会使应用程序崩溃

ios - 如何自定义UITableView包含多行,每行包含多个子行?

iphone - UIView 动画中的奇怪行为

ios - 如何过滤二维数组

iphone - 当标签中的文本被限制为适合宽度时,如何计算 UIFont 的大小?

ios - 从模态中关闭 View Controller

swift - Facebook 邀请好友无法在 swit 4.0 上运行?