1

I'm looking for the MPMediaItem attribute that would allow me to sort my MPMediaPlaylist items array in the same order that it is showing up in the Music app. Anyone? Thanks.

JackyJohnson
  • 3,106
  • 3
  • 28
  • 35

2 Answers2

0

This is fairly trivial, although not immediately obvious. Once you have your playlist, simply call the items property instead of collections.

For example:

MPMediaPlaylist *playlist = //..

// Loop through the items in the original order
for (MPMediaItem *item in playlist.items) {
    NSLog(@"item: %@", item.title);
}

// Loop through the items sorted by title
for (MPMediaItemCollection *collection in playlist.collections) {
    NSLog(@"collection: %@", collection.title);
}
squarefrog
  • 4,750
  • 4
  • 35
  • 64
0
        let myPlaylistQuery = MPMediaQuery.playlists()
    let playlists = myPlaylistQuery.collections

    print(playlists as Any)
    for playlist in playlists! {
        print(playlist.value(forProperty: MPMediaPlaylistPropertyName)!)

        let songs = playlist.items
        for song in songs {
            let songTitle = song.value(forProperty: MPMediaItemPropertyTitle)
            print("\t\t", songTitle!)
        }