AVAssetExport 对某些文件失败

16

我尝试从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;
        }
  }
 }];

AVAssetExportSessionStatusFailed: 错误域=AVFoundationErrorDomain 代码=-11800 "操作无法完成" UserInfo=0x214f20 {NSLocalizedFailureReason=发生未知错误(-12124),NSUnderlyingError=0x218270 "操作无法完成。(OSStatus 错误-12124。)", NSLocalizedDescription=操作无法完成}


设备上有足够的空间存放这些文件吗?或者你正在尝试导出的位置已经存在这些文件了吗? - Jack Freeman
4个回答

2

我自己玩了很多次,一些答案可能是相关和正确的。另外要注意的一件事是,如果尝试结合具有不同帧速率的视频,则经常会出现类似的AVFoundation错误。检查源文件的帧速率并检查您的CMTime。您可能需要在将它们添加到AVMutableComposition之前对一些文件进行预处理。


0

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

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

0

我也遇到过这个问题。你是否在使用iTunes Match?可能是文件不在实际设备上。您可以通过检查AVAsset的属性,例如hasProtectedContent或确保exportableYES来检查此内容。

另外,我注意到您正在使用AVExportSession预设AVAssetExportPresetAppleM4A。这将重新编码您要导出的文件。只需使用AVAssetExportPresetPassthrough进行传递可能更容易(并且速度更快)。这假定您对现有文件格式感到满意,可能是.m4a、.mp3、.wav等。


0

如果您开启了iTunes匹配功能并且文件未下载到设备上,就会出现这种情况。您能否检查一下您的情况是否如此?如果是,我可以提供代码来检测它。

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

    return NO;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接