问题描述
此代码导致静音:
let query = MPMediaQuery.songs()
let result = query.items
guard let items = result, items.count > 0 else {return}
let song = items[0]
let player = MPMusicPlayerController.applicationQueuePlayer
let coll = MPMediaItemCollection(items: [song])
let q = MPMusicPlayerMediaItemQueueDescriptor(itemCollection: coll)
player.setQueue(with: q)
player.play()
我已经遍历了代码,我们到达了player.play()
.我有一个MPMediaItem,并从中正确地形成了MPMediaItemCollection,并从那个中形成了MPMusicPlayerMediaItemQueueDescriptor.那为什么我的玩家不玩呢?
I've stepped through the code, and we reach player.play()
. I have an MPMediaItem and I've correctly formed an MPMediaItemCollection from that, and an MPMusicPlayerMediaItemQueueDescriptor from that. So why isn't my player playing?
推荐答案
您发现了一个错误.似乎初始化程序MPMusicPlayerMediaItemQueueDescriptor(itemCollection:)
已完全损坏:它导致队列描述符不可用.
You've found a bug. It appears that the initializer MPMusicPlayerMediaItemQueueDescriptor(itemCollection:)
is completely broken: it results in an unusable queue descriptor.
可能的解决方法是改为使用另一个初始化程序,即MPMusicPlayerMediaItemQueueDescriptor(query:)
.
The workaround, where possible, is to use the other initializer instead, namely MPMusicPlayerMediaItemQueueDescriptor(query:)
.
例如,在这种情况下,您可以编写(在原始代码的最后四行中选择):
For example, in this case, you could write (picking up in the last four lines of the original code):
let coll = MPMediaItemCollection(items: [song])
let predicate = MPMediaPropertyPredicate(
value: song.persistentID,
forProperty: MPMediaItemPropertyPersistentID)
let query = MPMediaQuery(filterPredicates: [predicate])
let q = MPMusicPlayerMediaItemQueueDescriptor(query: query)
player.setQueue(with: q)
player.play()
不幸的是,在许多情况下,您无法形成单个查询来获取您真正想要的MPMediaItemCollection.
Unfortunately, there are many circumstances where you can't form a single query that gets the MPMediaItemCollection you really wanted.
在此特定示例中,您也可以通过直接使用MPMediaItemCollection而不是由MPMediaItemCollection制作的MPMusicPlayerMediaItemQueueDescriptor设置播放器的队列来解决此问题.
In this particular example you could work around that, too, by setting the player's queue directly with an MPMediaItemCollection instead of a MPMusicPlayerMediaItemQueueDescriptor made from an MPMediaItemCollection.
但是,有些命令(例如append(_:)
)需要 一个MPMusicPlayerMediaItemQueueDescriptor,对于这样的事情,整个API基本上都已完成.自iOS 10.1起已对其进行清理,并且在iOS 11.1中仍进行清理.
But alas, there are some commands (such as append(_:)
) that require an MPMusicPlayerMediaItemQueueDescriptor, and for things like that, this entire API is basically hosed. It has been hosed since iOS 10.1 and it remains hosed in iOS 11.1.
这篇关于无法使用MPMusicPlayerMediaItemQueueDescriptor设置MPMusicPlayerController队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!