Получить код ошибки -11843 при экспорте mp3 файла в библиотеку ipod начиная с iOS 5.1
Я использую AVAssetExportSession для экспорта файлов mp3/m4a в библиотеку ipod. Этот метод хорошо работает на 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 ответ
Наконец-то нашел обходной путь.
Используйте AVAssetExportSession для экспорта, но добавьте ".mov" в конце URL экспорта. Это должно заставить 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);
}