[iOS][Swift]ミュージックライブラリにアクセスして音楽を再生する(MPMusicPlayerController使用) | nackpan Blog
 では、MPMusicPlayerControllerを用いて、ミュージックライブラリにアクセスしました。
そのときは、メディアピッカーを使いました。
 
今回は、メディアクエリを使用して、アイテムを取得する方法を紹介します。
 iPodライブラリアクセス プログラミングガイド(PDF)での「iPodライブラリの使用」部分の話になります
MPMediaQueryクラス
 MPMediaQuery Class Reference
MPMediaQueryクラスを使うと、
 「アルバムごとにまとめて」取得
 「アーティスト名を指定して」取得
 などグループ化したり、対象を絞り込んだりして、アイテムを取得することができます
グルーピング
グルーピングクエリには、
 albumsQuery
 artistsQuery
 songsQuery
 playlistsQuery
 podcastsQuery
 audiobooksQuery
 compilationsQuery
 composersQuery
 があります
グルーピングクエリは、MPMediaQuery!クラスを返します
 MPMediaQueryのプロパティ
 • collections
 • items
 に取得した内容が入っています。
collectionsは、MPMediaItemCollectionのarrayです
 (MPMediaItemCollectionはMPMediaItem(ミュージックライブラリアイテムを表すクラス)を集めて管理するクラスです)
 (albumsQueryの場合だと、アルバム一つ分が一つのMPMediaItemCollectionになっています)
itemsは、MPMediaItem(ミュージックライブラリアイテムを表すクラス)のarrayです。
MPMediaItem Class Reference
 MPMediaItemCollection Class Reference
アルバムごとにグルーピングして取得したのち、アルバムのタイトルを表示する例を示します
// 「アルバム」単位で取得
let query = MPMediaQuery.albumsQuery()
// 取得したアルバム情報を表示
if let collections = query.collections {
    for collection in collections {
        if let representativeTitle = collection.representativeItem!.albumTitle {
            print("アルバム名: \(representativeTitle)  曲数: \(collection.items.count)")
        }
    }
}フィルター
MPMediaQueryは、対象を絞るフィルターを加えることができます。
 それには
 addFilterPredicate(_ predicate: MPMediaPredicate!)
 を用います
アルバムごとにグルーピングして取得する例を先ほど示しましたが、そのさい、cloudItem(端末にないアイテム)を取り除く例を示します。
let query = MPMediaQuery.albumsQuery()
query.addFilterPredicate(MPMediaPropertyPredicate(value: false, forProperty: MPMediaItemPropertyIsCloudItem))
// 取得したアルバム情報を表示
if let collections = query.collections {
    for collection in collections {
        if let representativeTitle = collection.representativeItem!.albumTitle {
            print("アルバム名: \(representativeTitle)  曲数: \(collection.items.count)")
        }
    }
}MPMusicPlayerControllerでのクエリの利用
MPMusicPlayerControllerは、クエリを元に曲目キューをセットする
 setQueueWithQuery(_ query: MPMediaQuery)
 メソッドがあるので、クエリをそのまま渡してプレイヤーを作成することができます
let query = MPMediaQuery.albumsQuery() // プレイヤーにqueryから作成したキューをセット // (playerはMPMusicPlayerControllerのインスタンスを示す) player.setQueueWithQuery(query)
アーティスト名で絞り込むサンプル
サンプルプロジェクトを作成しました
 
 テキストフィールドにアーティスト名を入力して「実行」ボタンを押すと、入力したテキストを含むアーティストの曲を表示し、再生を開始します
        // アーティスト名で絞り込む
        // (ここのMPMediaPropertyPredicateで使われている変数artistは、String型でユーザーが入力したテキストから作成したもの)
        let predicate = MPMediaPropertyPredicate(value: artist, forProperty: MPMediaItemPropertyArtist, comparisonType: MPMediaPredicateComparison.Contains)
        query.addFilterPredicate(predicate)フィルターで、「指定したテキストを含むアーティスト」を選出しています。
プロジェクトはGitHubにあります
 nackpan/MediaQueryExample
ためしてみてくださいね
関連
[iOS][Swift]ミュージックライブラリにアクセスして音楽を再生する(MPMusicPlayerController使用) | nackpan Blog
 [iOS][Swift]ミュージックライブラリにアクセスして音楽を再生する(AVAudioPlayer使用) | nackpan Blog
 [iOS][Swift]AVAudioPlayerを使う(再生速度の変更、複数のプレイヤー) | nackpan Blog
 [iOS][Swift]AVAudioPlayerを使う(リソースファイルを使う) | nackpan Blog
 [iOS][Swift]AVAudioPlayerを使う(複数の曲をあつかう) | nackpan Blog
