将MPMediaItem转换为NSData

| 我正在尝试将MPMediaItem转换为NSData对象,以便可以使用AVAudioPlayer播放它。 在类似问题上发布了以下答案,其中包含有关如何解决该问题的步骤: MPMediaItems原始歌曲数据   当然,您可以访问   MPMediaItem。不清楚   一次,但是有效。就是这样:      从中获取媒体项目的网址   MPMediaItemPropertyAssetURL属性   使用此URL初始化AVURLAsset   以此初始化AVAssetReader   资产获取所需的AVAssetTrack   从AVURLAsset读取创建一个   AVAssetReaderTrackOutput与此   将此输出添加到   在之前和之后创建的AVAssetReader   -startReading使用AVAssetReaderTrackOutput的获取所有数据   -copyNextSampleBuffer利润! 使用\'copyNextSampleBuffer \'提取数据后,我现在有了CMSampleBufferRef对象。我如何从这里继续? 谢谢, 吉利     
已邀请:
尝试这个:
-(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;
             }
         }
     }];
}
    
尝试使用以下代码
 MPMediaItem *item;
 NSData *dataMedia = [NSData dataWithContentsOfURL:[item valueForProperty:MPMediaItemPropertyAssetURL]];
    

要回复问题请先登录注册