O'Reilly logo

iOS 5 Programming Cookbook by Vandad Nahavandipoor

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

9.8. Accessing the Music Library

Problem

You want to access an item that your user picks from her music library.

Solution

Use the MPMediaPickerController class:

MPMediaPickerController *mediaPicker = [[MPMediaPickerController alloc]
                                        initWithMediaTypes:MPMediaTypeAny];

Discussion

MPMediaPickerController is a view controller that the Music app displays to the user. By instantiating MPMediaPickerController, you can present a standard view controller to your users to allow them to select whatever item they want from their library and then transfer the control to your application. This is particularly useful in games, for instance, where the user plays the game and can have your application play his favorite tracks in the background.

You can get information from the media picker controller by becoming its delegate (conforming to MPMediaPickerControllerDelegate):

#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>

@interface Accessing_the_Music_LibraryViewController
           : UIViewController <MPMediaPickerControllerDelegate>

@end

Inside your displayMediaPicker: selector, implement the code required to display an instance of the media picker controller and present it to the user as a modal view controller:

- (void) displayMediaPicker{ MPMediaPickerController *mediaPicker = [[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeAny]; if (mediaPicker != nil){ NSLog(@"Successfully instantiated a media picker."); mediaPicker.delegate = self; mediaPicker.allowsPickingMultipleItems = NO; [self.navigationController ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required