那是我的问题 =)
MPMediaQuery *artistQuery = [MPMediaQuery artistsQuery];
NSArray *songsByArtist = [artistQuery collections];
如何在 songByArtist 中获取 MPMediaItemCollections 的每个艺术家的专辑数量?
例子:
披头士 3 专辑
AC/DC 6 专辑
谢谢你 !!
那是我的问题 =)
MPMediaQuery *artistQuery = [MPMediaQuery artistsQuery];
NSArray *songsByArtist = [artistQuery collections];
如何在 songByArtist 中获取 MPMediaItemCollections 的每个艺术家的专辑数量?
例子:
披头士 3 专辑
AC/DC 6 专辑
谢谢你 !!
我使用谓词获得艺术家的专辑和歌曲数量:
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
便利构造artistsQuery
函数不按专辑排序和分组。artistsQuery
返回按艺术家姓名字母顺序排序的所有艺术家的媒体项目集合数组。嵌套在每个艺术家集合中的是与该艺术家的所有歌曲相关联的媒体项数组。嵌套数组按歌曲名称的字母顺序排序。
按艺术家统计专辑的一种方法是枚举每个艺术家收藏的所有歌曲项,并使用 aNSMutableSet
来跟踪与每首歌曲相关的不同专辑标题。然后将集合的计数添加为 a 中每个艺术家键的值NSMutableDictionary
。不会添加任何重复的专辑标题,因为 aNSMutableSet
只会采用不同的对象:
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:
方法检索给定对象的计数。
布莱恩回答的 Swift 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)")
如果对您有帮助,请向我们查询此链接或在下面的此链接上查看更多信息
感谢 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)")
对不起,迟到的答案。
发布我的答案以防对某人有帮助。
下面的代码按专辑艺术家获取所有艺术家分组,并获取专辑中的所有专辑和歌曲。
/// 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)
}
}