切换摄像头后音频/视频不同步 [英] Audio/video out of sync after switch camera

查看:159
本文介绍了切换摄像头后音频/视频不同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个应用程序,可以在录制过程中从设备上的不同摄像机录制视频. 例如.用户从前置摄像头按下按钮开始录制",录制5秒钟后,用户按下切换摄像头"按钮,应用程序将视频源从前置摄像头切换到后置摄像头,并继续录制. 对于摄像机切换,我使用下一个代码:

I'm try create application where I can record video from different cameras on device during recording. For example. User press button "start record" from front camera, after 5 second recording user press button "Switch Camera" and application change video source from front to back camera and recording continue. For camera swithcing I use next code:

NSError *error;
AVCaptureDeviceInput *newVideoInput;
AVCaptureDevicePosition currentCameraPosition = [[videoInput device] position];

if (currentCameraPosition == AVCaptureDevicePositionBack)
{
    currentCameraPosition = AVCaptureDevicePositionFront;
}
else
{
    currentCameraPosition = AVCaptureDevicePositionBack;
}

AVCaptureDevice *backFacingCamera = nil;
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
for (AVCaptureDevice *device in devices) 
{
    if ([device position] == currentCameraPosition)
    {
        backFacingCamera = device;
    }
}
newVideoInput = [[AVCaptureDeviceInput alloc] initWithDevice:backFacingCamera error:&error];

if (newVideoInput != nil)
{
    [_captureSession beginConfiguration];

    [_captureSession removeInput:videoInput];
    if ([_captureSession canAddInput:newVideoInput])
    {
        [_captureSession addInput:newVideoInput];
        videoInput = newVideoInput;
    }
    else
    {
        [_captureSession addInput:videoInput];
    }
    //captureSession.sessionPreset = oriPreset;
    [_captureSession commitConfiguration];
}

_inputCamera = backFacingCamera;

此操作后,请更改视频源并继续写入,但是...音频/视频不同步... 有人能帮我解决这个问题吗?

After this appication change video sourse and continue writing, but... audio/video out of sync... Can anybody hehp me with this problem?

谢谢.

推荐答案

您需要停止录制,切换并重新启动. 相机开关不是即时AFAIK

you need to stop recording, switch and start it again. the camera switch isnt instant AFAIK

不能将您记录到n个文件中,以后再将它们粘贴在一起吗?

cant you record into n files and later stich them together?

尝试使用单个AVMutableComposition轨道,然后为音频设置可变的组合,为视频设置可变的组合. (请参阅使用AVMutableComposition合并两个m4v电影文件-视频不会合并)

try using individual AVMutableComposition tracks and then setting a mutablecomposition for audio and one for video. (see Merging two m4v Movie Files Using AVMutableComposition - Videos Will Not Merge)

这篇关于切换摄像头后音频/视频不同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆