Swift MediaPlayer ミュージックPlaylist一覧取得
概要
ミュージックのPlaylist一覧取得。
ソース
example
import UIKit
import MediaPlayer
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let mPMediaQuery = MPMediaQuery.playlists()
if let collections = mPMediaQuery.collections {
print(collections.count)
for collection in collections {
print("-/\\-")
print("\(collection.mediaTypes)")
collection.toString()
}
}
}
}
extension MPMediaItemCollection {
func toString() {
print("MPMediaItemCollection")
let keys = [MPMediaPlaylistPropertyName]
for key in keys {
if let value = self.value(forKey: key) {
if let value = value as? Bool {
print("- Bool \(key):\(value)")
}
else if let value = value as? String {
print("- String \(key):\(value)")
}
else if let value = value as? Int {
print("- Int \(key):\(value)")
}
}
}
}
}
自分で作成したPlaylistはMPMediaType 1(MPMediaType.music)です。
MPMediaType 0は「トップレート」「最近再生した項目」「最近追加した項目」等です。
-/\- MPMediaType(rawValue: 0) MPMediaItemCollection - String name:トップレート -/\- MPMediaType(rawValue: 0) MPMediaItemCollection - String name:最近再生した項目 -/\- MPMediaType(rawValue: 0) MPMediaItemCollection - String name:最近追加した項目 -/\- MPMediaType(rawValue: 1) MPMediaItemCollection - String name:トキオドライブ
プレイリスト名取得
MPMediaItemCollectionのMPMediaPlaylistPropertyName。
注意
Info.plistに「Privacy – Media Library Usage Description」追加が必要。
追加しない場合、実行時エラーになります。
「[access] This app has crashed because it attempted to access privacy-sensitive data without a usage description. The app’s Info.plist must contain an NSAppleMusicUsageDescription key with a string value explaining to the user how the app uses this data.」




