这是我的问题=)
MPMediaQuery *artistQuery = [MPMediaQuery artistsQuery];
NSArray *songsByArtist = [artistQuery collections];
如何在songsByArtist中获取MPMediaItemCollections的每位艺术家的专辑数量?
例如:
披头士乐队 3张专辑AC / DC 6张专辑
谢谢!!
答案 0 :(得分:5)
artistsQuery
便利构造函数不按专辑排序和分组。 artistsQuery
返回按艺术家姓名按字母顺序排序的所有艺术家的媒体项集合数组。嵌套在每个艺术家集合中的是与该艺术家的所有歌曲相关联的一系列媒体项目。嵌套数组按歌曲标题按字母顺序排序。
通过艺术家保持专辑数量的一种方法是枚举每个艺术家收藏的所有歌曲项目,并使用NSMutableSet
来跟踪与每首歌曲相关的不同专辑标题。然后将集合的计数添加为NSMutableDictionary
中每个艺术家键的值。由于NSMutableSet
只会采用不同的对象,因此不会添加任何重复的相册标题:
MPMediaQuery *artistQuery = [MPMediaQuery artistsQuery];
NSArray *songsByArtist = [artistQuery collections];
NSMutableDictionary *artistDictionary = [NSMutableDictionary dictionary];
NSMutableSet *tempSet = [NSMutableSet set];
[songsByArtist enumerateObjectsUsingBlock:^(MPMediaItemCollection *artistCollection, NSUInteger idx, BOOL *stop) {
NSString *artistName = [[artistCollection representativeItem] valueForProperty:MPMediaItemPropertyArtist];
[[artistCollection items] enumerateObjectsUsingBlock:^(MPMediaItem *songItem, NSUInteger idx, BOOL *stop) {
NSString *albumName = [songItem valueForProperty:MPMediaItemPropertyAlbumTitle];
[tempSet addObject:albumName];
}];
[artistDictionary setValue:[NSNumber numberWithUnsignedInteger:[tempSet count]]
forKey:artistName];
[tempSet removeAllObjects];
}];
NSLog(@"Artist Album Count Dictionary: %@", artistDictionary);
如果将查询更改为albumsQuery
,会更清晰。此查询按专辑名称对集合进行分组和排序。然后,只需要对一系列专辑集进行枚举,并在NSCountedSet
中保留每个专辑的代表性艺术家名称。计数集将跟踪插入对象的次数:
MPMediaQuery *albumQuery = [MPMediaQuery albumsQuery];
NSArray *albumCollection = [albumQuery collections];
NSCountedSet *artistAlbumCounter = [NSCountedSet set];
[albumCollection enumerateObjectsUsingBlock:^(MPMediaItemCollection *album, NSUInteger idx, BOOL *stop) {
NSString *artistName = [[album representativeItem] valueForProperty:MPMediaItemPropertyArtist];
[artistAlbumCounter addObject:artistName];
}];
NSLog(@"Artist Album Counted Set: %@", artistAlbumCounter);
您还可以使用NSCountedSet
方法检索countForObject:
中给定对象的计数。
答案 1 :(得分:5)
我使用谓词获取艺术家的专辑和歌曲数量:
MPMediaPropertyPredicate *artistNamePredicate = [MPMediaPropertyPredicate predicateWithValue:@"ArtistName" forProperty:MPMediaItemPropertyArtist];
MPMediaQuery *myComplexQuery = [[MPMediaQuery alloc] init];
[myComplexQuery addFilterPredicate: artistNamePredicate];
NSInteger songCount = [[myComplexQuery collections] count]; //number of songs
myComplexQuery.groupingType = MPMediaGroupingAlbum;
NSInteger albumCount = [[myComplexQuery collections] count]; //number of albums
答案 2 :(得分:1)
斯威夫特2翻译布莱恩的回答:
var artistQuery = MPMediaQuery.artistsQuery()
var artistQuery.groupingType = MPMediaGrouping.AlbumArtist
var songsByArtist = artistQuery.collections
var artistDictionary = NSMutableDictionary()
var tempSet = NSMutableSet()
songsByArtist.enumerateObjectsUsingBlock { (artistCollection, idx, stop) -> Void in
let collection = artistCollection as! MPMediaItemCollection
let rowItem = collection.representativeItem
let artistName = rowItem?.valueForProperty(MPMediaItemPropertyAlbumArtist)
let collectionContent:NSArray = collection.items
collectionContent.enumerateObjectsUsingBlock { (songItem, idx, stop) -> Void in
let item = songItem as! MPMediaItem
let albumName = item.valueForProperty(MPMediaItemPropertyAlbumTitle)
self.tempSet.addObject(albumName!)
}
self.artistDictionary.setValue(NSNumber(unsignedInteger: self.tempSet.count), forKey: artistName! as! String)
self.tempSet.removeAllObjects()
}
print("Album Count Dictionary: \(artistDictionary)")
答案 3 :(得分:0)
albums如果有帮助或在下面的链接中查看更多内容,请告诉我们
答案 4 :(得分:0)
谢谢Tim E,我无法让您的代码第一次工作,但我将其修改为此,现在可以正常工作。
let artistQuery = MPMediaQuery.artistsQuery()
artistQuery.groupingType = MPMediaGrouping.AlbumArtist
let songsByArtist = artistQuery.collections! as NSArray
let artistDictionary = NSMutableDictionary()
let tempSet = NSMutableSet()
songsByArtist.enumerateObjectsUsingBlock( { (artistCollection, idx, stop) -> Void in
let collection = artistCollection as! MPMediaItemCollection
let rowItem = collection.representativeItem
let artistName = rowItem?.valueForProperty(MPMediaItemPropertyAlbumArtist)
let collectionContent:NSArray = collection.items
collectionContent.enumerateObjectsUsingBlock({ (songItem, idx, stop) -> Void in
let item = songItem as! MPMediaItem
let albumName = item.valueForProperty(MPMediaItemPropertyAlbumTitle)
tempSet.addObject(albumName!)
})
artistDictionary.setValue(NSNumber(unsignedInteger: UInt(tempSet.count)), forKey: artistName! as! String)
tempSet.removeAllObjects()
})
print("Album Count Dictionary: \(artistDictionary)")
答案 5 :(得分:0)
对不起,迟到了。
发布我的答案,以防对某人有帮助。
下面的代码按照专辑艺术家的要求获取所有艺术家群组,并获得专辑中的所有专辑和歌曲。
/// Get all artists and their songs
///
func getAllArtists() {
let query: MPMediaQuery = MPMediaQuery.artists()
query.groupingType = .albumArtist
let artistsColelctions = query.collections
artists.removeAll()
var tempSet = NSMutableSet()
guard artistsColelctions != nil else {
return
}
// 1. Create Artist Objects from collection
for collection in artistsColelctions! {
let item: MPMediaItem? = collection.representativeItem
var artistName = item?.value(forKey: MPMediaItemPropertyArtist) as? String ?? "<Unknown>"
artistName = artistName.trimmingCharacters(in: NSCharacterSet.whitespacesAndNewlines)
let artistId = item!.value(forProperty: MPMediaItemPropertyArtistPersistentID) as! NSNumber
// temp
let albumName = item?.albumTitle
let albumID = item?.albumPersistentID
print(albumName)
print(albumID)
// Create artist item
let artist = Artist()
artist.name = artistName
artist.artworkTitle = String(artistName.characters.prefix(1)).uppercased()
artist.artistId = String(describing: artistId)
// 2. Get Albums for respective Artist object
//--------------------------------------------
let mediaQuery2 = MPMediaQuery.albums()
let predicate2 = MPMediaPropertyPredicate.init(value: artistId, forProperty: MPMediaItemPropertyArtistPersistentID)
mediaQuery2.addFilterPredicate(predicate2)
let albums = mediaQuery2.collections
for collection in albums! {
let item: MPMediaItem? = collection.representativeItem
let albumName = item?.value(forKey: MPMediaItemPropertyAlbumTitle) as? String ?? "<Unknown>"
let albumId = item!.value(forProperty: MPMediaItemPropertyAlbumPersistentID) as! NSNumber
let artistName = item?.value(forKey: MPMediaItemPropertyAlbumArtist) as? String ?? "unknown"
let genreName = item?.genre ?? ""
let year = item?.releaseDate ?? item?.dateAdded
let dateAdded = item?.dateAdded
// Create album object
let album = Album()
album.name = albumName
album.artistName = artistName
album.genre = genreName
album.releaseDate = year
album.dateAdded = dateAdded
album.albumId = String(describing: albumId)
// Add artwork to album object
let artwork = Artwork.init(forAlbum: item)
album.artwork = artwork
// 3. Get Songs inside the resepctive Album object
//---------------------------------------------------
let mediaQuery = MPMediaQuery.songs()
let predicate = MPMediaPropertyPredicate.init(value: albumId, forProperty: MPMediaItemPropertyAlbumPersistentID)
mediaQuery.addFilterPredicate(predicate)
let song = mediaQuery.items
if let allSongs = song {
var index = 0
for item in allSongs {
let pathURL: URL? = item.value(forProperty: MPMediaItemPropertyAssetURL) as? URL
let isCloud = item.value(forProperty: MPMediaItemPropertyIsCloudItem) as! Bool
let trackInfo = TrackInfo()
trackInfo.index = index
trackInfo.mediaItem = item
trackInfo.isCloudItem = isCloud
trackInfo.isExplicitItem = item.isExplicitItem
trackInfo.isSelected = false
trackInfo.songURL = pathURL
album.songs?.append(trackInfo)
index += 1
}
}
artist.albums?.append(album)
}
// Finally add the artist object to Artists Array
artists.append(artist)
}
}