需要对集合和项目的MPMediaQuery结果进行很好的解释

时间:2017-03-18 18:55:37

标签: swift3 mpmusicplayercontroller mpmediaquery

我需要一些帮助来理解MPMediaQuery以及如何访问结果,以便我可以使用setQueue(带:)的查询。

以下是我为何感到困惑的一个例子。

在我的图书馆里,我有一张有3张专辑的艺术家。 我的目标是按顺序查看这3张专辑:

  • 专辑A.
    • track 1
    • track 2
    • track 3
  • 专辑2
    • track 1
    • track 2
    • track 3
  • 专辑3
    • track 1
    • track 2
    • track 3

当我使用此查询时,相册/歌曲未按预期排序。 即使没有随机播放,它们几乎会出现洗牌现象。

var qryArtists = MPMediaQuery()
qryArtists = MPMediaQuery.artists()
qryArtists.groupingType = MPMediaGrouping.albumArtist
let currLoc = qryArtists.collectionSections![indexPath.section].range.location
myMP.setQueue(with: qryArtists.collections![indexPath.row + currLoc])
for junk in qryArtists.collections![indexPath.row + currLoc].items{
    print(" collections title \(junk.albumTitle!) track \(junk.albumTrackNumber) song \(junk.title!)")
}

我得到了这些结果:

collections title Cosmic Thing track 8 song Channel Z collections title Cosmic Thing track 1 song Cosmic Thing collections title Wild Planet track 6 song Devil In My Car collections title Wild Planet track 2 song Dirty Back Road collections title Wild Planet track 4 song Give Me Back My Man collections title Cosmic Thing track 5 song June Bug collections title Wild Planet track 1 song Party Out Of Bounds collections title Wild Planet track 5 song Private Idaho collections title Wild Planet track 7 song Quiche Lorraine collections title Cosmic Thing track 6 song Roam collections title The B-52's track 15 song Rock Lobster collections title Wild Planet track 3 song Runnin' Around collections title Wild Planet track 8 song Strobe Light collections title Cosmic Thing track 9 song Topaz collections title Wild Planet track 9 song 53 Miles West Of Venus

请注意专辑/歌曲的顺序不正确

但是,如果我使用此print语句,我会得到预期的结果:

for junk in newQry.items!{
    print("items title \(junk.albumTitle!) track \(junk.albumTrackNumber) song \(junk.title!)")
}

结果:

items title The B-52's track 15 song Rock Lobster items title Cosmic Thing track 1 song Cosmic Thing items title Cosmic Thing track 5 song June Bug items title Cosmic Thing track 6 song Roam items title Cosmic Thing track 8 song Channel Z items title Cosmic Thing track 9 song Topaz items title Wild Planet track 1 song Party Out Of Bounds items title Wild Planet track 2 song Dirty Back Road items title Wild Planet track 3 song Runnin' Around items title Wild Planet track 4 song Give Me Back My Man items title Wild Planet track 5 song Private Idaho items title Wild Planet track 6 song Devil In My Car items title Wild Planet track 7 song Quiche Lorraine items title Wild Planet track 8 song Strobe Light items title Wild Planet track 9 song 53 Miles West Of Venus

另外,另一个非常奇怪的效果:如果我设置了MusicPlayer查询:

myMP.setQueue(with: newQry)

然后发出相同的'项目' print语句,现在结果与'集合'完全相同。版本!

为什么设置队列会改变查询的行为方式?

由于我不能setQueue使用newQry.items,我如何建立一个队列以预期的顺序获取相册和歌曲?

1 个答案:

答案 0 :(得分:1)

好的,我通过更多的研究自己解决了这个问题。 这里的技巧是使用正确排序的ITEMS,并构建一个新的集合作为队列。

只需添加一行代码:

tensor = tensor > 0

然后更改setQueue函数:

let collection = MPMediaItemCollection(items: newQry.items!)

这是最终的工作代码块 - 与我上面的原始帖子OP进行比较:

myMP.setQueue(with: collection)