NOt able to convert from MPMediaItem(mp3 song) to NSData

…衆ロ難τιáo~ 提交于 2019-12-11 16:49:29

问题


I have tried following code :

These is my delegate method of 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];
}

This is my custom method MPMediaItem to NSData:

- (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);
     }];
}

I am getting "null" in NSlog.

I have also check this post :

how to convert nsdata to MPMediaitem song iOS Sdk

but not getting solution.

I am using xcode 4.6 and ios 6.1.

Can any one tell me what is wrong here?

来源:https://stackoverflow.com/questions/15039578/not-able-to-convert-from-mpmediaitemmp3-song-to-nsdata

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!