我基于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,如果在冻结后按下按钮,视频将再次开始工作...

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

最佳答案

尝试为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];

关于ios - AVCaptureSession的AVCaptureVideoPreviewLayer随机停止视频流,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36264915/

10-10 20:44