从iOS 5.1开始,在导出mp3文件的同时获取错误代码-11843

我使用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文件使用其他文件扩展名是不合理的。

终于find了一个解决方法。

使用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); }