AVAssetExportSession not working in ios5

混江龙づ霸主 提交于 2019-12-08 08:43:21

问题


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];
}

回答1:


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.




回答2:


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.)




回答3:


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.



来源:https://stackoverflow.com/questions/8019033/avassetexportsession-not-working-in-ios5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!