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) ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access