更新:奇怪的是這個代碼工作正常在iPad 2,但沒有一個iPad 4日創AVExportSession工作在模擬器,iPad 2的而不是iPad的4
更新#2:如果我改變presetName:AVAssetExportPresetHighestQuality
到presetName:AVAssetExportPresetPassThrough
視頻成功出口但我無法在設備中播放它。如果我通過xCode的組織者將應用程序包關閉到我的電腦,我可以播放它。再次,這個問題只發生在iPad 4上,而不是iPad 2,64位模擬器,視網膜模擬器或1x模擬器。
我正在使用AVExportSession
混合一些音頻和視頻。它在模擬器和iPad 2上運行相當愉快,但不是iPad第4代。導出會話出現-11820
錯誤(AVErrorExportFailed
),但這是我能夠從流程中獲得的有用信息的範圍。源文件存在,其他所有內容都在流暢地運行,但不是AVExportSession
。
你能幫助我在設備上工作嗎?
道歉的方法的詳細程度。
-(NSURL*)bindAudioAndVideo:(NSString*)audioFileName videoFileName:(NSString*)videoFileName
{
//documents folder
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsFolder = [[NSString alloc] initWithString:[paths objectAtIndex:0]]; //Get the docs directory
AVMutableComposition* mixComposition = [AVMutableComposition composition];
NSString* audio_inputFileName = audioFileName;
NSString* audio_inputFilePath = [documentsFolder stringByAppendingPathComponent:audio_inputFileName];
NSURL* audio_inputFileUrl = [NSURL fileURLWithPath:audio_inputFilePath];
NSString* video_inputFileName = videoFileName;
NSString* video_inputFilePath = [documentsFolder stringByAppendingPathComponent:video_inputFileName];
NSURL* video_inputFileUrl = [NSURL fileURLWithPath:video_inputFilePath];
NSString* outputFileName = @"outputFile.mp4";
NSString* outputFilePath = [documentsFolder stringByAppendingPathComponent:outputFileName];
NSURL* outputFileUrl = [NSURL fileURLWithPath:outputFilePath];
//Check files actually exist before beginning (they do)
AVMutableComposition* mixComposition = [AVMutableComposition composition];
CMTime nextClipStartTime = kCMTimeZero;
AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:video_inputFileUrl options:nil];
CMTimeRange video_timeRange = CMTimeRangeMake(kCMTimeZero,videoAsset.duration);
AVMutableCompositionTrack *a_compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
[a_compositionVideoTrack insertTimeRange:video_timeRange ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:nextClipStartTime error:nil];
AVURLAsset* audioAsset = [[AVURLAsset alloc]initWithURL:audio_inputFileUrl options:nil];
CMTimeRange audio_timeRange = CMTimeRangeMake(kCMTimeZero, audioAsset.duration);
AVMutableCompositionTrack *b_compositionAudioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
[b_compositionAudioTrack insertTimeRange:audio_timeRange ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:nextClipStartTime error:nil];
AVAssetExportSession* _assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetHighestQuality];
_assetExport.outputFileType = @"com.apple.quicktime-movie";
_assetExport.outputURL = outputFileUrl;
[_assetExport exportAsynchronouslyWithCompletionHandler:
^(void) {
[self addSkipBackupAttributeToItemAtURL:outputFileUrl];
NSLog(@"Completed. Tidy time.");
switch ([_assetExport status]) {
case AVAssetExportSessionStatusCompleted:
NSLog(@"Export Completed");
break;
case AVAssetExportSessionStatusFailed:
NSLog(@"Export failed: %@", [[_assetExport error] localizedDescription]);
NSLog (@"FAIL %@",_assetExport.error); //-11820! I AM A USELESS ERROR CODE
NSLog (@"supportedFileTypes: %@", _assetExport.supportedFileTypes);
break;
case AVAssetExportSessionStatusCancelled:
NSLog(@"Export cancelled");
break;
default:
break;
}
NSTimer *refreshTimer = [NSTimer timerWithTimeInterval:0.1 target:self selector:@selector(exportCompleteRefreshView) userInfo:Nil repeats:NO];
//Throw back to main thread unuless you want really long delays for no reason.
[[NSRunLoop mainRunLoop] addTimer:refreshTimer forMode:NSRunLoopCommonModes];
}
];
return outputFileUrl;
}
爲什麼你不使用常量作爲文件類型?而t =你如何構建你的outputFileURL。在模擬器/設備中,我發現許多問題是由於URL。 – Linuxios
我已經添加了如何構建路徑。 outputFileURL在導出後存在,但它是'零KB'。不知道爲什麼我不使用常量作爲文件類型,我該怎麼做才能改變它? – glenstorey
源視頻和音頻文件的格式是什麼? – ChrisH