I use the AVAssetExportSession to export mp3/m4a files in ipod library. This method works well on iOS 5.0 and earlier. However after upgrade iOS to 5.1, this method doesn't work for mp3 anymore, but still works for m4a.
Here is the source code.
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;
}
}
}];
Here is the message I get in console:
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}
Anyone knows why? The error message tells me to change the file extension, but it is not reasonable to use other file extension for mp3 file.
Finally found a workaround.
Use AVAssetExportSession to export but append ".mov" at the end of export url. This should make AVAssetExportSession successfully export the song. Last step, rename the exported file with NSFileManager, remove the ".mov" at end.
To rename a file saved in documents directory, you can use NSFileManager as below:
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With