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 iPod library, or music library. 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 that endows its subclasses with the ability to describe themselves through key–value pairs called properties. (This use of the word “properties” has nothing to do with Objective-C language properties; these properties are more like entries in an NSDictionary.)

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

An MPMediaItem has a type, according to the value of its MPMediaItemPropertyMediaType property: it might, for example, be music, a podcast, an audiobook, or a video. A media item’s properties will be intuitively familiar from your use of iTunes: ...

Get Programming iOS 7, 4th Edition now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.