How to retrieve Artist lists with MPMediaQuery?
Asked Answered
H

3

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?

Hypabyssal answered 28/1, 2013 at 18:33 Comment(0)
F
7

you should write:

cell.textLabel.text = [NSString stringWithFormat:@"%@",[[[self.arrayOfArtist objectAtIndex:indexPath.row] representativeItem] valueForProperty:MPMediaItemPropertyArtist]];
For answered 28/1, 2013 at 19:4 Comment(0)
S
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)")

}
Shaduf answered 3/10, 2016 at 5:26 Comment(4)
Dude, its a question about objective C, why give an answer in swift?Swords
Logic remains same. I believe, anyone who needs help can convert this code to Objc. Please let me know, I can do the same.Shaduf
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.Mathewson
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.Mathewson
M
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.

Mishandle answered 28/1, 2013 at 19:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.