MediaPlayer Framework 1
I’ve been working with the MediaPlayer framework for the last couple of years, bumping my shins into its rough corners early and often. I file radars for what I find, but it always feels like shouting into the void, so I figured I might as well double-dip and write stuff over here, too!
On today’s exciting episode: the delightful setQueueWithItemCollection:
method, and how it does something I really didn’t expect.
To make your iOS device play a song in its music library, an MPMusicPlayerController
needs to be given some songs. There’re a few different ways to do this, but the one I just mentioned is our huckleberry for today. That method accepts an MPMediaItemCollection
, which is - for our purposes - basically just an array of MPMediaItem
objects.
In Myoosic, I even (basically) do this:
- (void)setQueue:(NSArray <MPMediaItem*>*)items {
MPMediaItemCollection* c = [MPMediaItemCollection collectionWithItems:items];
[self.player setQueueWithItemCollection:c];
}
Here’s the bit that tripped me up today: what happens if that array is empty? After all, my setQueue:
method just takes an array - that array could very well be empty.
Well, what happens is hilarious and not at all what I expected: calling setQueueWithItemCollection:
with an empty collection will queue every song available in your library!
I don’t know if this is what you’d expect, but it certainly tripped me up. It’s easy enough to guard against an empty items
array, but it seems strange that I’d need to.