我正在尝试拍摄使用 iVidCap 插件创建的视频并向其添加音频。基本上与这个问题完全相同:Writing video + generated audio to AVAssetWriterInput, audio stuttering。我已经使用这篇文章中的代码作为基础来尝试自己修改 iVidCap.mm 文件,但应用程序总是在 endRecordingSession 中崩溃。
我不确定我需要如何修改 endRecordingSession 以适应音频(原始插件只是创建一个视频文件)。这是功能:
- (int) endRecordingSession: (VideoDisposition) action {
NSLog(@"Start endRecordingSession");
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NSLog(@"Auto released pool");
NSString *filePath;
BOOL success = false;
[videoWriterInput markAsFinished];
NSLog(@"Mark video writer input as finished");
//[audioWriterInput markAsFinished];
// Wait for the video status to become known.
// Is this really doing anything?
int status = videoWriter.status;
while (status == AVAssetWriterStatusUnknown) {
NSLog(@"Waiting for video to complete...");
[NSThread sleepForTimeInterval:0.5f];
status = videoWriter.status;
}
NSLog(@"Video completed");
@synchronized(self) {
success = [videoWriter finishWriting];
NSLog(@"Success: %@", success);
if (!success) {
// We failed to successfully finalize the video file.
NSLog(@"finishWriting returned NO");
} else {
// The video file was successfully written to the Documents folder.
filePath = [[self getDocumentsFileURL:videoFileName] path];
if (action == Save_Video_To_Album) {
// Move the video to an accessible location on the device.
NSLog(@"Temporary video filePath=%@", filePath);
if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(filePath)) {
NSLog(@"Video IS compatible. Adding it to photo album.");
UISaveVideoAtPathToSavedPhotosAlbum(filePath, self, @selector(copyToPhotoAlbumCompleteFromVideo: didFinishSavingWithError: contextInfo:), nil);
} else {
NSLog(@"Video IS NOT compatible. Could not be added to the photo album.");
success = NO;
}
} else if (action == Discard_Video) {
NSLog(@"Video cancelled. Removing temporary video file: %@", filePath);
[self removeFile:filePath];
}
}
[self cleanupWriter];
}
isRecording = false;
[pool drain];
return success; }
现在它在 [videoWriter finishWriting] 上崩溃。我尝试添加 [audioWriterInput markAsFinished],但随后它崩溃了。我会联系原始海报,因为他们似乎可以正常工作,但似乎没有办法发送私人消息。
有没有人对我如何让它工作或为什么它崩溃有任何建议?我已经尽力解决这个问题,但我对 Obj-C 还是很陌生。如果需要,我可以发布其余代码(其中很多都在前面引用的原始帖子中)。