5

Hello i am trying to retrieve Artist with MPMediaQuery in iOS with following code.

In My ViewDidLenter code hereoad

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

And In my cellForRowAtIndexPath

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

When i check with NSLog, my arrayOfArtist count is about 330.

However in my UITableView , it's only show NULL.

Is there anything i am wronging?

Fire Fist
  • 7,032
  • 12
  • 63
  • 109

3 Answers3

7

you should write:

cell.textLabel.text = [NSString stringWithFormat:@"%@",[[[self.arrayOfArtist objectAtIndex:indexPath.row] representativeItem] valueForProperty:MPMediaItemPropertyArtist]];
Matthias Nagel
  • 386
  • 3
  • 5
5

You can use the code to retrieve the artists and their songs.

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

}
Abdul Yasin
  • 3,480
  • 1
  • 28
  • 42
  • Dude, its a question about objective C, why give an answer in swift? – Jobalisk Dec 13 '18 at 01:12
  • Logic remains same. I believe, anyone who needs help can convert this code to Objc. Please let me know, I can do the same. – Abdul Yasin Dec 13 '18 at 08:26
  • Hello. Can you give me a link with a tutorial or sample code to get a list of artists from my iPhone library (like Apple's music application)? With sections: A, B, C... and the rows corresponding to sections (Artist names) Exactly what the Apple music App does. It is impossible to find a complete code to understand how it works. There is no information. I am desperate. I will appreciate your help. Thank you. – Markus May 06 '20 at 15:32
  • Well, there is information: the Apple documentation. But I'm either stupid, or I have no way to write the code so that I can achieve something. I've only achieved to show songs in a TableView. – Markus May 06 '20 at 15:35
2

You need to grab the artist property and then save it in the array. The valueForProperty method does not work correctly if your trying to use it on a standard array.

Makleesh
  • 988
  • 10
  • 15