Using the Media Item Picker

The media item picker is the pre-packaged view controller for letting a user choose media items from the device iPod library. Using the picker is very simple:

  1. Designate a controller object as a delegate of the picker.

  2. Invoke the picker from the controller.

  3. When the user indicates that they are finished, the delegate receives the chosen collection of media items and dismisses the picker.

Setting Up a Media Item Picker Delegate

To set up a controller object as a media item picker delegate, first add the protocol’s name in the interface declaration in the controller’s header file, as follows:

@interface myController : UIViewController <MPMediaPickerControllerDelegate> {
    // interface declaration
}

Next, implement the two delegate methods from that protocol. The first method, shown in Listing 3-1, responds to the user having chosen some media items. It dismisses the picker and invokes the controller’s playback queue update method.

Listing 3-1  Responding to a new collection of media items from the picker

- (void) mediaPicker: (MPMediaPickerController *) mediaPicker
            didPickMediaItems: (MPMediaItemCollection *) collection {
 
    [self dismissModalViewControllerAnimated: YES];
    [self updatePlayerQueueWithMediaCollection: collection];
}

For example code showing how to update a playback queue, see Listing 2-5.

The second picker delegate method handles the case of the user tapping Done without having chosen any items to play. See Listing 3-2 for a basic implementation.

Listing 3-2  Responding if the user cancels the picker

- (void) mediaPickerDidCancel: (MPMediaPickerController *) mediaPicker {
 
    [self dismissModalViewControllerAnimated: YES];
}

Displaying a Media Item Picker

Listing 3-3 shows how to configure and display a media item picker—including establishing the controller object as the delegate of the picker.

Listing 3-3  Displaying a media item picker

MPMediaPickerController *picker =
    [[MPMediaPickerController alloc]
        initWithMediaTypes: MPMediaTypeAnyAudio];                   // 1
 
[picker setDelegate: self];                                         // 2
[picker setAllowsPickingMultipleItems: YES];                        // 3
picker.prompt =
    NSLocalizedString (@"Add songs to play",
                        "Prompt in media item picker");
 
[myController presentModalViewController: picker animated: YES];    // 4
[picker release];

Here’s how this code works:

  1. Creates a media item picker. The parameter indicates the sort of media items to display. For options, see the Media Item Type Flags enumeration.

  2. Establishes your controller object as the delegate.

  3. Specifies that the user can pick multiple items. You can instead display a single-item picker by excluding this statement; the default behavior is to disallow multiple selection.

  4. Displays the picker. The myController object retains it so you then release it to balance the alloc call.