Let Start
1. Create Project.
2. Take button and initialize it.
3. Import MediaPlayer.framework.
#import <MediaPlayer/MediaPlayer.h>
4. On button click open media album to select music.
- (IBAction)btnMedia_clicked:(id)sender {
MPMediaPickerController *mediaPicker = [[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeMusic];
mediaPicker.delegate = self;
mediaPicker.allowsPickingMultipleItems = NO;
mediaPicker.prompt = @"Select songs to play";
// UINavigationController *navi=[[UINavigationController alloc] initWithRootViewController:mediaPicker];
[self presentViewController:mediaPicker animated:YES completion:Nil];
[mediaPicker release];
}
5. on Media player Delegate method, import music and save it to Document Directory
- (void) mediaPicker: (MPMediaPickerController *) mediaPicker didPickMediaItems: (MPMediaItemCollection *) mediaItemCollection
{
[self dismissViewControllerAnimated:YES completion:nil];
if (mediaItemCollection) {
MPMediaItem *song=[[[mediaItemCollection items] objectAtIndex:0] retain];
if (! song) {
return;
}
NSURL *assetURL = [song valueForProperty:MPMediaItemPropertyAssetURL];
AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL:assetURL options:nil];
NSLog (@"Core Audio %@ directly open library URL %@",
coreAudioCanOpenURL (assetURL) ? @"can" : @"cannot",
assetURL);
NSLog (@"compatible presets for songAsset: %@",
[AVAssetExportSession exportPresetsCompatibleWithAsset:songAsset]);
/* approach 1: export just the song itself
*/
AVAssetExportSession *exporter = [[AVAssetExportSession alloc]
initWithAsset: songAsset
presetName: AVAssetExportPresetAppleM4A];
NSLog (@"created exporter. supportedFileTypes: %@", exporter.supportedFileTypes);
exporter.outputFileType = @"com.apple.m4a-audio";
// exporter.outputFileType=@"com.apple.quicktime-movie";
NSString *exportFile = [myDocumentsDirectory() stringByAppendingPathComponent: @"exported.m4a"];
myDeleteFile(exportFile);
NSURL *exportURL = [[NSURL fileURLWithPath:exportFile] retain] ;
exporter.outputURL = exportURL;
// do the export
[exporter exportAsynchronouslyWithCompletionHandler:^{
int exportStatus = exporter.status;
switch (exportStatus) {
case AVAssetExportSessionStatusFailed: {
// log error to text view
NSError *exportError = exporter.error;
NSLog (@"AVAssetExportSessionStatusFailed: %@", exportError);
//errorView.text = exportError ? [exportError description] : @"Unknown failure";
// errorView.hidden = NO;
break;
}
case AVAssetExportSessionStatusCompleted: {
NSLog (@"AVAssetExportSessionStatusCompleted");
// fileNameLabel.text = [exporter.outputURL lastPathComponent];
// set up AVPlayer
//[self setUpAVPlayerForURL: exporter.outputURL];
// [self enablePCMConversionIfCoreAudioCanOpenURL: exporter.outputURL];
break;
}
case AVAssetExportSessionStatusUnknown: { NSLog (@"AVAssetExportSessionStatusUnknown"); break;}
case AVAssetExportSessionStatusExporting: { NSLog (@"AVAssetExportSessionStatusExporting"); break;}
case AVAssetExportSessionStatusCancelled: { NSLog (@"AVAssetExportSessionStatusCancelled"); break;}
case AVAssetExportSessionStatusWaiting: { NSLog (@"AVAssetExportSessionStatusWaiting"); break;}
default: { NSLog (@"didn't get export status"); break;}
}
}];
}
}
-(void)mediaPickerDidCancel:(MPMediaPickerController *)mediaPicker
{
[self dismissViewControllerAnimated:YES completion:nil];
}
IT will import song and put it on document directory