简体   繁体   中英

AVAssetExportSession not working in ios5

In my application I am combining two audio files using AVAssetExportSession and it works fine in earlier ios versions.But in ios5 device its not working. What i am getting is an error

AVAssetExportSessionStatusFailed: Error Domain=AVFoundationErrorDomain Code=-11820 "Cannot Complete Export" UserInfo=0x1df1c0 {NSLocalizedRecoverySuggestion=Try exporting again., NSLocalizedDescription=Cannot Complete Export}

The code that I use for exporting is given below

Did anyone experience the same issue? Please provide your valuable suggestions. I am in an urgent need to fix this issue..

//Export function to export the combined audios as one.
-(void)exportAudioFile:(AVComposition*)combinedComposition
{
    AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:combinedComposition
                                                                           presetName:AVAssetExportPresetPassthrough];
    NSArray *presets =[AVAssetExportSession exportPresetsCompatibleWithAsset:combinedComposition];
    NSLog(@"presets======%@",presets);
    NSLog (@"can export: %@", exportSession.supportedFileTypes);
    NSArray *dirs = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectoryPath = [dirs objectAtIndex:0];
    exportPath = [documentsDirectoryPath stringByAppendingPathComponent:@"CombinedNew.m4a"];
    [[NSFileManager defaultManager] removeItemAtPath:exportPath error:nil];
    exportURL = [NSURL fileURLWithPath:exportPath];
    exportSession.outputURL = exportURL;
    exportSession.outputFileType = @"com.apple.m4a-audio";
    exportSession.shouldOptimizeForNetworkUse = YES;
    [exportSession exportAsynchronouslyWithCompletionHandler:^{
        NSLog (@"i is in your block, exportin. status is %d",
               exportSession.status);
        switch (exportSession.status) 
        {
            case AVAssetExportSessionStatusFailed: 
            {
                // log error to text view
                NSError *exportError = exportSession.error;
                DEBUG_LOG(@"AVAssetExportSessionStatusFailed: %@", exportError);
                [self enableUI];
                break;
            }
            case AVAssetExportSessionStatusCompleted: 
            {
                DEBUG_LOG(@"AVAssetExportSessionStatusCompleted");
                DEBUG_LOG(@"Completed export");
                exportSuccess = YES;
                if (recorderFilePath) 
                {
                    NSError *finalurlError;
                    [[NSFileManager defaultManager]removeItemAtPath:recorderFilePath  error:&finalurlError];
                    finalurlError = nil;
                    [[NSFileManager defaultManager]copyItemAtPath:[exportURL path] toPath:recorderFilePath error:&finalurlError];
                }
                isExported = YES;
                fileUrl = [NSURL fileURLWithPath:recorderFilePath];  
                [self performSelectorInBackground:@selector(updatePlayerForUrl:) withObject:fileUrl];
                break;
            }
            case AVAssetExportSessionStatusUnknown: 
            {   
                DEBUG_LOG(@"AVAssetExportSessionStatusUnknown");                 
                break;
            }
            case AVAssetExportSessionStatusExporting: 
            { 
                DEBUG_LOG(@"AVAssetExportSessionStatusExporting");                 
                break;
            }
            case AVAssetExportSessionStatusCancelled: 
            { 
                DEBUG_LOG(@"AVAssetExportSessionStatusCancelled");
                break;
            }
            case AVAssetExportSessionStatusWaiting: 
            { 
                DEBUG_LOG(@"AVAssetExportSessionStatusWaiting");                 
                break;
            }
            default: 
            { 
                DEBUG_LOG(@"didn't get export status");                
                break;
            }

        };
    }];
    [exportSession release];
}

I sorted out the answer for myself and would like to share it with others who experience the same problem.

The problem is that for some reason the AVAssetExportPresetPassthrough is not working properly in ios5. Substituting it with AVAssetExportPresetAppleM4A solved the issue.
But it takes longer to export now.

Perhaps a way around it is to use AVAssetWriter directly and not use AVAssetExportSession. Please please please, file a bug at http://bugreport.apple.com so that maybe it gets fixed in the next rev of iOS5. (I filed one of my own, but the more the merrier.)

As a workaround, I found that using .mov as the file extension, then renaming it back to mp3 seems to work. I dont need to do this for m4a files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM