天天看点

iPod Library Access Programming Guide 3 -- Media Item PickerUsing the Media Item Picker

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

@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];
}
           
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  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];
           

继续阅读