How to Pull The Artist Value from Mpmediaitemcollection

How to pull the artist value from MPMediaItemCollection

You access the properties of a MPMediaItemCollection via it's representativeItem. You fetch the artist's name like so:

let artist : MPMediaItemCollection = tableData.collections![indexPath.row]
let artistName = artist.representativeItem.artist ?? "Unknown Artist"

Though if I were you I wouldn't forcibly unwrap tableData.collections because if your user has an empty iTunes library that line will case the application to crash.

How to get artwork from MPMediaItemCollection

You should use valueForProperty instead:

$0.valueForProperty(MPMediaItemPropertyArtwork) as? MPMediaItemArtwork

However, I think unlike songs or albums, MediaPlayer API does not provide such property key that lets you retrieve the artwork of a playlist. You can check out possible ones that can be used with MPMediaPlaylist class:

let MPMediaPlaylistPropertyPersistentID: String
let MPMediaPlaylistPropertyName: String
let MPMediaPlaylistPropertyPlaylistAttributes: String
let MPMediaPlaylistPropertySeedItems: String

One alternative is, you can get artworks of songs in the playlist, and show either one of them or combine them to create a new artwork for the playlist.

I think Music app does the same thing like below if a playlist doesn't have an artwork image.

Sample Image

let playlist = MPMediaQuery.playlistsQuery().collections?.first
let artworks = playlist?.items.map { $0.valueForProperty(MPMediaItemPropertyArtwork) as? MPMediaItemArtwork }

Fetch list of all artists from iOS Media Library

For a list of artists, use MPMediaItemPropertyArtist rather than MPMediaPlaylistPropertyName for the media item property key.

Since your data source is using a MPMediaItemCollection, use the representativeItem property to get the artist title string for each collection.

Then set the textLabel's text to the artist string.

MPMediaItemCollection *artistCollection = self.artistsArray[indexPath.row];
NSString *artistTitle = [[artistCollection representativeItem] valueForProperty:MPMediaItemPropertyArtist];
cell.textLabel.text = artistTitle;

MPMediaQuery.artistsQuery() to show in groups

I'm going to answer my own question...

The problem was here:

let artist: MPMediaItemCollection = tableData.collections![indexPath.row]
if artist.valueForProperty(MPMediaItemPropertyArtist) == nil {

and is answered here:
How to pull the artist value from MPMediaItemCollection

Thanks to Wes...

ios - How to play MPMediaItemCollection?

This code creates three separate music player controllers. Make one and store it in a variable, then send the messages to it

MPMusicPlayerController().setQueueWithItemCollection((decodedPlaylistData[indexPath.row] as? MPMediaItemCollection)!)

MPMusicPlayerController().nowPlayingItem = (decodedPlaylistData[indexPath.row] as? MPMediaItemCollection)!.items[0] as! MPMediaItem

MPMusicPlayerController().play()

iOS: Getting and displaying album artwork from iPod library

You should be able to fetch the artwork into a UIImage via the following:

MPMediaItemArtwork *itemArtwork = [mediaItem valueForProperty:MPMediaItemPropertyArtwork];
UIImage *artworkUIImage = [itemArtwork imageWithSize:CGSizeMake(64, 64)];

In essence, the MPMediaItemPropertyArtwork property returns a MPMediaItemArtwork which you can then obtain a UIImage from.

Need good explanation of MPMediaQuery results for collections and items

OK, I solved this myself with a lot more research.
The trick here is to use the ITEMS which are sorted correctly, and build a new Collection to use as the queue.

All it takes is that addition of a single line of code:

let collection = MPMediaItemCollection(items: newQry.items!)

and then a change to the setQueue function:

myMP.setQueue(with: collection)

Here's the final working code block - compare to my original post OP above:

    let newQry = MPMediaQuery.albums()
newQry.addFilterPredicate(
MPMediaPropertyPredicate(
value: artistArray[indexPath.row + currLoc],
forProperty: MPMediaItemPropertyAlbumArtist,
comparisonType: .equalTo
)
)
//build a new collection with the sorted items then load the collection!
let collection = MPMediaItemCollection(items: newQry.items!)
myMP.stop()
myMP.setQueue(with: collection)
myMP.play()


Related Topics



Leave a reply



Submit