Using the MPMediaPickerController
instance you can choose from the iPod library's song list, album list, etc.. Here is an example which selects all the songs from the iPod and displays in a modal view controller.
- (IBAction) selectSong: (id) sender
{
MPMediaPickerController *picker =
[[MPMediaPickerController alloc] initWithMediaTypes: MPMediaTypeMusic];
picker.delegate = self;
picker.allowsPickingMultipleItems = NO;
picker.prompt = NSLocalizedString (@"Select any song from the list", @"Prompt to user to choose some songs to play");
[self presentModalViewController: picker animated: YES];
[picker release];
}
Now you need to implement the delegate to store the song into your local variable. Here, selectedSongCollection
is an instance of MPMediaItemCollection
.
- (void) mediaPicker: (MPMediaPickerController *) mediaPicker didPickMediaItems: (MPMediaItemCollection *) mediaItemCollection
{
[self dismissModalViewControllerAnimated: YES];
selectedSongCollection=mediaItemCollection;
}
After you are done with selecting the song, implement the delegate to dismiss the picker:
- (void) mediaPickerDidCancel: (MPMediaPickerController *) mediaPicker
{
[self dismissModalViewControllerAnimated: YES];
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…