需要对集合和项目的 MPMediaQuery 结果进行很好的解释
Need good explanation of MPMediaQuery results for collections and items
我需要一些帮助来理解 MPMediaQuery 以及如何访问结果,以便我可以使用带有 setQueue(with:) 的查询。
下面是我感到困惑的一个例子。
在我的音乐库中,我有一位艺术家有 3 张专辑。
我的目标是查询这 3 张专辑,按顺序排列每首曲目:
- 专辑A
- 轨道 1
- 轨道 2
- 轨道 3
- 专辑2
- 轨道 1
- 轨道 2
- 轨道 3
- 专辑3
- 轨道 1
- 轨道 2
- 轨道 3
当我使用这个查询时,Albums/songs 没有按预期排列。
尽管没有打开随机播放,它们几乎看起来是随机播放的。
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
请注意 Albums/Songs 的顺序不正确
但是,如果我改用此打印语句,我会得到预期的结果:
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)
然后发出相同的 'items' 打印语句,结果现在以与 'collections' 版本完全相同的方式混合!
为什么设置队列会改变查询的行为方式?
因为我不能 setQueue
与 newQry.items
,我如何建立一个队列以按预期顺序获取专辑和歌曲?
好的,我通过更多的研究自己解决了这个问题。
这里的技巧是使用正确排序的 ITEMS,并构建一个新的 Collection 用作队列。
只需添加一行代码:
let collection = MPMediaItemCollection(items: newQry.items!)
然后更改 setQueue 函数:
myMP.setQueue(with: collection)
这是最终的工作代码块 - 与上面我原来的 post 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()
我需要一些帮助来理解 MPMediaQuery 以及如何访问结果,以便我可以使用带有 setQueue(with:) 的查询。
下面是我感到困惑的一个例子。
在我的音乐库中,我有一位艺术家有 3 张专辑。 我的目标是查询这 3 张专辑,按顺序排列每首曲目:
- 专辑A
- 轨道 1
- 轨道 2
- 轨道 3
- 专辑2
- 轨道 1
- 轨道 2
- 轨道 3
- 专辑3
- 轨道 1
- 轨道 2
- 轨道 3
当我使用这个查询时,Albums/songs 没有按预期排列。 尽管没有打开随机播放,它们几乎看起来是随机播放的。
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
请注意 Albums/Songs 的顺序不正确
但是,如果我改用此打印语句,我会得到预期的结果:
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)
然后发出相同的 'items' 打印语句,结果现在以与 'collections' 版本完全相同的方式混合!
为什么设置队列会改变查询的行为方式?
因为我不能 setQueue
与 newQry.items
,我如何建立一个队列以按预期顺序获取专辑和歌曲?
好的,我通过更多的研究自己解决了这个问题。 这里的技巧是使用正确排序的 ITEMS,并构建一个新的 Collection 用作队列。
只需添加一行代码:
let collection = MPMediaItemCollection(items: newQry.items!)
然后更改 setQueue 函数:
myMP.setQueue(with: collection)
这是最终的工作代码块 - 与上面我原来的 post 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()