The Music Picker

The music picker (MPMediaPickerController) is a view controller (UIViewController) whose view is an self-contained navigation interface in which the user can select a media item. This interface looks very much like the iPod/Music app. You have no access to the actual view; you are expected to present the view controller modally (or, on the iPad, in a popover).

You can limit the type of media items displayed by creating the controller using initWithMediaTypes:. You can make a prompt appear at the top of the navigation bar (prompt). And you can govern whether the user can choose multiple media items or just one, with the allowsPickingMultipleItems property. That’s all there is to it.

While the view is showing, you learn what the user is doing through two delegate methods (MPMediaPickerController):

  • mediaPicker:didPickMediaItems:
  • mediaPickerDidCancel:

How you use these depends on the value of the controller’s allowsPickingMultipleItems:

The controller’s allowsPickingMultipleItems is NO (the default)
Every time the user taps a media item, your mediaPicker:didPickMediaItems: is called, handing you an MPMediaItemCollection consisting of all items the user has tapped so far (including the same item multiple times if the user taps the same item more than once). When the user taps Cancel, your mediaPickerDidCancel: is called.
The controller’s allowsPickingMultipleItems is YES
The interface has Plus buttons at the right end of every media item, similar to the iPod/Music ...

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