iPhone 获取所有专辑/艺术家

Posted

技术标签:

【中文标题】iPhone 获取所有专辑/艺术家【英文标题】:iPhone get all albums/artists 【发布时间】:2010-10-15 09:26:41 【问题描述】:

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

提前致谢!

【问题讨论】:

【参考方案1】:

使用 MPMediaQuery:

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

allItems 数组现在包含 MPMediaItemCollections,分组由专辑完成。现在您可以遍历数组。

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

【讨论】:

【参考方案2】:

感谢您的回答,这是打印出专辑和艺术家的工作示例代码,以防有人需要:

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】:

给你。您可以获取专辑及其歌曲。

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


【讨论】:

以上是关于iPhone 获取所有专辑/艺术家的主要内容,如果未能解决你的问题,请参考以下文章

按艺术家获取所有 Spotify 歌曲的音频属性

如何在 Spotify 上获取艺术家的所有歌曲?

从 iOS 媒体库中获取所有艺术家的列表

SPAlbumBrowse - 获取艺术家专辑的准确列表

如何使用 spotify api 获取艺术家的所有曲目?

iphone - 在应用程序上打开艺术家链接