LoginSignup
1
2

More than 5 years have passed since last update.

Swift MediaPlayer ミュージックAlbum一覧取得

Posted at

概要

ミュージックのAlbum一覧取得。
Apple Musicで追加したAlbumも対象にしたい。

ソース

example

曲数5曲より大きいAlbumを表示。

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.」

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2