我希望找到一种方法,在前后摄像头之间切换时保持无缝的音频跟踪。市场上有许多应用程序可以做到这一点,例如SnapChat...
解决方案应该使用AVCaptureSession和AVAssetWriter。此外,它不应明确使用AVMutableComposition,因为在AVMutableComposition和AVCaptureSession ATM之间存在一个错误bug。此外,我无法承担后处理时间。
目前,当我更改视频输入时,音频录制会跳过并失去同步。
我将包括可能相关的代码。
翻转相机
也许需要调整音频采样时间戳?
解决方案应该使用AVCaptureSession和AVAssetWriter。此外,它不应明确使用AVMutableComposition,因为在AVMutableComposition和AVCaptureSession ATM之间存在一个错误bug。此外,我无法承担后处理时间。
目前,当我更改视频输入时,音频录制会跳过并失去同步。
我将包括可能相关的代码。
翻转相机
-(void) updateCameraDirection:(CamDirection)vCameraDirection {
if(session) {
AVCaptureDeviceInput* currentInput;
AVCaptureDeviceInput* newInput;
BOOL videoMirrored = NO;
switch (vCameraDirection) {
case CamDirection_Front:
currentInput = input_Back;
newInput = input_Front;
videoMirrored = NO;
break;
case CamDirection_Back:
currentInput = input_Front;
newInput = input_Back;
videoMirrored = YES;
break;
default:
break;
}
[session beginConfiguration];
//disconnect old input
[session removeInput:currentInput];
//connect new input
[session addInput:newInput];
//get new data connection and config
dataOutputVideoConnection = [dataOutputVideo connectionWithMediaType:AVMediaTypeVideo];
dataOutputVideoConnection.videoOrientation = AVCaptureVideoOrientationPortrait;
dataOutputVideoConnection.videoMirrored = videoMirrored;
//finish
[session commitConfiguration];
}
}
样品缓冲区
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
//not active
if(!recordingVideo)
return;
//start session if not started
if(!startedSession) {
startedSession = YES;
[assetWriter startSessionAtSourceTime:CMSampleBufferGetPresentationTimeStamp(sampleBuffer)];
}
//Process sample buffers
if (connection == dataOutputAudioConnection) {
if([assetWriterInputAudio isReadyForMoreMediaData]) {
BOOL success = [assetWriterInputAudio appendSampleBuffer:sampleBuffer];
//…
}
} else if (connection == dataOutputVideoConnection) {
if([assetWriterInputVideo isReadyForMoreMediaData]) {
BOOL success = [assetWriterInputVideo appendSampleBuffer:sampleBuffer];
//…
}
}
}
也许需要调整音频采样时间戳?