无法从MPMediaItem(mp3歌曲)转换为NSData

时间:2013-02-23 10:40:09

标签: iphone ios xcode ios6

我尝试过以下代码:

这是MPMediPickerController的委托方法:

- (void) mediaPicker: (MPMediaPickerController *) mediaPicker didPickMediaItems: (MPMediaItemCollection *) mediaItemCollection {

    // Dismiss the media item picker.
    [self dismissModalViewControllerAnimated: YES];

    NSLog(@"%@ %d",mediaItemCollection,mediaItemCollection.count);

    NSArray *newMediaItem= [mediaItemCollection items];


    MPMediaItem *item=[[newMediaItem objectAtIndex:0] retain];

    [self uploadMusicFile:item];
}

这是我对NSData的自定义方法MPMediaItem:

- (void) uploadMusicFile:(MPMediaItem *)song
{
    NSURL *url = [song valueForProperty: MPMediaItemPropertyAssetURL];


    AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL: url options:nil];

    AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset: songAsset
                                                                      presetName: AVAssetExportPresetPassthrough];

    exporter.outputFileType = @"public.mpeg-4";
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *exportFile = [documentsDirectory stringByAppendingPathComponent:
                            @"exported.mp4"];

    NSURL *exportURL = [[NSURL fileURLWithPath:exportFile] retain];
    exporter.outputURL = exportURL;

    [exporter exportAsynchronouslyWithCompletionHandler:
     ^{
         NSData *data = [NSData dataWithContentsOfFile: [documentsDirectory
                                                         stringByAppendingPathComponent: @"exported.mp4"]];

            NSLog(@"%@",data);
     }];
}

我在NSlog中获得“null”。

我也查了这篇文章:

how to convert nsdata to MPMediaitem song iOS Sdk

但没有得到解决方案。

我正在使用xcode 4.6和ios 6.1。

任何人都可以告诉我这里有什么问题吗?

0 个答案:

没有答案