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) ...
Get Programming iOS 8 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.