需要很好地解释集合和项目的 MPMediaQuery 结果

way*_*neh 2 mpmusicplayercontroller mpmediaquery swift3

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

这是我为什么感到困惑的一个例子。

在我的图书馆中,我有一位拥有 3 张专辑的艺术家。我的目标是查询这 3 张专辑,按顺序排列每首曲目:

  • 专辑A
    • 轨道 1
    • 轨道 2
    • 轨道 3
  • 专辑2
    • 轨道 1
    • 轨道 2
    • 轨道 3
  • 专辑3
    • 轨道 1
    • 轨道 2
    • 轨道 3

当我使用此查询时,专辑/歌曲没有按预期顺序排列。即使 shuffle 没有打开,它们也几乎看起来是随机的。

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!)")
}
Run Code Online (Sandbox Code Playgroud)

我得到这些结果:

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

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

但是,如果我使用这个打印语句,我会得到预期的结果:

for junk in newQry.items!{
    print("items title \(junk.albumTitle!) track \(junk.albumTrackNumber) song \(junk.title!)")
}
Run Code Online (Sandbox Code Playgroud)

结果:

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)
Run Code Online (Sandbox Code Playgroud)

然后发出 SAME 'items' 打印语句,结果现在以与 'collections' 版本完全相同的方式混合!

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

由于我不能setQueue使用newQry.items,我该如何构建队列以按预期顺序获取专辑和歌曲?

way*_*neh 6

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

只需要添加一行代码:

let collection = MPMediaItemCollection(items: newQry.items!)
Run Code Online (Sandbox Code Playgroud)

然后更改 setQueue 函数:

myMP.setQueue(with: collection)
Run Code Online (Sandbox Code Playgroud)

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

    let newQry = MPMediaQuery.albums()
    newQry.addFilterPredicate(
        MPMediaPropertyPredicate(
            value: artistArray[indexPath.row + currLoc],
            forProperty: MPMediaItemPropertyAlbumArtist,
            comparisonType: .equalTo
        )
    )
    //build a new collection with the sorted items then load the collection!
    let collection = MPMediaItemCollection(items: newQry.items!)
    myMP.stop()
    myMP.setQueue(with: collection)
    myMP.play()
Run Code Online (Sandbox Code Playgroud)