Using exportAsynchronouslyWithCompletionHandler

I am trying to export an audio file from an iPod library. I want to create a new file in this iPod library file. There is a small example in the AV Foundation Programming Guide on Reading and Writing Assets. However, this code does not work for me. 4 errors occurred

Export Status 4 Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo=0x1edfa0 {NSLocalizedFailureReason=An unknown error occurred (-12124), NSUnderlyingError=0x1ebdc0 "The operation couldn't be completed. (OSStatus error -12124.)", NSLocalizedDescription=The operation could not be completed} 

My code

 AVURLAsset *objAvUrlAsset = [[AVURLAsset alloc] initWithURL:[tempArray objectAtIndex:0] options:nil]; NSLog(@"%s objAvUrlAsset %@", __FUNCTION__, objAvUrlAsset); NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:objAvUrlAsset]; NSLog(@"%s compatiblePresets %@", __FUNCTION__, compatiblePresets); if ([compatiblePresets containsObject:AVAssetExportPresetAppleM4A]) { AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:objAvUrlAsset presetName:AVAssetExportPresetAppleM4A]; NSString * exportPath = [NSString stringWithFormat:@"testTrack1.M4A"]; NSURL *exportURL = [NSURL fileURLWithPath:exportPath]; exportSession.outputURL = exportURL; exportSession.outputFileType = AVFileTypeAppleM4A; exportSession.shouldOptimizeForNetworkUse = YES; NSLog(@"%s export Session %@",__FUNCTION__, exportSession); [exportSession exportAsynchronouslyWithCompletionHandler: ^(void) { // export completed NSLog(@"Export Status %d %@", exportSession.status, exportSession.error); }]; [exportSession release]; } 

nslog message

 -[MyLocalMusicLibrary getAllTracks] objAvUrlAsset AVURLAsset: 0x1dd5a0, URL = ipod-library://item/item.mp3?id=-212958261728896866 -[MyLocalMusicLibrary getAllTracks] compatiblePresets ( AVAssetExportPresetAppleM4A, AVAssetExportPreset960x540, AVAssetExportPresetLowQuality, AVAssetExportPresetMediumQuality, AVAssetExportPreset640x480, AVAssetExportPresetHighestQuality, AVAssetExportPreset1280x720 ) -[MyLocalMusicLibrary getAllTracks] export Session AVAssetExportSession: 0x1e85c0, asset = AVURLAsset: 0x1dd5a0, URL = ipod-library://item/item.mp3?id=-212958261728896866, presetName = AVAssetExportPresetAppleM4A, outputFileType = com.apple.m4a-audio -[MyLocalMusicLibrary getAllTracks] Export Status 4 Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo=0x1edfa0 {NSLocalizedFailureReason=An unknown error occurred (-12124), NSUnderlyingError=0x1ebdc0 "The operation couldn't be completed. (OSStatus error -12124.)", NSLocalizedDescription=The operation could not be completed} 

How can I fix these four errors. I need to export AVAssetReader and AVAssetWriter for this.

+4
source share
2 answers

I got the same result from AVAssetExportSession that a completely useless NSError trying to tell you that the path you export does not fully exist. You must make sure that you create the path to the directory in which you place the file using NSFileManager .

+8
source

The problem for me was that I tried to convert the file directly from the tmp directory after getting it from the UIImagePickerController

I needed to save the file in the library first and then iterate over the ALAssetLibrary files to find the last video. Then I converted this video to .mp4 and saved it in the local document directory of the application for use, as I please.

It’s kind of funny that it is necessary, but it works.

0
source

Source: https://habr.com/ru/post/1345681/


All Articles