Chapter 16. Music Library

An iOS device can be used for the same purpose as the original iPod — to hold and play music, podcasts, and audiobooks. These items constitute the device’s music library. (The relevant guide in Apple’s documentation, iPod Library Access Programming Guide, preserves a more archaic name.) iOS provides the programmer with various forms of access to the device’s music library; you can:

  • Explore the music library.
  • Play an item from the music library.
  • Learn and control what the Music app’s music player is doing.
  • Present a standard interface for allowing the user to select a music library item.

These abilities are provided by the Media Player framework; you’ll need to import MediaPlayer.

Exploring the Music Library

Everything in the music library, as seen by your code, is an MPMediaEntity. This is an abstract class. It has two concrete subclasses:

MPMediaItem
An MPMediaItem is a single item (a “song”).
MPMediaCollection
An MPMediaCollection is an ordered list of MPMediaItems, rather like an array; it has a count, and its items property is an array.

MPMediaEntity endows its subclasses with the ability to describe themselves through key–value pairs called properties. The property keys have names like MPMediaItemPropertyTitle. To fetch a property’s value, call valueForProperty: with its key. You can fetch multiple properties with enumerateValuesForProperties:usingBlock:. Thus, the use of the word “properties” here has nothing to do with object properties; these ...

Get Programming iOS 9 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.