My goal is to get the cover picture for the playlists in iPod library. And I did something like
playlistMediaItemCollections = MPMediaQuery.playlistsQuery().collections ?? []
let artworks = playlistMediaItemCollections.map { $0.valueForKey(MPMediaItemPropertyArtwork) as? MPMediaItemArtwork }
But it results in error
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<MPConcreteMediaPlaylist 0x1468b1eb0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key artwork.'
Anyone knows how I can get the playlist artwork? Thanks
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.
let playlist = MPMediaQuery.playlistsQuery().collections?.first
let artworks = playlist?.items.map { $0.valueForProperty(MPMediaItemPropertyArtwork) as? MPMediaItemArtwork }