Swift MediaPlayer ミュージックAlbum一覧取得
概要
ミュージックのAlbum一覧取得。
Apple Musicで追加したAlbumも対象にしたい。
ソース
example
曲数5曲より大きいAlbumを表示。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | import UIKit import MediaPlayer class ViewController : UIViewController { override func viewDidLoad () { super . viewDidLoad () let mPMediaQuery = MPMediaQuery . albums () if let collections = mPMediaQuery . collections { print ( collections . count ) for collection in collections { if collection . count > 5 { print ( "\( collection . items [ 0 ]. albumTitle ! ) : \( collection . count )" ) } } } } } |
注意
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.」