Ios 某些文件的AVAssetExport失败

Ios 某些文件的AVAssetExport失败,ios,objective-c,avassetexportsession,Ios,Objective C,Avassetexportsession,我已尝试从iPod库导出音频文件。我的目标是使用此iPod库文件在应用程序文档文件夹中创建新文件。它无法仅为某些项目创建文件。下面是我的代码片段 AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL: url options:nil]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSStri

我已尝试从iPod库导出音频文件。我的目标是使用此iPod库文件在应用程序文档文件夹中创建新文件。它无法仅为某些项目创建文件。下面是我的代码片段

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

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

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

exporter.outputFileType = @"com.apple.m4a-audio";

NSString *songName  =   [filename stringByAppendingString:@".m4a"];

NSString *musicfilepath  = [documentsDirectory stringByAppendingPathComponent:@"musics/"];

[[NSFileManager defaultManager] createDirectoryAtPath:musicfilepath withIntermediateDirectories:YES attributes:nil error:nil];

NSString *exportFile = [musicfilepath stringByAppendingPathComponent:songName];



NSError *error1;

if([[NSFileManager defaultManager] fileExistsAtPath:exportFile]) 
{

    [[NSFileManager defaultManager] removeItemAtPath:exportFile error:&error1];

}

NSURL* exportURL = [[NSURL fileURLWithPath:exportFile] retain];

exporter.outputURL = exportURL; 
尝试使用错误处理程序块时,我收到如下所示的错误:

    [exporter exportAsynchronouslyWithCompletionHandler:^{

    int exportStatus = exporter.status;

    switch (exportStatus) {

        case AVAssetExportSessionStatusFailed: {

            NSError *exportError = exporter.error;

            NSLog (@"AVAssetExportSessionStatusFailed: %@", exportError);

            break;
        }
  }
 }];

AvassetExportSessionStatus失败:错误域=AVFoundationErrorDomain代码=-11800“操作无法完成”用户信息=0x214f20{NSLocalizedFailureReason=发生未知错误(-12124),NSUnderlyingError=0x218270“操作无法完成。(OSStatus错误-12124)。”,NSLocalizedDescription=操作无法完成}

如果您打开了iTunes match,并且文件未下载到设备上,则可能会发生这种情况。你能检查一下你的情况吗?如果是这样,我可以给代码来检测它

+ (BOOL)isDownloadedFromiCloud:(MPMediaItem *)item {
    if ([item valueForProperty:MPMediaItemPropertyAssetURL] != nil) {
        AVURLAsset *asset = [AVURLAsset assetWithURL:[item valueForProperty:MPMediaItemPropertyAssetURL]];
        if (asset.exportable)
            return YES;
    }

    return NO;
}

我也经历过这种情况。你在使用iTunes match吗?可能是文件不在实际设备上。您可以通过检查属性(如
hasProtectedContent
或确保
exportable
YES
)在AVAsset上进行检查


另外,我注意到您使用的是
AVExportSession
preset
avassetexterportpresetapplem4a'。这将重新编码您正在导出的文件。使用
AVAssetExportPresetPassthrough`,只传递信息可能更容易(也更快)。这假设您对现有的文件格式很满意,可能是.m4a、.mp3、.wav等。

您使用的音乐文件路径可能是错误的。试试这个

NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 

NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0]; 
documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@"Music"];
NSError* error;
if (![[NSFileManager defaultManager] fileExistsAtPath:documentsDirectory])
    [[NSFileManager defaultManager] createDirectoryAtPath:documentsDirectory                withIntermediateDirectories:NO attributes:nil error:&error]; 

NSString *exportFile = [documentsDirectory stringByAppendingPathComponent:songName];

if([[NSFileManager defaultManager] fileExistsAtPath:exportFile]) 
{

    [[NSFileManager defaultManager] removeItemAtPath:exportFile error:&error1];

}

if (error != nil) {
NSURL* exportURL = [[NSURL fileURLWithPath:exportFile] retain];

exporter.outputURL = exportURL; 
[exporter exportAsynchronouslyWithCompletionHandler:^{

int exportStatus = exporter.status;

switch (exportStatus) {

    case AVAssetExportSessionStatusFailed: {

        NSError *exportError = exporter.error;

        NSLog (@"AVAssetExportSessionStatusFailed: %@", exportError);

        break;
        }
    }
  }
];
}

我自己对此做了很多研究,其中一些答案可能是相关和正确的。另一件需要注意的事情是,如果我尝试组合具有不同帧速率的视频,我经常会遇到类似的
AVFoundation
错误。检查源文件的帧速率,并检查
CMTime
。在将某些文件添加到
AVMutableComposition

设备上是否有足够的空间放置这些文件之前,您可能需要对其进行预处理?还有,它们是否已经存在于您试图导出它们的地方?