Hacking MPMediaQuery for better album shuffling

So I’m working on a Music app.  I’m a little frustrated with Apple’s assumption that we all have massive iCloud limits and/or live in a country with great mobile reception.  I live in Germany and as soon as you leave the cities, your mobile reception gets really sketchy unless you are a subscriber of the state “monopoly” Deutsche Telekom.  German bureaucracy dictates that anything with “Deutsche” in the business name will be grossly overpriced and half as effective, while customer service being usually horrible.

This is a post about programming!  😀  So yeah, I’m working on an app that’s to be a lot like the original Music app on the iPod touch: Just show me the songs I have on this device, and make it easy for me to navigate around.

One thing that became apparent was MPMusicPlayerController‘s inability to pick random albums.  It can pick random songs, but not random albums and then play the album.  It can first shuffle through the songs in an album before moving on to another album, but really?  Why would I insult an artist by listening to their album in the wrong order?  Can you imagine listening to Pink Floyd’s Dark Side of the Moon with the tracks shuffled?  Makes no sense, except perhaps a Greatest Hits album…

So I thought “why don’t I just hack with the MPMediaQuery class?  And I found a solution that works.  At least if you aren’t using this query for UITableView and populating all that, as you’d require itemSections, and collectionSections, etc.

So, without going into too much more detail, if you have already got a bit of experience with using the MediaPlayer framework on iOS, you’ll know that it’s a little quirky, as most of the audio folks tend to be over at Apple / everywhere (former Audio Designer here)…

import UIKit
import MediaPlayer

class MPHackMediaQuery: MPMediaQuery {

    private var modifiedItems: [MPMediaItem]?
    private var modifiedCollections: [MPMediaItemCollection]?
    
    init(with query: MPMediaQuery) {
        super.init(filterPredicates: query.filterPredicates)
        self.groupingType = query.groupingType
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    override var items: [MPMediaItem]? {
        if modifiedCollections == nil {
            if let collections = super.collections {
                modifiedCollections = self.randomize(collections)
            }
        }
        if modifiedItems == nil {
            if let collections = self.modifiedCollections {
                var items = [MPMediaItem]()
                for collection in collections {
                    items.append(contentsOf: collection.items)
                }
                modifiedItems = items
                
            } else {
                return nil
            }
        }
        
        return modifiedItems
    }
    
    override var collections: [MPMediaItemCollection]? {
        
        if modifiedCollections == nil {
            if let collections = super.collections {
                modifiedCollections = self.randomize(collections)
            }
        }
        
        return modifiedCollections
    }
    
    private func randomize(_ collections: [MPMediaItemCollection]) -> [MPMediaItemCollection] {
        
        var indices: [Int] = []
        for i in 0.. 0
        
        return shuffledCollections
    }
}

So as you can see, you can set a queue on your MPMusicPlayerController using a MPMediaQuery and this will work… IF you set your controller’s shuffleMode to .off, which makes sense because we aren’t shuffling the songs of the album, we just “pre-shuffled” the order in which the albums are chosen.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s