Zuyin XU
Zuyin XU

Reputation: 43

How to get MPMediaPlaylist's user defined cover image and description?

I know how to get MPMediaQuery's title by:

MPMediaQuery *playlistsQuery = [MPMediaQuery playlistsQuery];

NSArray *items = [playlistsQuery collections];

MPMediaPlaylist *myPlaylist = items.firstObject;

NSLog(@"%@",myPlaylist.name); //"New playlist title"

Does anyone know how to access MPMediaPlaylist's cover & description?

Screenshot - User created Playlist

Upvotes: 4

Views: 687

Answers (2)

Adam
Adam

Reputation: 1913

Getting description is easy, just use

var descriptionText: String? { get }

from MPMediaPlaylist

To get user defined image you have to use private API, here's an extension you can use to get image stored on disk, or get image's URL:

extension MPMediaPlaylist {
    /**
     User selected image for playlist stored on disk.
     */
    var userImage: UIImage? {
        guard let catalog = value(forKey: "artworkCatalog") as? NSObject else {
            return nil
        }

        let sel = NSSelectorFromString("bestImageFromDisk")

        guard catalog.responds(to: sel),
            let value = catalog.perform(sel)?.takeUnretainedValue(),
            let image = value as? UIImage else {
            return nil
        }
        return image
    }

    /**
     URL for playlist's image.
     */
    var imageUrl: URL? {
        if let catalog = value(forKey: "artworkCatalog") as? NSObject,
            let token = catalog.value(forKey: "token") as? NSObject,
            let url = token.value(forKey: "availableArtworkToken") as? String {
            return URL(string: "https://is2-ssl.mzstatic.com/image/thumb/\(url)/260x260cc.jpg")
        }
        return nil
    }
}

As always, keep in mind that it can stop working anytime when Apple changes stuff under the hood or your app might get rejected from App Store.

Upvotes: 1

bryanjclark
bryanjclark

Reputation: 6434

I haven't figured out the custom playlist artwork, but I do know how to get the playlist description! Here's the answer in Swift 3 syntax:

let allPlaylists = MPMediaQuery.playlists().collections
let playlist = allPlaylists.first

// I dunno how to get the custom playlist artwork, 
// but you can get artwork from items in the playlist:
let artwork = playlist?.representativeItem?.artwork

// The description is here:
let description = playlist?.descriptionText

Upvotes: 0

Related Questions