我尝试从iPod-Library导出音频文件。我的目标是使用此iPod-Library文件在应用程序文档文件夹中创建新文件。它无法仅为某些项目创建文件。以下是我的代码段。
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 =操作无法完成}
答案 0 :(得分:2)
自己玩了很多,一些答案可能是相关和正确的。需要注意的另一件事是,如果尝试组合具有不同帧速率的视频,我经常会遇到类似的AVFoundation
错误。检查源文件的帧速率并检查CMTime
。在将某些文件添加到AVMutableComposition
之前,您可能需要对其进行预处理。
答案 1 :(得分: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;
}
答案 2 :(得分:0)
我也经历过这一点。你在使用iTunes匹配吗?可能是该文件不在实际设备上。您可以通过检查hasProtectedContent
或确保exportable
为YES
等属性在AVAsset上进行检查。
另外,我注意到您正在使用AVExportSession
预设AVAssetExportPresetAppleM4A'. This will re-encode the file you are exporting. It may be easier (and much faster) to just pass-though instead using
AVAssetExportPresetPassthrough`。这假设您对现有的文件格式感到满意,可能是.m4a,.mp3,.wav等......
答案 3 :(得分: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;
}
}
}
];
}