O'Reilly logo

Programming iOS 8 by Matt Neuburg

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 that 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 properties are more like entries in an NSDictionary. However, starting in iOS 7, an MPMediaEntity is endowed with some object properties whose names correspond to the property names. Thus, for example, you can say either myItem.valueForProperty(MPMediaItemPropertyTitle) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required