12.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=[[MPMediaPickerControlleralloc]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 the 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 "ViewController.h"#import <MediaPlayer/MediaPlayer.h>@interfaceViewController()<MPMediaPickerControllerDelegate>@property(nonatomic,strong)MPMediaPickerController*mediaPicker;@end@implementationViewController
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{self.mediaPicker=[[MPMediaPickerControlleralloc]initWithMediaTypes:MPMediaTypeAny];if(self.mediaPicker!=nil){NSLog(@"Successfully instantiated a media picker.");self.mediaPicker.delegate=self;
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