从iOS 5.1开始在ipod库中导出mp3文件时获取错误代码-11843

时间:2012-03-11 07:31:01

标签: ios

我使用AVAssetExportSession在ipod库中导出mp3 / m4a文件。此方法适用于iOS 5.0及更早版本。然而,在将iOS升级到5.1之后,此方法不再适用于mp3,但仍适用于m4a。

这是源代码。

AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL: [mediaItem assetUrl] options:nil];

NSLog (@"compatible presets for songAsset: %@",[AVAssetExportSession exportPresetsCompatibleWithAsset:songAsset]);

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

NSLog (@"created exporter. supportedFileTypes: %@", exporter.supportedFileTypes);
NSLog(@"output file type=%@",[mediaItem fileType]);
NSLog(@"export file path=%@",exportPath);
exporter.outputFileType =[mediaItem fileType];

NSError *error1;
error1=0;

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

    [[NSFileManager defaultManager] removeItemAtPath:exportPath error:&error1];
    if(error1)
        NSLog(@"%@",error1);

}

NSURL* exportURL = [NSURL fileURLWithPath:exportPath];

exporter.outputURL = exportURL; 

// do the export
[exporter exportAsynchronouslyWithCompletionHandler:^{

    int exportStatus = exporter.status;

    switch (exportStatus) {

        case AVAssetExportSessionStatusFailed: {

            NSError *exportError = exporter.error;

            NSLog (@"AVAssetExportSessionStatusFailed: %@", exportError);
            [delegate convertCancelled:self];
            [exporter release];

            break;
        }

        case AVAssetExportSessionStatusCompleted: {

            NSLog (@"AVAssetExportSessionStatusCompleted");
            [delegate convertDone:self];
            [exporter release];
            break;
        }

        case AVAssetExportSessionStatusUnknown: { NSLog (@"AVAssetExportSessionStatusUnknown");
            break;
        }
        case AVAssetExportSessionStatusExporting: { NSLog (@"AVAssetExportSessionStatusExporting"); 
            break;
        }

        case AVAssetExportSessionStatusCancelled: { NSLog (@"AVAssetExportSessionStatusCancelled");

            NSLog(@"Cancellated");
            [delegate convertCancelled:self];

            break;
        }

        case AVAssetExportSessionStatusWaiting: {
            NSLog (@"AVAssetExportSessionStatusWaiting");
            break;
        }

        default: 
        { NSLog (@"didn't get export status"); 
            break;
        }
    }

}];

以下是我在控制台中收到的消息:

output file type=com.apple.quicktime-movie
export file path=/private/var/mobile/Applications/xxxxxx/tmp/I See the Light - Instrumental cover.mp3
AVAssetExportSessionStatusFailed: Error Domain=AVFoundationErrorDomain Code=-11843 "Cannot write output file" 
UserInfo=0x93876e0 {NSLocalizedRecoverySuggestion=Change the output extension and try again., NSLocalizedDescription=Cannot write output file}

任何人都知道为什么? 错误消息告诉我更改文件扩展名,但使用mp3文件的其他文件扩展名是不合理的。

1 个答案:

答案 0 :(得分:16)

终于找到了解决方法。

使用AVAssetExportSession导出,但在导出URL的末尾附加“.mov”。 这应该使AVAssetExportSession成功导出歌曲。 最后一步,使用NSFileManager重命名导出的文件,最后删除“.mov”。

要重命名保存在文档目录中的文件,可以使用NSFileManager,如下所示:

NSString *exportFile = ... //.. path of saved *.mov file in documents directory
NSString *newPath = [[exportFile stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"newFileName.mp3"];

NSError *renameError = nil;
[[NSFileManager defaultManager] moveItemAtPath:exportFile toPath:newPath error:&renameError];

if (renameError) {
    NSLog (@"renameError=%@",renameError.localizedDescription);
}else {
    NSLog (@" No renameError(Success) :: newPath=%@",newPath);
}