AVCaptureSession的AVCaptureVideoPreviewLayer随机停止视频流

时间:2016-03-28 14:57:44

标签: ios objective-c avcapturesession

我基于AVCaptureSession创建了一个简单的AVCaptureVideoPreviewLayer,并将该图层添加到UIView中。

AVCaptureSession *session = [[AVCaptureSession alloc] init];
session.sessionPreset = AVCaptureSessionPresetHigh;

AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

NSError *error = nil;
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];

[session addInput:input];

AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
previewLayer.frame = self.cameraView.bounds;

[self.cameraView.layer addSublayer:previewLayer];

[session startRunning];

运行应用程序后,代码似乎工作正常 - 但过了一会儿(大约60.- 90秒),视频随机冻结!

我添加了一个按钮来停止并启动AVCaptureSession agin,如果我在冻结后按下按钮,视频会再次开始工作......

有人知道随机停止视频流的原因吗?

1 个答案:

答案 0 :(得分:4)

尝试为AVCaptureMovieFileOutput设置maxRecordedDuration ,并在AVCaptureSession 中添加 ,这是5000秒记录持续时间的示例代码

AVCaptureMovieFileOutput *MovieFileOutput; =[[AVCaptureMovieFileOutput alloc] init];

Float64 MaxRecordDuration = 5000;           //Maximum RecordDuration in seconds replace 5000 with YOUR_MAX_DURATION
int32_t preferredTimeScale = 30;    //Frames per second
CMTime maxDuration = CMTimeMakeWithSeconds(MaxRecordDuration, preferredTimeScale);    //<<SET MAX DURATION

MovieFileOutput.maxRecordedDuration = maxDuration;
MovieFileOutput.minFreeDiskSpaceLimit = 1024 * 1024;                        
if ([session canAddOutput:MovieFileOutput])
{
    [session addOutput:MovieFileOutput];
}
[session commitConfiguration];
[session startRunning];