如何使用MPMediaQuery检索Artist列表?

时间:2013-01-28 18:33:49

标签: ios cocoa-touch mpmediaquery

您好我正在尝试使用以下代码在iOS中使用MPMediaQuery检索Artist。

在我的ViewDidL enter code here oad

MPMediaQuery *query = [MPMediaQuery artistsQuery];
self.arrayOfArtist = [query collections];

在我的cellForRowAtIndexPath

   cell.textLabel.text = [NSString stringWithFormat:@"%@",[[self.arrayOfArtist objectAtIndex:indexPath.row] valueForProperty:MPMediaItemPropertyArtist]];

当我使用NSLog查看时,我的arrayOfArtist计数约为330。

但是在我的UITableView中,它只显示NULL

我有什么不对的吗?

3 个答案:

答案 0 :(得分:6)

你应该写:

cell.textLabel.text = [NSString stringWithFormat:@"%@",[[[self.arrayOfArtist objectAtIndex:indexPath.row] representativeItem] valueForProperty:MPMediaItemPropertyArtist]];

答案 1 :(得分:2)

您需要获取artist属性,然后将其保存在数组中。如果您尝试在标准数组上使用它,则valueForProperty方法无法正常工作。

答案 2 :(得分:1)

您可以使用该代码检索艺术家及其歌曲。

    /// Get all artists and their songs
///
func getAllArtists() {
    let query: MPMediaQuery = MPMediaQuery.artists()
    let allArtists = query.collections

    allArtistItems?.removeAll()

    guard allArtists != nil else {
        return
    }

    for collection in allArtists! {
        let item: MPMediaItem? = collection.representativeItem




        let artistName = item?.value(forKey: MPMediaItemPropertyArtist) as? String ?? "<Unknown>"
        let artistId = item!.value(forProperty: MPMediaItemPropertyArtistPersistentID) as! NSNumber

        let artist = Artist()
        artist.name = artistName
        artist.artistId = String(describing: artistId)
        print("Artist name: \(artistName)")

        // Get all songs of this Artist
        let mediaQuery = MPMediaQuery.songs()
        let predicate = MPMediaPropertyPredicate.init(value: artistId, forProperty: MPMediaItemPropertyArtistPersistentID)
        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
                    artist.songs?.append(trackInfo)
                    index += 1
                }
            }

        }

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

    }


    print("Total Artist count: \(allArtistItems?.count)")

}