16

我试图从 iPod 库中导出音频文件。我的目标是使用此 iPod-Library 文件在 app Document 文件夹中创建新文件。它无法仅为某些项目创建文件。下面是我的代码片段。

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: Error Domain=AVFoundationErrorDomain Code=-11800“操作无法完成” UserInfo=0x214f20 {NSLocalizedFailureReason=发生未知错误(-12124),NSUnderlyingError=0x218270“操作无法完成。(OSStatus错误-12124 .)", NSLocalizedDescription=操作无法完成}

4

4 回答 4

2

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

于 2014-01-24T15:46:43.707 回答
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;
}
于 2013-09-20T18:37:57.047 回答
0

我也经历过这种情况。你在使用 iTunes 匹配吗?可能是该文件不在实际设备上。您可以通过检查诸如hasProtectedContent或确保exportableis之类的属性来检查 AVAsset YES

另外,我注意到您正在使用AVExportSession预设的AVAssetExportPresetAppleM4A'. This will re-encode the file you are exporting. It may be easier (and much faster) to just pass-though instead usingAVAssetExportPresetPassthrough`。这假设您对现有的文件格式感到满意,可能是 .m4a、.mp3、.wav 等...

于 2013-10-04T02:00:03.703 回答
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;
        }
    }
  }
];
}
于 2013-12-18T10:31:37.723 回答