艺术家专辑数量
Posted
技术标签:
【中文标题】艺术家专辑数量【英文标题】:Number of Albums By Artists 【发布时间】:2012-05-15 08:33:45 【问题描述】:这是我的问题 =)
MPMediaQuery *artistQuery = [MPMediaQuery artistsQuery];
NSArray *songsByArtist = [artistQuery collections];
songsByArtist 中 MPMediaItemCollections 的每个艺术家的专辑数量如何获取?
示例:
披头士乐队 3 专辑
交流/直流 6 专辑
谢谢!!
【问题讨论】:
【参考方案1】:我使用谓词获得艺术家的专辑和歌曲数量:
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】: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);
您还可以使用countForObject:
方法在NSCountedSet
中检索给定对象的计数。
【讨论】:
【参考方案3】:Bryan 答案的 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)")
【讨论】:
【参考方案4】:专辑如果对您有帮助,请向我们查询或在下面的此链接中查看更多信息
http://developer.apple.com/library/ios/#documentation/mediaplayer/reference/MPMediaQuery_ClassReference/Reference/Reference.html
【讨论】:
【参考方案5】:感谢 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)")
【讨论】:
【参考方案6】:抱歉回复晚了。
发布我的答案以防对某人有帮助。
以下代码按专辑艺术家获取所有艺术家分组,并获取专辑中的所有专辑和歌曲。
/// 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)
【讨论】:
以上是关于艺术家专辑数量的主要内容,如果未能解决你的问题,请参考以下文章