4

我知道如何通过以下方式获得 MPMediaQuery 的标题:

MPMediaQuery *playlistsQuery = [MPMediaQuery playlistsQuery];

NSArray *items = [playlistsQuery collections];

MPMediaPlaylist *myPlaylist = items.firstObject;

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

有谁知道如何访问 MPMediaPlaylist 的封面和描述?

屏幕截图 - 用户创建的播放列表

4

2 回答 2

1

获取描述很容易,只需使用

var descriptionText: String? { get }

MPMediaPlaylist

要获取用户定义的图像,您必须使用私有 API,这是一个可用于获取存储在磁盘上的图像或获取图像的扩展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
    }
}

与往常一样,请记住,当 Apple 在后台更改内容或您的应用程序可能被 App Store 拒绝时,它可能随时停止工作。

于 2020-04-03T11:16:38.913 回答
0

我还没有弄清楚自定义播放列表的插图,但我知道如何获取播放列表描述!这是 Swift 3 语法中的答案:

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
于 2017-03-01T18:22:25.027 回答