iPhone获取所有专辑/艺术家

时间:2010-10-15 09:26:41

标签: iphone ipod

你们是否有关于如何从iPod媒体库中检索所有音乐专辑或艺术家的示例代码(或其链接)?

提前致谢!

3 个答案:

答案 0 :(得分:21)

使用MPMediaQuery:

MPMediaQuery *allAlbumsQuery = [MPMediaQuery albumsQuery];
NSArray *allAlbumsArray = [allAlbumsQuery collections];

allItems数组现在包含MPMediaItemCollections,分组由专辑完成。现在您可以浏览阵列了。

for (MPMediaItemCollection *collection in allAlbumsArray) {
    MPMediaItem *item = [collection representativeItem];
}

答案 1 :(得分:4)

感谢您的回答,这里有一些示例代码可以打印出相册和艺术家,以防有人需要它:

NSMutableString *outText = [[NSMutableString alloc] initWithString:@"Albums:"];
[outText appendFormat:@"\r\n count:%i",[[[MPMediaQuery albumsQuery] collections] count]];
for (MPMediaItemCollection *collection in [[MPMediaQuery albumsQuery] collections]) {
        [outText appendFormat:@"\r\n -%@",[[collection representativeItem] valueForProperty:MPMediaItemPropertyAlbumTitle]];
}

[outText appendString:@"\r\n\r\n Artist:"];

for (MPMediaItemCollection *collection in [[MPMediaQuery artistsQuery] collections]) {
        [outText appendFormat:@"\r\n -%@",[[collection representativeItem] valueForProperty:MPMediaItemPropertyArtist]];
}
NSLog(@"%@",[outText autorelease]);

答案 2 :(得分:0)

你走了。你可以得到专辑和他们的歌曲。

    /// Get all albums and their songs
///
func getAllAlbums() {
    let query: MPMediaQuery = MPMediaQuery.albums()
    let allAlbums = query.collections

    allAlbumItems?.removeAll()

    guard allAlbums != nil else {
        return
    }

    for collection in allAlbums! {
        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: MPMediaItemPropertyArtist) as? String ?? "<Unknown>"

        let album = Album()
        album.name = albumName
        album.artistName = artistName
        album.albumId = String(describing: albumId)
        print("Album name: \(albumName)")

        // Get all songs in this album
        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
                if pathURL == nil {
                    print("@Warning!!! Track : \(item) is not playable.")
                } else {
                    let trackInfo = SongItem()
                    trackInfo.index = index
                    trackInfo.mediaItem = item

                    let title = item.value(forProperty: MPMediaItemPropertyTitle) as? String ?? "<Unknown>"
                    let artistName = item.value(forProperty: MPMediaItemPropertyArtist) as? String ?? "<Unknown>"
                    trackInfo.songName = title
                    trackInfo.artistName = artistName

                    trackInfo.isSelected = false
                    trackInfo.songURL = item.value(forProperty: MPMediaItemPropertyAssetURL) as? URL
                    album.songs?.append(trackInfo)
                    index += 1
                }
            }

        }

        // Finally add the album object to albums array
        allAlbumItems?.append(album)

    }


    print("Total Album count: \(allAlbumItems?.count)")

}