iPhone get all albums/artists

前提是你 提交于 2019-12-31 13:31:08

问题


does any of you have example code (or a link to it) of how to retrieve all music albums or artist from the iPod media library?

Thanks in advance!


回答1:


Use a MPMediaQuery:

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

The allItems array does now contain MPMediaItemCollections, grouping is done by album. Now you can walk through the arrays.

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



回答2:


Thanks for the answer, here is working sample code that prints out the albums and artists in case someone needs it:

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]);



回答3:


Here you go. You can get the albums and their songs.

    /// 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)")

}


来源:https://stackoverflow.com/questions/3940985/iphone-get-all-albums-artists

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!