【发布时间】:2014-02-27 06:35:18
【问题描述】:
我正在制作一个音乐应用程序,用户可以在其中访问 ios 音乐库并将歌曲保存到其应用程序(即文档目录)。我可以使用 MPMediaPickerController 访问音乐库,但不知道如何处理其委托方法以便将所选歌曲保存到我的文档目录。 目前我正在使用此代码
- (void) mediaPicker: (MPMediaPickerController *) mediaPicker
didPickMediaItems: (MPMediaItemCollection *) collection
{
[self dismissViewControllerAnimated:YES completion:nil];
[self playSelectedMediaCollection: collection];
}
- (void) playSelectedMediaCollection: (MPMediaItemCollection *) collection {
if (collection.count == 1) {
NSArray *items = collection.items;
MPMediaItem *mediaItem = [items objectAtIndex:0];
[self mediaItemToData:mediaItem];
}
}
-(void)mediaItemToData:(MPMediaItem*)mediaItem
{
// Implement in your project the media item picker
MPMediaItem *curItem = mediaItem;//musicPlayer.nowPlayingItem;
NSURL *url = [curItem valueForProperty: MPMediaItemPropertyAssetURL];
AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL: url options:nil];
AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset: songAsset
presetName: AVAssetExportPresetPassthrough];
exporter.outputFileType = @"public.mpeg-4";
NSString *exportFile = [[self myDocumentsDirectory] stringByAppendingPathComponent:
@"exported.mp4"];
NSURL *exportURL = [NSURL fileURLWithPath:exportFile] ;
exporter.outputURL = exportURL;
NSData *data = [NSData dataWithContentsOfFile: [[self myDocumentsDirectory]
stringByAppendingPathComponent: @"exported.mp4"]];
//
// NSLog(@"%@",data);
// NSURL *audioUrl = exportURL;
// NSLog(@"Audio Url=%@",audioUrl);
// audioData = [NSData dataWithContentsOfURL:audioUrl];
// NSLog(@"%@",audioData);
// Do with data something
// do the export
// (completion handler block omitted)
[exporter exportAsynchronouslyWithCompletionHandler:
^{
// int exportStatus=exporter.status;
NSLog(@"%d export status",exporter.status);
if (exporter.status==AVAssetExportSessionStatusCompleted)
{
NSLog(@"successfull");
}
NSData *data = [NSData dataWithContentsOfFile: [[self myDocumentsDirectory]
stringByAppendingPathComponent: @"exported.mp4"]];
NSURL *audioUrl = exportURL;
NSLog(@"Audio Url=%@",audioUrl);
audioData = [NSData dataWithContentsOfURL:audioUrl];
NSLog(@"%@",audioData);
// Do with data something
}];
}
在上面的代码中,调试器永远不会来到导出会话异步块。让我知道上述代码中是否需要任何修改,或者您是否有任何工作代码可满足我的要求。 提前谢谢....
【问题讨论】:
标签: ios iphone objective-c mpmusicplayercontroller avassetexportsession