iOS 6 issue Convert MPMediaItem to NSData

后端 未结 3 1273
星月不相逢
星月不相逢 2021-02-04 17:42

i had tried the below code.

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

            


        
3条回答
  •  悲&欢浪女
    2021-02-04 18:15

    i have found the solution for iOS 6.See below modify code

    -(void)mediaItemToData : (MPMediaItem * ) curItem
    {
        NSURL *url = [curItem valueForProperty: MPMediaItemPropertyAssetURL];
    
        AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL: url options:nil];
    
        AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset: songAsset
                                                                          presetName:AVAssetExportPresetAppleM4A];
    
        exporter.outputFileType =   @"com.apple.m4a-audio";
    
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString * myDocumentsDirectory = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    
        [[NSDate date] timeIntervalSince1970];
        NSTimeInterval seconds = [[NSDate date] timeIntervalSince1970];
        NSString *intervalSeconds = [NSString stringWithFormat:@"%0.0f",seconds];
    
        NSString * fileName = [NSString stringWithFormat:@"%@.m4a",intervalSeconds];
    
        NSString *exportFile = [myDocumentsDirectory stringByAppendingPathComponent:fileName];
    
        NSURL *exportURL = [NSURL fileURLWithPath:exportFile];
        exporter.outputURL = exportURL;
    
        // do the export
        // (completion handler block omitted)
        [exporter exportAsynchronouslyWithCompletionHandler:
         ^{
             int exportStatus = exporter.status;
    
             switch (exportStatus)
             {
                 case AVAssetExportSessionStatusFailed:
                 {
                     NSError *exportError = exporter.error;
                     NSLog (@"AVAssetExportSessionStatusFailed: %@", exportError);
                     break;
                 }
                 case AVAssetExportSessionStatusCompleted:
                 {
                     NSLog (@"AVAssetExportSessionStatusCompleted");
    
                     NSData *data = [NSData dataWithContentsOfFile: [myDocumentsDirectory
                                                                     stringByAppendingPathComponent:fileName]];
    
                     //DLog(@"Data %@",data);
                     data = nil;
    
                     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;
                 }
             }
         }];
    }
    

    See this link for more info.

提交回复
热议问题