MPMusicPlayerControllerMutableQueue插入Apple音乐歌曲不起作用 [英] MPMusicPlayerControllerMutableQueue insert an Apple Music song not working

查看:86
本文介绍了MPMusicPlayerControllerMutableQueue插入Apple音乐歌曲不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我几天来一直在尝试使用MusicKit API.我一直在尝试使用 MPMusicPlayerApplicationController

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.

我已经使用 setQueue(with:[String])和Apple Music歌曲的商店标识符数组初始化了队列.然后,我想允许用户对队列中的歌曲重新排序.我使用下面的代码来尝试.

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

如果 afterItem 为nil(即,歌曲已正确插入队列的开头),则上面的代码将按预期工作.但是,如果 afterItem 不为零,则什么也不会发生.队列与没有插入发生且完成处理程序中未提供错误的情况完全相同.无论插入的歌曲是否已经在队列中,都会发生此问题.

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?

推荐答案

好,我找到了解决方法.

Ok, I found the solution.

如果您要更改队列.

您需要返回查询

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

这篇关于MPMusicPlayerControllerMutableQueue插入Apple音乐歌曲不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆